LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 07-03-2012, 04:24 PM   #1
unkn(0)wn
Member
 
Registered: Aug 2011
Distribution: Slackware 14, Debian 7.0.0 Wheezy, Windows 7, Windows 8
Posts: 117

Rep: Reputation: Disabled
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
 
Old 07-03-2012, 06:14 PM   #2
SaintDanBert
Senior Member
 
Registered: Jan 2009
Location: "North Shore" Louisiana USA
Distribution: Mint v21.3 & v22.x with Cinnamon
Posts: 1,792
Blog Entries: 3

Rep: Reputation: 108Reputation: 108
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.
Old 07-03-2012, 06:15 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,397

Rep: Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777
Try also
Code:
\time ./a.out 1000 400
for more info/an alternate view
 
1 members found this post helpful.
Old 07-03-2012, 06:35 PM   #4
unkn(0)wn
Member
 
Registered: Aug 2011
Distribution: Slackware 14, Debian 7.0.0 Wheezy, Windows 7, Windows 8
Posts: 117

Original Poster
Rep: Reputation: Disabled
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
 
Old 07-03-2012, 07:41 PM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,251

Rep: Reputation: 4159Reputation: 4159Reputation: 4159Reputation: 4159Reputation: 4159Reputation: 4159Reputation: 4159Reputation: 4159Reputation: 4159Reputation: 4159Reputation: 4159
Quote:
Originally Posted by unkn(0)wn View Post
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.
Old 07-03-2012, 09:58 PM   #6
unkn(0)wn
Member
 
Registered: Aug 2011
Distribution: Slackware 14, Debian 7.0.0 Wheezy, Windows 7, Windows 8
Posts: 117

Original Poster
Rep: Reputation: Disabled
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.
 
Old 07-03-2012, 11:27 PM   #7
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,251

Rep: Reputation: 4159Reputation: 4159Reputation: 4159Reputation: 4159Reputation: 4159Reputation: 4159Reputation: 4159Reputation: 4159Reputation: 4159Reputation: 4159Reputation: 4159
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".
 
Old 07-04-2012, 08:39 AM   #8
unkn(0)wn
Member
 
Registered: Aug 2011
Distribution: Slackware 14, Debian 7.0.0 Wheezy, Windows 7, Windows 8
Posts: 117

Original Poster
Rep: Reputation: Disabled
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.
 
Old 07-04-2012, 08:54 AM   #9
unkn(0)wn
Member
 
Registered: Aug 2011
Distribution: Slackware 14, Debian 7.0.0 Wheezy, Windows 7, Windows 8
Posts: 117

Original Poster
Rep: Reputation: Disabled
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
elapsed time calculation from two date strings hisunday Linux - Newbie 3 03-28-2012 06:00 PM
Get elapsed time HarryBoy Programming 2 08-08-2008 06:13 AM
diff. between elapsed time and cpu time madhugp Linux - General 1 01-15-2007 07:57 AM
find elapsed time innuendo_98 Programming 2 11-10-2005 08:03 AM
getting elapsed time.. Mad_C Programming 2 04-01-2003 01:12 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration