i was wondering whish is faster for(;;) or while(true) as for myself i think for(;;) is faster coz it doesnt chk if value is 1 is just keep going on and on
Printable View
i was wondering whish is faster for(;;) or while(true) as for myself i think for(;;) is faster coz it doesnt chk if value is 1 is just keep going on and on
But I don't have a ;) key...
I think most compilers will optimize that code down to the same assembler instructions anyway, so it doesn't matter.
QuantumPete
I remember reading in an old book about optimisation in C that noted that compilers regarded old at the time of writing would produce code that evaluated the constant expression in:
or more accurately, since true is a C++ keyword and C99 macro:Code:while (true)
but those same compilers would correctly produce "infinite loop" code when they encountered:Code:while (1)
No doubt this is not true of modern compilers, but it is of historical interest, and thus I decided to prefer the latter because it is arguably more "canonical" :)Code:for (;;)
becauseQuote:
Hmm, I wonder why that is.
could be a mistake and written instead ofCode:while(1)
for exampleCode:while(l)
On the same reason
could produce a warning about assignment in the if instead of comparisonCode:if(f=fopen...)
So do you think it is a good idea to subject people to warnings for mistakes they did not make?
Snafuist posted this rather amusing set of related facts the other day.
I always get plenty of
warning C4706: assignment within conditional expression
when I do something like
When it's exactly what I want to say. But then I always use warning levels = 4 (highest).Code:if (!(lf = fopen(LOGFILE_NAME, "a"))) {
printf("Failure to create or open logfile: %s\n", LOGFILE_NAME);
...
}