For loop optimization
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 2 of 2

Thread: For loop optimization

Hybrid View

  1. #1
    Join Date
    Apr 2009

    For loop optimization

    I have identified the bottleneck in my code, and I was hoping for some advice on how to get the code running more quickly/efficiently.

    for(s = 0; s < start.size(); s++){
        inc = factor_base_index[s];
        mpz_set_ui(base2, inc);
        sz = mpz_sizeinbase(base2, 2);
        for(st = start[s]; st < 262144; st += inc){
            //increment the zero array by ~log2(inc)
            mpz_add_ui(zero[st], zero[st], sz);
    The bold line is where the majority of the work is being done.

    All this is doing is taking a prime number, storing it in the variable inc, finding the number of bits in inc, and incrementing the array 'zero' by the number of bits.

    Any help would be really appreciated

  2. #2
    Join Date
    Feb 2011
    United States

    Re: For loop optimization

    Here are some methods of calculating log2 of a number:

    Some more high performance than others.
    Best Regards,

    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

This is a survey!

HTML5 Development Center