I am having trouble with a simple no-opp loop on linux
when compiled with gcc, the loop is ignored (unles I have something in its block)Code:for (int i=0 ; i < 999999 ; i++) {}
Anyway to avoid this behavior ??
Printable View
I am having trouble with a simple no-opp loop on linux
when compiled with gcc, the loop is ignored (unles I have something in its block)Code:for (int i=0 ; i < 999999 ; i++) {}
Anyway to avoid this behavior ??
turn off optimization? (-O0)
also, are you sure the loop is ignored? on modern computers 999999 iterations is nothing.
try
runs for just over 1 second on my 2ghz Core 2 Duo.Code:for (int i = 0; i < 3e8; ++i) { }
loop is ignored, bec when I run the prog (it is very short) it finishes instantly, but when I print i it takes some time to complete.
printing calls a OS function that is taking up most of the time
Code:cyberfish@cyberfish-laptop:/tmp$ echo "int main() { for (int i = 0; i < 3e8; ++i ) ; }" > a.cpp
cyberfish@cyberfish-laptop:/tmp$ g++ a.cpp
cyberfish@cyberfish-laptop:/tmp$ time ./a.out
real 0m1.225s
user 0m1.200s
sys 0m0.012s
- niceQuote:
runs for just over 1 second on my 2ghz Core 2 Duo.
I am runing linux over a virtual machine on vista on P4 2.66 so it should be slower.
Anyway I fixed the problem like this:
Code:for (int i=0 ; i < 999999 ; i++)
for (int x=0 ; x < 9999 ; x++) {};
that is more or less the same as
Code:for (int i=0 ; i < 999999*9999 ; i++);
Very nice trick, did not know about that.
Thanks !
you didn't know how to multiply two numbers together?
embedding the second for loop in the original multiplies the iterations of the outer by the inner.
Do you understand that the only reason it doesn't delay before finishing is because a modern computer can compute over 2 billion instructions a second?
A better way would be to declare i as being volatile. This will at least continue to do the same thing at various optimisation levels.
But what's the point of doing nothing 999999 times?
On modern processors, it's a fine grained way of modifying the fan rotation speeds ;)
On modern computers, it's a nice way of eating up the cpu for useless tasks. Use an API to delay instead. I don't know about any apis for linux though.
Yes, in Linux there is a wonderful little function called "usleep" that "waits" for the same amount of time whatever processor you have, whether it's a slow processor, or a fast one, and you specify the number of microseconds, so 1000000 (1 million) will take 1 second.
Using "timing with empty loops" is very much dependant on how fast the processor makes the loop and how clever the compiler is - but I believe at least some time back that gcc was actually NOT optimizing away empty loops entirely, but it would potentially move things out of the loop, if the loop content was "unchanging", e.g.would become:Code:for(i = 0; i < 10000; i++)
x = sin(1);
This can make a big difference!Code:x = sin(1);
for(i = 0; i < 10000; i++)
;
[Oh, and it may also unroll the loop FIRST, then stuff things outside, so the loop may count up by 4 or 8 at a time, I think].
And I'm pretty sure that microsofts compilers will do even more than that.
In a Windows application, the function "Sleep" will allow the application to "sleep" for a specified number of milliseconds.
Footnote: Although the time is specified in milliseconds or microseconds, neither Linux nor Windows are what is called "Real Time OS's", so the precision of any timing is very dependant on what else is going on in the system. The precision that you can expect is something in the region of 10ms or worse - which is perfectly fine for human waiting times, but if you want to make perfect notes by tweaking the pins connected to a speaker, you'll be a bit out of tune now and again. This, however, is no different if you were to use any other timing method, because for precise timing, you NEED a real-time OS.
--
Mats
In general, a do-nothing loop is a bad thing on on a PC.* It requres 100% CPU time to do nothing, and the multitasking operating system will take cycles away from your other programs in order to allocate time to your do-nothing loop. Your program becomes a "hog", slowing down any other programs that are running. (A few milliseconds usually won't hurt anything, although it might foul-up something like real-time audio/video.)
When you use Sleep() it sleeps your program, freeing-up the CPU time for other programs. Sleep() is not Standard ANSI/ISO C++, but it's best to use a non-standard function in this situation.
* The operating system does run a do-nothing loop when the CUP is "idle", but this is the lowest priority task. And, it's OK (usually it's required) to use do-nothing loops in an embedded system, where you have full control of all running programs and you are in charge of allocating CPU cycles.