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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.