开发者

Can someone explain unfamiliar code which seems to measure runtime

[user@host][~] 6> cc -xO4 timing.c -o timing
[user@host][~] 6> ./timing
Total run time: 0
[user@host][~] 6>

Could someone tell me what this snippet of code does and where it is used? I am supposing from the command line.

Aso, it seems开发者_StackOverflow like someone is testing a .c program for run time. But why is Total run time 0? Im not familiar with this command -xO4, either.

I was showed this example aswell, where the run time isnt 0. Obviously the -xO4 does something or...?

[user@host][~] 6> cc timing.c -o timing
[user@host][~] 6> ./timing
Total run time: 3520000
[user@host][~] 6>


the first line compiles a c program. The second line runs it. I'm guessing it takes less than a second to run so the reported time rounds to zero.


Just on the -XO4 switch, quote:

-xO4

Performs loop unrolling, avoids creating stack frames when possible, and automatically inlines functions contained in the same file, as well as the optimization done by levels 2 and 3. Note that this optimization level can cause stack traces from adb and dbx to be incorrect.

though this slightly depends on the target processor. I.e., this is for a SPARC processor and the above for x86.


If the question is: "If timing.c is compiled with -xO4, why does the run time go from 3520000 to 0?", then the answer is that -x04 performs optimizations* that make timing run faster than if it were built without the flag. We don't know what units are used for run time. If it's milliseconds, then your speedup is from about 1 hour runtime to less than a millisecond. That's a little surprising.

*(See Abel's post for specific details.)


The first line compiles a C program from the source file timing.c into an executable called timing. The -xO4 is a flag passed to the compiler. It is platform / compiler specific, but it looks like an option to do level 4 optimization. The second line executes the code. You can find out how long "timer" takes to execute with the "time " program.

time ./timer


Ok, there are several explanations to your question, after you've edited it to show the second execution, without the -x04 command line argument.

The first explanation I can think of is that the -x04 argument, which @Abel has done a good job of documenting here, unrolls so much of the code that some basic overhead in a loop disappears completely. More likely, some looping code disappears completely.

The second explanation is that the command loads information from disk, or a similar slow medium, and the second execution is actually the first, where it actually had to wait for the disk. In the second execution (the one you show first), the data to be loaded is already in the cache, so it's just copied memory to memory, which is much faster.

In either case, it's very strange that some optimization and/or cache technique removes all overhead and code execution time, so most likely the code in the first execution you've shown takes less time than the accuracy of the timing method being used. For instance, if the accuracy is 1ms, the total runtime is less than 1ms, so it looks like zero.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜