Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8115853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T03:36:03+00:00 2026-06-06T03:36:03+00:00

I have this statement in my c program and I want to optimize. By

  • 0

I have this statement in my c program and I want to optimize. By optimization I particularly want to refer to bitwise operators (but any other suggestion is also fine).

uint64_t h_one = hash[0];
uint64_t h_two = hash[1];
for ( int i=0; i<k; ++i )
{
    (uint64_t *) k_hash[i] = ( h_one + i * h_two ) % size;   //suggest some optimization for this line.
} 

Any suggestion will be of great help.

Edit:
As of now size can be any int but it is not a problem and we can round it up to the next prime (but may be not a power of two as for larger values the power of 2 increases rapidly and it will lead to much wastage of memory)

h_two is a 64 bit int(basically a chuck of 64 bytes).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T03:36:04+00:00Added an answer on June 6, 2026 at 3:36 am

    so essentially you’re doing

    k_0 = h_1 mod s
    k_1 = h_1 + h_2 mod s = k_0 + h_2 mod s
    k_2 = h_1 + h_2 + h_2 mod s = k_1 + h_2 mod s
    ..
    k_n = k_(n-1) + h_2 mod s
    

    Depending on overflow issues (which shouldn’t differ from the original if size is less than half of 2**64), this could be faster (less easy to parallelize though):

    uint64_t h_one = hash[0];
    uint64_t h_two = hash[1];
    k_hash[0] = h_one % size;
    for ( int i=1; i<k; ++i )
    {
        (uint64_t *) k_hash[i] = ( k_hash[i-1] + h_two ) % size;
    } 
    

    Note there is a possibility that your compiler already came to this form, depending on which optimization flags you use.

    Of course this only eliminated one multiplication. If you want to eliminate or reduce the modulo, I guess that based on h_two%size and h_1%size you can predetermine the steps where you have to explicitly call %size, something like this:

    uint64_t h_one = hash[0]%size;
    uint64_t h_two = hash[1]%size;
    k_hash[0] = h_one;
    step = (size-(h_one))/(h_two)-1;
    for ( int i=1; i<k; ++i )
    {
        (uint64_t *) k_hash[i] = ( k_hash[i-1] + h_two );
        if(i==step)
        {
            k_hash[i] %= size;
        }
    } 
    

    Note I’m not sure of the formula (didn’t test it), it’s more a general idea. This would greatly depend on how good your branch prediction is (and how big a performance-hit a misprediction is). ALso it’s only likely to help if step is big.

    edit: or more simple (and probably with the same performance) -thanks to Mystical:

    uint64_t h_one = hash[0]%size;
    uint64_t h_two = hash[1]%size;
    k_hash[0] = h_one;
    for ( int i=1; i<k; ++i )
    {
        (uint64_t *) k_hash[i] = ( k_hash[i-1] + h_two );
        if(k_hash[i] > size)
        {
            k_hash[i] -= size;
        }
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have kept hearing this statement. Switch..Case is Evil for code maintenance, but it
I don't know how to code this but i want program to skip or
I have this code in my program: (I included the cout statements for debugging
I currently have this statement in my SQL View (SQL Server 2008 R2) which
I have this sql statement: CREATE TABLE [dbo].[User]( [UserId] [int] IDENTITY(1,1) NOT NULL, [FirstName]
i have this MySQL statement from a search page, the user enters there postcode
I have this if statement that tests for the 2 conditions below. The second
I have this update statement: mysql_query (UPDATE loan SET loan_reff_id='$_POST[loan_reff_id]', commit_date='$_POST[commit_date]',app_loan_type='Tertiary Loan', app_ln_amnt='$_POST[app_ln_amnt]', institution_name='$_POST[institution_name]',
I have seen this statement in many of the documention samples, like here This
I have this very simple sql statement: SELECT max_dose FROM psychotropes WHERE (patient_meds.psychotrope =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.