Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

for(loops)

Name: Anonymous 2010-10-22 16:52



int main(void) {
  for(int n=0;n!=50;n++)
    cout << "Hello, world no. %d\n" << n << endl;
}

int main(void) {
  for(int n=0;n!=50;++n)
    cout << "Hello, world no. %d\n" << n << endl;
}

int main(void) {
  for(int n=0;n!=50;n=n+1)
    cout << "Hello, world no. %d\n" << n << endl;
}

int main(void) {
  for(int n=0;n!=50;n+=1)
    cout << "Hello, world no. %d\n" << n << endl;
}


The second code runs fastest, followed by the first code, then the third and fourth tied. I though these were logically the same? Why the speed difference.

Name: Anonymous 2010-10-22 16:55

>int main (void)
also
>++n
troll thread

Name: Anonymous 2010-10-22 16:58

>>2
>(no space)Quote

Back to Fuck off, ``faggot''.

Name: Anonymous 2010-10-22 17:06

printf control sequences passed into std::cout
Does this actually work? I don't use SEPPLES myself.

Name: Anonymous 2010-10-22 17:17

>>3
Fuck off yourself.

>>4
Are you retarded?

>>1
You obviously are.

Name: Anonymous 2010-10-22 18:18

>>1
Google Rotational Velocidensity, OP.

Name: Anonymous 2010-10-23 1:58

wow cool benchmark for write sysacall

Name: Anonymous 2010-10-23 12:59

6 people missed the Like button.

Name: Anonymous 2010-10-23 13:52

less of this

Was this post VIP quality?

Name: Anonymous 2010-10-23 13:59

>>8
Lol'd.

>>4
That would be fun. Just like magic quotes!!

But really, I = IV.

Name: Anonymous 2010-10-23 15:36

>>10
I'm sorry, but one was never equal to four, not even in ancient Romany.

Name: Anonymous 2010-10-23 15:42

>>6
back to /g/, please

Name: Anonymous 2010-10-23 17:21

I don't even know how many times I've seen this stupid thread on /prog/, or more precisely the thread discussing performance differences between post/pre increment/decrement operators.

The generated code is usually the same most of the time, however it depends on a variety of factors:if you are going to use the value after the operation, the rest of the code (seemingly unrelated, but affects register allocation and overall allocation strategy for the variables in your function), the compiler (and what kinds of optimizations can it perform(dataflow, SSA, etc)), its optimization settings. In the rare cases when the generated code is different, most of the time, the differences are very tiny, such as one extra instruction (such as an extra mov), usually such instructions could even be executed in parallel by some modern CPUs when possible, thus this has no real effect on speed, however on other CPUs it might have a very minor effect. Then you have larger differences which are unrelated to your code at all, such as the OS and its scheduler (realtime or not), which is likely where your apparently speed differences come from - calculating the errors induced by the scheduler is important, but you'll find out that you won't really be able to notice a real difference in performance unless you run it an outrageous amount of time, but then you also have to factor in benchmarking errors and whatnot.

So the answer to OP's question is that the difference in performance is either inexistent, or is incredibly tiny and platform specific and he should never, ever worry about it in practice.

>>6's joke answer somehow even feels appropriate here, because OP is trying to measure something he does not really understand, and something which might not even exist in the generated code (depends on his compiler, etc). It reminds me of silly things like talks of consciousness in microtubules...

Name: Anonymous 2010-10-23 18:19

>>13
TOP-rated post!
What are you doing on /prog/?

Name: Anonymous 2010-10-23 18:44

1. Use fprintf ("fast printf") instead of printf.
2. ++i is faster than both i++ and i = i + 1.
3. void main(void) is faster than int main(void) or int main(int, char **) since no value needs to be returned to the OS.
4. Swapping with exclusive-or (a^=b^=a^=b swaps a and b) is faster than using a temporary. This works for all types (including structures), but not on all compilers. Some compilers may also give you a harmless warning.
5. Static storage duration objects are faster than automatic storage duration objects because the CPU doesn't have to set aside storage on the stack every time a function is called. Make your loop indexes global so that you can use them everywhere:
int i;
void func(void) { for (i = 0; i < 10; i++) ; /* ... */ }
void func2(void) { for (i = 0; i < 20; i++) ; /* ... */ }
/* ... */

6. Compilers often give more memory to arrays than you asked for. Here's how to check how big an array actually is (memset returns a null pointer if the size you passed to it is bigger than the size of the array you passed to it):
int arr[256];
size_t realsize;
for (realsize = 0; realsize <= SIZE_MAX; ++realsize)
        if (!memset(arr, 0, realsize)) break;
/* now you know that arr actually has realsize / sizeof (int) elements */

If you combine this with #5, your program will be faster in the long run (but this usually doesn't work for short programs).

Name: Anonymous 2010-10-23 19:39

Make your loop indexes global so that you can use them everywhere:
Okay, I laughed.

Name: Anonymous 2010-10-23 22:10

>>13
consciousness in microtubules...
Penrose, always good for a laugh.

Name: Anonymous 2010-10-24 6:36

>>5
Thank you Void-sama.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List