LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Which is correct time elapsed in running code (https://www.linuxquestions.org/questions/linux-newbie-8/which-is-correct-time-elapsed-in-running-code-4175414738/)

unkn(0)wn 07-03-2012 04:24 PM

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.

SaintDanBert 07-03-2012 06:14 PM

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


chrism01 07-03-2012 06:15 PM

Try also
Code:

\time ./a.out 1000 400
for more info/an alternate view

unkn(0)wn 07-03-2012 06:35 PM

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

syg00 07-03-2012 07:41 PM

Quote:

Originally Posted by unkn(0)wn (Post 4718558)
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.

unkn(0)wn 07-03-2012 09:58 PM

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.

syg00 07-03-2012 11:27 PM

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".

unkn(0)wn 07-04-2012 08:39 AM

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.

unkn(0)wn 07-04-2012 08:54 AM

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:29 AM.