Linux - Newbie This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
|
07-03-2012, 04:24 PM
|
#1
|
Member
Registered: Aug 2011
Distribution: Slackware 14, Debian 7.0.0 Wheezy, Windows 7, Windows 8
Posts: 117
Rep:
|
Which is correct time elapsed in running code
Hello,
i used two methods to compute time elapsed in one of my program
First is
from ctime library,
Code:
clock_t start=clock();
...Loops...
clock_t stop=clock();
cout<<"Time required : "<<(double)(stop-start)/CLOCKS_PER_SEC;
And Second from Bash command "time"
Code:
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
|
|
|
07-03-2012, 06:14 PM
|
#2
|
Senior Member
Registered: Jan 2009
Location: "North Shore" Louisiana USA
Distribution: Mint v21.3 & v22.x with Cinnamon
Posts: 1,792
Rep:
|
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.
|
|
1 members found this post helpful.
|
07-03-2012, 06:15 PM
|
#3
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,397
|
Try also
Code:
\time ./a.out 1000 400
for more info/an alternate view
|
|
1 members found this post helpful.
|
07-03-2012, 06:35 PM
|
#4
|
Member
Registered: Aug 2011
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?
Please
|
|
|
07-03-2012, 07:41 PM
|
#5
|
LQ Veteran
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,251
|
Quote:
Originally Posted by unkn(0)wn
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.
|
|
1 members found this post helpful.
|
07-03-2012, 09:58 PM
|
#6
|
Member
Registered: Aug 2011
Distribution: Slackware 14, Debian 7.0.0 Wheezy, Windows 7, Windows 8
Posts: 117
Original Poster
Rep:
|
Hmm.. That's what i thought too. Well i think i am clear.
BTW, i'll be using
Code:
#include <sys/time.h>
timeval start, end;
gettimeofday(&start, NULL);
//...
gettimeofday(&end, NULL);
to get more accurate time.
Thank you all for your helps.
|
|
|
07-03-2012, 11:27 PM
|
#7
|
LQ Veteran
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,251
|
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".
|
|
|
07-04-2012, 08:39 AM
|
#8
|
Member
Registered: Aug 2011
Distribution: Slackware 14, Debian 7.0.0 Wheezy, Windows 7, Windows 8
Posts: 117
Original Poster
Rep:
|
Thank you syg00, But i am working under a Project on Parallel Processing. So i must have to use more than one cores and threads.
|
|
|
07-04-2012, 08:54 AM
|
#9
|
Member
Registered: Aug 2011
Distribution: Slackware 14, Debian 7.0.0 Wheezy, Windows 7, Windows 8
Posts: 117
Original Poster
Rep:
|
Here's the link of an example of gettimeofday() :
http://www.ccplusplus.com/2011/11/ge...y-example.html
As gnu-time uses gettimeofday() in order to get microseconds precision.
|
|
|
All times are GMT -5. The time now is 12:40 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|