[SOLVED] Which is correct time elapsed in running code
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
me@maths131:~$ gcc -Wall -fopenmp canon.cpp -lstdc++
me@maths131:~$ time ./a.out 1000 400
Time required : 10.29 //Time came by using clock_t
real 0m5.187s
user 0m10.289s
sys 0m0.016s
Now as you can see one is exactly half of other. And i also computed time using stopwatch, it comes in favor of command "time".
I am confused which is right? And its important.
PS: if "time" is right, then suggest some commands to use that time in cpp code in runtime. And i was coding OpenMP (Parallel Processing) in CPU with two cores.
Last edited by unkn(0)wn; 07-03-2012 at 06:35 PM.
Reason: Forgot to tell about OpenMP
In your first fragment, it appears that START and STOP are reversed
... or do my old eyes play tricks on me?
The following might speak to some of your concerns, but I don't know for certain. I also have not direct knowledge of how the results from various system calls and commands are affected by multi-core processors.
The man page for the 'time' command offers the following:
Code:
The elapsed time is not collected atomically with the execution of the program; as a result, in bizarre circumstances (if the time command gets stopped or swapped out in between when the program being timed exits and when time calculates how long it took to run), it could be much larger than the actual execution time.
When the running time of a command is very nearly zero, some values (e.g., the percentage of CPU used) may be reported as either zero (which is wrong) or a question mark.
Most information shown by time is derived from the wait3(2) system call. The numbers are only as good as those returned by wait3(2). On systems that do not have a wait3(2) call that returns status information, the times(2) system call is used instead. However, it provides much less information than wait3(2), so on those systems time reports the majority of the resources as zero.
and also:
Code:
Users of the bash shell need to use an explicit path in order to run the external time command and not the shell builtin variant. On system where time is installed in /usr/bin, the first example would become
/usr/bin/time wc /etc/hosts
Last edited by SaintDanBert; 07-03-2012 at 06:16 PM.
Distribution: Slackware 14, Debian 7.0.0 Wheezy, Windows 7, Windows 8
Posts: 117
Original Poster
Rep:
Thank you for your replies, and sorry, yes i mistyped that.
Somehow i know that time command is giving me correct result. I compared it with stopwatch and found approx equal.
Actually, i have to run loop for 500 times and calculate time elapsed for each loop. So i need "clock_t" to trigger time. But when i checked the correctness of "clock_t" i got confused. Why is it wrong? Because program is a parallel algorithm using two cores simultaneously? Is the time shown by "clock_t" 10.29 is exactly two times (because of 2 cores) of its actual value? Or any other reasons?
Why is it wrong? Because program is a parallel algorithm using two cores simultaneously? Is the time shown by "clock_t" 10.29 is exactly two times (because of 2 cores) of its actual value?
This would be my guess - clock_t would simply accumulate used clock ticks - unnormalized.
If you're interested, you might try rebooting with maxcpus=1. That way all the code will ahve to run on the one processor, and should make the numbers more "sensible".
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.