Find the answer to your Linux question:
Results 1 to 5 of 5
Code: for &#40; int i = 0; i < x.size&#40;&#41;; i++ &#41; &#123; ... &#125; In the code above, the condition of this for loop is tested by a function ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Engineer
    Join Date
    Nov 2002
    Location
    Queens, NY
    Posts
    1,319

    compile optimization


    Code:
    for &#40; int i = 0; i < x.size&#40;&#41;; i++ &#41; &#123;
      ...
    &#125;
    In the code above, the condition of this for loop is tested by a function call. If x.size() returns a relatively small number, it won't make much difference in speed. However, what happens if x.size() returns 100,000,000? This results in x.size() being called 100,000,001 times. A better solution would can be demonstrated in the follwing code.
    Code:
    int stop = x.size&#40;&#41;;
    for &#40; int i = 0; i < stop; i++ &#41; &#123;
      ...
    &#125;
    As you can see, all I did was store the value from x.size() to variable stop. I've read that some compilers will optimize the code that I used in the first segment. I'm assuming that optimization is performed on the loop by manipulating the loop condition as I have done in the second segment. Does anyone know anything about this? Does gcc/g++ have these options?
    The best things in life are free.

  2. #2
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    It cannot do this normally, since it doesn't now what will happen if x.size isn't called as many times as it's supposed to. Maybe it changes global variables or returns different values each time?
    However, in GCC, these two will be the same:
    Code:
    int size&#40;void&#41; &#123;
        return&#40;something good&#41;;
    &#125;
    ...
    stop = size&#40;&#41;;
    for&#40;i = 0; i < stop; i++&#41; &#123;
        ....
    &#125;
    Code:
    int __attribute__ &#40;&#40;pure&#41;&#41; size&#40;void&#41; &#123;
        return&#40;something good&#41;;
    &#125;
    ...
    for&#40;i = 0; i < size&#40;&#41;; i++&#41; &#123;
        ....
    &#125;
    __attribute__ ((pure)) specifies gives size about the same status as an arithmetic operator, in that the compiler knows that it doesn't modify anything, and it return value depends only on its parameters and on global variables. To be even more strict, specify __attribute__ ((const)), which speicifies that the function doesn't even depend on global values, and doesn't examine any pointers that it is passed.

    The strange is that the GCC texinfo manual clearly specifies this, but when I try it and look at the assembler output, it doesn't optimize it that way. I don't know what's wrong; maybe I've just missed some little thing.

    Neither do I know if it works in C++.

  3. #3
    Linux Engineer
    Join Date
    Nov 2002
    Location
    Queens, NY
    Posts
    1,319

    thanks

    Dolda,

    It looks like I'll have to do some more reading up on this topic. Just out of curiosity, where did you get this info from?
    The best things in life are free.

  4. #4
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    In GCC's texinfo documention, under "C Extensions" and "Function Attributes".

  5. #5
    Linux Engineer
    Join Date
    Nov 2002
    Location
    Queens, NY
    Posts
    1,319
    Dolda,

    Not that I'm ungrateful but is there anyone else besides Dolda who answers my questions? I gotta say majority(90%) are all answered by Dolda.
    The best things in life are free.

Posting Permissions

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