c++ implementing clock to measure execution time - c++

I have written a program in c++ and am trying to measure the time it takes to execute completely
int main (int argc, char**argv){
clock_t tStart = clock();
//doing my program's work here
printf("Time taken: %.2fs\n", (double)(clock() - tStart)/CLOCKS_PER_SEC);
return 0;
}
My issue is that it will always print out 0.00s for the execution time. Could this be due to using multiple pthreads in my program (my program uses pthread_join to make sure that all threads have completed executing so I don't think this should be an issue)?
edit: //doing program's work =...
for(i = 0;i<4;i++){
err = pthread_create(&threads[i], NULL, print, NULL);
pthread_join(threads[i], NULL);
}
void *print(void *data){
printf("hello world");
}

printf("Time taken: %.2fs\n", (double)(clock() - tStart)/CLOCKS_PER_SEC);
All three of your arithmetic operands are integers, so you perform integer division and get 0.
Cast either the LHS or the RHS of the / symbol to a floating-point type. And run your code more times! Your benchmark is useless if it measures just a single run (which is pretty evident since you got 0, not 1 or like 300 or something).

It really depends on what is in //doing my program's work here. If it is kicking off other threads, then you will definitely need to wait or poll to get a time. Show the code to get more help! In a similar situation I was in recently, however, it turned out that my code was actually running in less than 0.01 seconds.

Related

how to write a measurement function for multithreaded function [duplicate]

I am running a .cpp code (i) in sequential style and (ii) using OpenMP statements. I am trying to see the time difference. For calculating time, I use this:
#include <time.h>
.....
main()
{
clock_t start, finish;
start = clock();
.
.
.
finish = clock();
processing time = (double(finish-start)/CLOCKS_PER_SEC);
}
The time is pretty accurate in sequential (above) run of the code. It takes about 8 seconds to run this. When I insert OpenMP statements in the code and thereafter calculate the time I get a reduction in time, but the time displayed is about 8-9 seconds on the console, when actually its just 3-4 seconds in real time!
Here is how my code looks abstractly:
#include <time.h>
.....
main()
{
clock_t start, finish;
start = clock();
.
.
#pragma omp parallel for
for( ... )
for( ... )
for (...)
{
...;
}
.
.
finish = clock();
processing time = (double(finish-start)/CLOCKS_PER_SEC);
}
When I run the above code, I get the reduction in time but the time displayed is not accurate in terms of real time. It seems to me as though the clock () function is calculating each thread's individual time and adding up them up and displaying them.
Can someone tell the reason for this or suggest me any other timing function to use to measure the time in OpenMP programs?
Thanks.
It seems to me as though the clock () function is calculating each thread's individual time and adding up them up and displaying them.
This is exactly what clock() does - it measures the CPU time used by the process, which at least on Linux and Mac OS X means the cumulative CPU time of all threads that have ever existed in the process since it was started.
Real-clock (a.k.a. wall-clock) timing of OpenMP applications should be done using the high resolution OpenMP timer call omp_get_wtime() which returns a double value of the number of seconds since an arbitrary point in the past. It is a portable function, e.g. exists in both Unix and Windows OpenMP run-times, unlike gettimeofday() which is Unix-only.
I've seen clock() reporting CPU time, instead of real time.
You could use
struct timeval start, end;
gettimeofday(&start, NULL);
// benchmark code
gettimeofday(&end, NULL);
delta = ((end.tv_sec - start.tv_sec) * 1000000u +
end.tv_usec - start.tv_usec) / 1.e6;
To time things instead
You could use the built in omp_get_wtime function in omp library itself. Following is an example code snippet to find out execution time.
#include <stdio.h>
#include <omp.h>
int main(){
double itime, ftime, exec_time;
itime = omp_get_wtime();
// Required code for which execution time needs to be computed
ftime = omp_get_wtime();
exec_time = ftime - itime;
printf("\n\nTime taken is %f", exec_time);
}
Well yes, that's what clock() is supposed to do, tell you how much processor time the program used.
If you want to find elapsed real time, instead of CPU time, use a function that returns wall clock time, such as gettimeofday().
#include "ctime"
std::time_t start, end;
long delta = 0;
start = std::time(NULL);
// do your code here
end = std::time(NULL);
delta = end - start;
// output delta

How to calculate compile time, execution time ,performance and success measure of a test?

I have c++ program in that i want to have compiletime, execution time ,performance measure and success measure of a test.Right now i am calculating time as follows:
clock_t starts = clock();
test_case();
clock_t ends = clock();
double time = (double)(ends - starts);
But i dont know wether "time" is compile time or execution time. If it is compile time then how will i get execution time or if it is execution time how will i get its compile time. Also, i need to have performance and success mesure of the "test_case()". So, suggest me how will i get it.
The time that you are calculating is the execution time. clock() returns the number of clock ticks since your program started. Hence taking the difference of starts and ends will give you execution time of test_case() in second multiplied by CLOCKS_PER_SEC. CLOCKS_PER_SEC is the number of clock ticks per second.
Compile time calculation can be done using template metaprogramming.
To get a view template metaprogramming, have a look at: Compile Time Calculation
If you are using UNIX, you can easily get compilation time using command like:
time g++ file_name.cpp
This will output the time required by g++ file_name.cpp to compile.
The above function outputs the execution time. I would prefer to use query performance counter for finding the execution time.
However, we can find the build time if we are using a VC++ compiler.
The option can be found at Tools->Options->VC++ProjectSettings->BuildTime
The time is execution time, because it get by the ends and starts.But the function clock() is not a good way for caculate codes' execution time, for its low accuracy(maybe ms).
I suggest you use c++ STL chrono, you will get more accurate output.Example:
int main()
{
chrono::high_resolution_clock::time_point begin_time = chrono::high_resolution_clock::now();
for(int i=0;i<100000;i++)
{
//do something
;
}
chrono:: high_resolution_clock::time_point stop_time = chrono::high_resolution_clock::now();
chrono::duration<double> slapsed = duration_cast<duration<double>>(stop_time - begin_time);
cout << "time takes " << slapsed.count() * 1000 << "ms" << endl;
return 0;
}
I hope this can help you.

Measure execution time in C++ OpenMP code

I am running a .cpp code (i) in sequential style and (ii) using OpenMP statements. I am trying to see the time difference. For calculating time, I use this:
#include <time.h>
.....
main()
{
clock_t start, finish;
start = clock();
.
.
.
finish = clock();
processing time = (double(finish-start)/CLOCKS_PER_SEC);
}
The time is pretty accurate in sequential (above) run of the code. It takes about 8 seconds to run this. When I insert OpenMP statements in the code and thereafter calculate the time I get a reduction in time, but the time displayed is about 8-9 seconds on the console, when actually its just 3-4 seconds in real time!
Here is how my code looks abstractly:
#include <time.h>
.....
main()
{
clock_t start, finish;
start = clock();
.
.
#pragma omp parallel for
for( ... )
for( ... )
for (...)
{
...;
}
.
.
finish = clock();
processing time = (double(finish-start)/CLOCKS_PER_SEC);
}
When I run the above code, I get the reduction in time but the time displayed is not accurate in terms of real time. It seems to me as though the clock () function is calculating each thread's individual time and adding up them up and displaying them.
Can someone tell the reason for this or suggest me any other timing function to use to measure the time in OpenMP programs?
Thanks.
It seems to me as though the clock () function is calculating each thread's individual time and adding up them up and displaying them.
This is exactly what clock() does - it measures the CPU time used by the process, which at least on Linux and Mac OS X means the cumulative CPU time of all threads that have ever existed in the process since it was started.
Real-clock (a.k.a. wall-clock) timing of OpenMP applications should be done using the high resolution OpenMP timer call omp_get_wtime() which returns a double value of the number of seconds since an arbitrary point in the past. It is a portable function, e.g. exists in both Unix and Windows OpenMP run-times, unlike gettimeofday() which is Unix-only.
I've seen clock() reporting CPU time, instead of real time.
You could use
struct timeval start, end;
gettimeofday(&start, NULL);
// benchmark code
gettimeofday(&end, NULL);
delta = ((end.tv_sec - start.tv_sec) * 1000000u +
end.tv_usec - start.tv_usec) / 1.e6;
To time things instead
You could use the built in omp_get_wtime function in omp library itself. Following is an example code snippet to find out execution time.
#include <stdio.h>
#include <omp.h>
int main(){
double itime, ftime, exec_time;
itime = omp_get_wtime();
// Required code for which execution time needs to be computed
ftime = omp_get_wtime();
exec_time = ftime - itime;
printf("\n\nTime taken is %f", exec_time);
}
Well yes, that's what clock() is supposed to do, tell you how much processor time the program used.
If you want to find elapsed real time, instead of CPU time, use a function that returns wall clock time, such as gettimeofday().
#include "ctime"
std::time_t start, end;
long delta = 0;
start = std::time(NULL);
// do your code here
end = std::time(NULL);
delta = end - start;
// output delta

pthread_join is being a bottleneck

I have an application where pthread_join is being the bottleneck. I need help to resolve this problem.
void *calc_corr(void *t) {
begin = clock();
// do work
end = clock();
duration = (double) (1000*((double)end - (double)begin)/CLOCKS_PER_SEC);
cout << "Time is "<<duration<<"\t"<<h<<endl;
pthread_exit(NULL);
}
int main() {
start_t = clock();
for (ii=0; ii<16; ii++)
pthread_create(&threads.p[ii], NULL, &calc_corr, (void *)ii);
for (i=0; i<16; i++)
pthread_join(threads.p[15-i], NULL);
stop_t = clock();
duration2 = (double) (1000*((double)stop_t - (double)start_t)/CLOCKS_PER_SEC);
cout << "\n Time is "<<duration2<<"\t"<<endl;
return 0;
}
The time printed in the thread function is in the range of 40ms - 60ms where as the time printed in the main function is in the 650ms - 670ms. The irony is, my serial code runs in 650ms - 670ms time. what can I do to reduce the time taken by pthread_join?
Thanks in advance!
On Linux, clock() measures the combined CPU time. It does not measure the wall time.
This is explains why you get ~640 ms = 16 * 40ms. (as pointed out in the comments)
To measure wall time, you should be using something like:
gettimeofday()
clock_gettime()
By creating some threads you are adding an overhead to your system: Creation time, scheduling time. Creating a thread require allocating the stack, etc; scheduling means more context switching. Also, pthread_join suspends execution of the calling thread until the target thread terminates. Which means you want for thread 1 to finish, when he does you are rescheduled as quick as possible but not instantly, then you wait for thread 2, etc...
Now your computer has few cores, like one or 2, and you are creating 16 threads. At best 2 threads of your program will run at the same time and just by adding their clock measurements you have something around 400 ms.
Again It depends on lot of things, so I quickly flown over what is happening.

Benchmarking an application in a fully loaded machine

I need to "time" or benchmark a number crunching application written in C/C++. The problem is that the machine where I run the program is usually full of people doing similar things, so the CPUs are always at full load.
I thought about using functions from time.h liket "get time of the day" (don't remember the exact syntax, sorry) and similars, but I am afraid they would not be good for this case, am I right?
And the program "time" from bash, gave me some errors long time ago.
Also the problem is, that sometimes I need to get timings in the range of 0.5 secs and so on.
Anybody has a hint?
P.S.: compiler is gcc and in some cases nvcc (NVIDIA)
P.S.2: in my benchmarks I just want to measure the execution time between two parts of the main function
You didn't mention which compiler you are using, but with GNU's g++ I usually set the -pg flag to build with profiling informations.
Each time you run the application, it will create an output file that, parsed with gprof application, will give you lots of information about the performances.
See this for starters.
From your other recent questions, you seem to be using MPI for parallelisation. Assuming this question is within the same context, then the simplest way to time your application would be to use MPI_Wtime().
From the man page:
This subroutine returns the current
value of time as a double precision
floating point number of seconds. This
value represents elapsed time since
some point in the past. This time in
the past will not change during the
life of the task. You are responsible
for converting the number of seconds
into other units if you prefer.
Example usage:
#include "mpi.h"
int main(int argc, char **argv)
{
int rc, taskid;
double t_start, t_end;
MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD,&taskid);
t_start = MPI_Wtime();
/* .... your computation kernel .... */
t_end = MPI_Wtime();
/* make sure all processes have completed */
MPI_Barrier(MPI_COMM_WORLD);
if (taskid == 0) {
printf("Elapsed time: %1.2f seconds\n", t_start - t_end);
}
MPI_Finalize();
return 0;
}
The advantage of this is that we let the underlying MPI library handle platform specific ways of handling time, although you might want to use MPI_Wtick() to determine the resolution of the timer used on each platform.
It's hard to meaningfully compare timings from programs running for such a short time. Usually the solution is to run multiple times.
The time builtin in bash (or /usr/bin/time) will report time actually used by the processor, which will be more useful on a loaded machine than wall-clock time, but there is too much going on to really compare timings on a fine grain – huge differences of orders of magnitude will still be apparent.
You can also use clock to get a rough estimate:
#include <ctime>
#include <iostream>
struct Timer {
std::clock_t _start, _stop;
Timer() : _start(std::clock()) {}
void restart() { _start = std::clock(); }
void stop() { _stop = std::clock(); }
std::clock_t clocks() const { return _stop - _start; }
double secs() const { return double(clocks()) / CLOCKS_PER_SEC; }
};
int main() {
Timer t;
//run_some_code();
t.stop();
std::cout << "That took " << t.secs() << " seconds.\n";
return 0;
}