time taken

I am trying to find out time taken by a function to compute how much time it takes


    struct timeval myTime_1;
    ::gettimeofday(&myTime_1, NULL);
callFunc()

    struct timeval myTime_2;
    ::gettimeofday(&myTime_2, NULL);

printf ("Time taken = %ld", myTime_2.tv_sec - myTime_1.tv_sec);

is this the accurate way or some other better way of doing this?
learningunixAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

coredatarecoveryCommented:
with eclipse, you may find a way in the settings to get your answer if you develop in eclipse.

Most compilers with gui have a calculated time involved.


One of the problems with geting an accurate time for running a procedure, besides calculating it's empathy, is system interrupts, stack calls, OS overhead all can break into the level of the operating system and throw off your values.

The best way to calculate efficiency is to look at the compiled code and calculate how many clock cycles each operand takes at the machine code level.
apart from that you have to rely on your compiler, not on a loop (Unless you have a BIG routine taking more than 1 second, the above code will not return much useful information)

0
phoffricCommented:
You can create a float by combining the seconds and the tv_sec and then subtract the two floats. This will give you a more accurate result. In an extreme example both times could be in the same second, and the difference is 0; yet the actual time might be 0.9 sec.
0
phoffricCommented:
I meant by combining tv_sec with tv_usec to get a float.
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

coredatarecoveryCommented:
But you still might gave system calls interrupt your youtube giving you different results.
0
learningunixAuthor Commented:
I'll try to add timer within the function instead of outside function, that way I don't have to worry about interrupt, stack etc.

Here's what I'll do:

 struct timeval myTime_1;
 gettimeofday(&myTime_1, NULL);
 double t1 = (double)myTime_1.tv_sec;
 t1 = (t1 * 1000 ) + (double)(myTime_1.tv_usec / 1000);

 sleep (3);

 struct timeval myTime_2;
 gettimeofday(&myTime_2, NULL);
 double t2 = (double)myTime_2.tv_sec;
 t2 = (t2 * 1000 ) + (double)(myTime_2.tv_usec / 1000);

 printf ("Total Time Taken = %f mSec\n", t2-t1);


This way it will give me difference in mSec which is more granular than sec
0
phoffricCommented:
There are different kinds of timing measurements.I took your code and added some clock() code as well.

See http://rabbit.eng.miami.edu/info/functions/time.html for a review of different timing mechanisms. Here are the results of the below program:
$ time ./a
Total Time Taken = 3125.000000 mSec
clock: 125.0000 mSec of processing

real    0m3.140s
user    0m0.155s
sys     0m0.016s

Open in new window

#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
#include <time.h>

int main() {
 struct timeval myTime_1;
 gettimeofday(&myTime_1, NULL);
 double t1 = (double)myTime_1.tv_sec;
 t1 = (t1 * 1000 ) + (double)(myTime_1.tv_usec / 1000);
 clock_t tc1=clock();

 sleep (3);

 clock_t tc2=clock();

 struct timeval myTime_2;
 gettimeofday(&myTime_2, NULL);
 double t2 = (double)myTime_2.tv_sec;
 t2 = (t2 * 1000 ) + (double)(myTime_2.tv_usec / 1000);

 printf ("Total Time Taken = %f mSec\n", t2-t1);
 printf("clock: %.4lf mSec of processing\n", 1000*(tc2-tc1)/(double)CLOCKS_PER_SEC);
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
learningunixAuthor Commented:
Interesting what does clock do?

so lets say if I have a block with some mutex lock which takes some time to acquire the lock.

so will tc2-tc1  be  
Total time for block - sleep(3) - time taken to acuire the Lock?


struct timeval myTime_1;
 gettimeofday(&myTime_1, NULL);
 double t1 = (double)myTime_1.tv_sec;
 t1 = (t1 * 1000 ) + (double)(myTime_1.tv_usec / 1000);
 clock_t tc1=clock();

 sleep (3);

 mutex_lock()
    //do something
 mutex_unlock()

 clock_t tc2=clock();

 struct timeval myTime_2;
 gettimeofday(&myTime_2, NULL);
 double t2 = (double)myTime_2.tv_sec;
 t2 = (t2 * 1000 ) + (double)(myTime_2.tv_usec / 1000);

 printf ("Total Time Taken = %f mSec\n", t2-t1);
 printf("clock: %.4lf mSec of processing\n", 1000*(tc2-tc1)/(double)CLOCKS_PER_SEC);
}

0
phoffricCommented:
>> Interesting what does clock do?
"The clock() function returns an approximation of processor time used by the program."
       http://linux.die.net/man/3/clock

     If you are running a single program, then clock gives a reasonable approximation of the processor time used by this program. If you have 10 other programs hogging the machine, then no matter how long your single program takes to complete, its processor time usage will remain about the same.

     So, during the 3 second sleep, your program was not running, and therefore it did not accumulate clock ticks.


>> "time taken to acuire the Lock"
     By this I understand you to mean, the time where the thread is dormant while waiting for the lock to become available (as opposed to the actual cpu time to handle the mutex kernel call).

>> so will tc2-tc1  be   Total time for block - sleep(3) - time taken to acuire the Lock?
    It depends. If the mutex is a shared mutex across two different programs, then yes. But, if you are talking about a mutex in a multi-threading environment, then for the OS that I've worked with, all the multi-threads spawned from a single thread are considered one program. Even if one thread is blocked to, say, increment a counter, then the clock() time to do this single operation could be a long time if other threads are running.

    I've confirmed this behavior on my Linux-like Cygwin system where I force two threads to run sequentially (and they both perform the same computational task). tc1 was established before creating the two threads. tc2 is defined in each thread after it completes the operation.

     The first thread reports about 7.5 seconds, and the 2nd reports about 15.0 seconds; even though the 2nd thread is waiting on the mutex for the first 7.5 seconds. (And adding a 3 second sleep in the thread, as you would expect, does not affect the clock() time outcome.)
0
learningunixAuthor Commented:
Thx
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.