Why boost::timer gives me such strange results?
My working solution is to use wrapper about gettimeofday function from <time.h>, but I don't understand why boost::timer is not working for me here. What do I do wrong?
class Timer {
private:
timeval startTime;
public:
void start(){
gettimeofday(&startTime, NULL);
}
double stop(){
timeval endTime;
long seconds, useconds;
double duration;
gettimeofday(&endTime, NULL);
seconds = endTime.tv_sec - startTime.tv_sec;
useconds = endTime.tv_usec - startTime.tv_usec;
duration = seconds + useconds/1000000.0;
return duration;
}
long stop_useconds(){
timeval endTime;
long useconds;
gettimeofday(&endTime, NULL);
useconds = endTime.tv_usec - startTime.tv_usec;
return useconds;
}
static void printTime(double duration){
printf("%5.6f seconds\n", duration);
}
};
test:
//test
for (int i = 0; i < 10; i++) {
void *vp = malloc(1024*sizeof(int));
memset((int *)vp, 0, 1024);
void* itab = malloc(sizeof(int)*1024*256); //1MiB table
if (itab) {
memset ( (int*)itab, 0, 1024*256*sizeof (int) );
float elapsed;
boost::timer t;
Timer timer = Timer();
timer.start();
Munge64(itab, 1024*256);
double duration = timer.stop();
long lt = timer.stop_useconds();
timer.printTime(duration);
cout << t.elapsed() << endl;
elapsed = t.elapsed();
cout << ios::fixed << setprecision(10) << elapsed << endl;
cout << ios::fixed << setprecision(10) << t.elapsed() << endl;
printf("Munge8 elapsed:%ld useconds\n", lt);
elapsed = 0;
free(vp);
free(itab);
//printf("Munge8 elapsed:%d\n", elapsed);
}
}
results:
0.000100 seconds
0 << ??????????
40 << ????????????????
40 << ???????????????????????????????????
Munge8 elapsed:100 useconds
0.000100 seconds
0
40
40
Munge8 elapsed:100 useconds
0.000099 seconds
0
40
40
Munge8 elapsed:99 useconds
You should not use boost::timer - http://www.boost.org/doc/libs/1_54_0/libs/timer/doc/original_timer.html#Class timer
On POSIX like it measures CPU time - not wall clock time.
Consider using boost::chrono or std::chrono - you would want to look at steady_clock - as other clocks when implementing a timer if you want to isolate yourself from drift or shift in system wall clock. I expect on POSIX this will use clock_gettime on CLOCK_MONOTONIC.
Related
I would like to measure wallclock time taken by my algorithm in C++. Many articles point to this code.
clock_t begin_time, end_time;
begin_time = clock();
Algorithm();
end_time = clock();
cout << ((double)(end_time - begin_time)/CLOCKS_PER_SEC) << endl;
But this measures only cpu time taken by my algorithm.
Some other article pointed out this code.
double getUnixTime(void)
{
struct timespec tv;
if(clock_gettime(CLOCK_REALTIME, &tv) != 0) return 0;
return (tv.tv_sec + (tv.tv_nsec / 1000000000.0));
}
double begin_time, end_time;
begin_time = getUnixTime();
Algorithm();
end_time = getUnixTime();
cout << (double) (end_time - begin_time) << endl;
I thought it would print wallclock time taken by my algorithm. But surprisingly, the time printed by this code is much lower than cpu time printed by previous code. So, I am confused. Please provide code for printing wallclock time.
Those times are probably down in the noise. To get a reasonable time measurement, try executing your algorithm many times in a loop:
const int loops = 1000000;
double begin_time, end_time;
begin_time = getUnixTime();
for (int i = 0; i < loops; ++i)
Algorithm();
end_time = getUnixTime();
cout << (double) (end_time - begin_time) / loops << endl;
I'm getting approximately the same times in a single threaded program:
#include <time.h>
#include <stdio.h>
__attribute((noinline)) void nop(void){}
void loop(unsigned long Cnt) { for(unsigned long i=0; i<Cnt;i++) nop(); }
int main()
{
clock_t t0,t1;
struct timespec ts0,ts1;
t0=clock();
clock_gettime(CLOCK_REALTIME,&ts0);
loop(1000000000);
t1=clock();
clock_gettime(CLOCK_REALTIME,&ts1);
printf("clock-diff: %lu\n", (unsigned long)((t1 - t0)/CLOCKS_PER_SEC));
printf("clock_gettime-diff: %lu\n", (unsigned long)((ts1.tv_sec - ts0.tv_sec)));
}
//prints 2 and 3 or 2 and 2 on my system
But clocks manpage only describes it as returning an approximation. There's no indication that approximation is comparable to what clock_gettime returns.
Where I get drastically different results is where I throw in multiple threads:
#include <time.h>
#include <stdio.h>
#include <pthread.h>
__attribute((noinline)) void nop(void){}
void loop(unsigned long Cnt) {
for(unsigned long i=0; i<Cnt;i++) nop();
}
void *busy(void *A){ (void)A; for(;;) nop(); }
int main()
{
pthread_t ptids[4];
for(size_t i=0; i<sizeof(ptids)/sizeof(ptids[0]); i++)
pthread_create(&ptids[i], 0, busy, 0);
clock_t t0,t1;
struct timespec ts0,ts1;
t0=clock();
clock_gettime(CLOCK_REALTIME,&ts0);
loop(1000000000);
t1=clock();
clock_gettime(CLOCK_REALTIME,&ts1);
printf("clock-diff: %lu\n", (unsigned long)((t1 - t0)/CLOCKS_PER_SEC));
printf("clock_gettime-diff: %lu\n", (unsigned long)((ts1.tv_sec - ts0.tv_sec)));
}
//prints 18 and 4 on my 4-core linux system
That's because both musl and glibc on Linux use clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts) to implement clock() and the CLOCK_PROCESS_CPUTIME_ID nonstandard clock is described in the clock_gettime manpage as returning time for all process threads together.
In C# I am using the Stopwatch class. I can get the ticks, and milliseconds with no problems.
Now that I am testing code while learning C++ I try to get measurements but
I don't know where the results are that match the C# Stopwatch solution equivalent. I tried to search but the information is too broad and I couldn't find an absolute solution.
double PCFreq = 0.0;
__int64 CounterStart = 0;
void StartCounter()
{
LARGE_INTEGER li;
if(!QueryPerformanceFrequency(&li))
std::cout << "QueryPerformanceFrequency failed!\n";
PCFreq = double(li.QuadPart)/1000.0;
QueryPerformanceCounter(&li);
CounterStart = li.QuadPart;
}
double GetCounter()
{
LARGE_INTEGER li;
QueryPerformanceCounter(&li);
return double(li.QuadPart-CounterStart)/PCFreq;
}
As that gives me two different results, I tend to believe the clock. :)
start = StartCounter()
//some function or for loop
end = GetCounter()
marginPc = end - start;
start = clock();
// ...same
end= clock();
marginClck = end - start;
std::cout<< "Res Pc: " << marginPc << "\r\nRes Clck: " marginClck<< std::endl;
With the clock version I tried both unsigned int and double but the results were still different.
What is the proper method equivalent to the C# Stopwatch?
clock() gives you the number of milliseconds since the program started. For example, the following program will print a number close to 500:
int main()
{
Sleep(500);
cout << clock() << endl;
/*
POSIX version:
std::cout << clock() * 1000.0 / CLOCKS_PER_SEC << std::endl;
CLOCKS_PER_SEC is 1000 in Windows
*/
return 0;
}
QueryPerformanceCounter is sort of similar to GetTickCount64, it is based on the time when the computer started. When you do Stop-Watch type subtraction, the results are very close. QueryPerformanceCounter is more accurate. chrono method from #BoPersson's link is also based on QueryPerformanceCounter.
MSDN recommends using QueryPerformanceCounter (QPC) for high resolution stamps:
Acquiring high-resolution time stamps
The same QPC function is used in managed code:
For managed code, the System.Diagnostics.Stopwatch class uses
QPC as its precise time basis
This function should have reasonable accuracy:
long long getmicroseconds()
{
LARGE_INTEGER fq, t;
QueryPerformanceFrequency(&fq);
QueryPerformanceCounter(&t);
return 1000000 * t.QuadPart / fq.QuadPart;
}
The computer clock is usually accurate to +/-1 second per day.
From above link:
Duration Uncertainty
1 microsecond ± 10 picoseconds (10-12)
1 millisecond ± 10 nanoseconds (10-9)
1 second ± 10 microseconds
1 hour ± 60 microseconds
1 day ± 0.86 seconds
1 week ± 6.08 seconds
To simplify your other function, you can avoid double results. QuadPart is long long, so use that throughout the functions:
long long PCFreq = 0;
long long CounterStart = 0;
void StartCounter()
{
LARGE_INTEGER li;
QueryPerformanceFrequency(&li);
PCFreq = li.QuadPart;
QueryPerformanceCounter(&li);
CounterStart = li.QuadPart;
}
long long GetCounter()
{
if (PCFreq < 1) return 0;
LARGE_INTEGER li;
QueryPerformanceCounter(&li);
//for milliseconds: 1,000
return 1000 * (li.QuadPart - CounterStart) / PCFreq;
//for microseconds: 1,000,000
//return 1000000 * (li.QuadPart - CounterStart) / PCFreq;
}
Your bug is this. You have StartCounter return CounterStart = li.QuadPart;
But GetCounter returns double(li.QuadPart-CounterStart)/PCFreq.
I.e. one is divided by PCFreq and the other is not. It's not valid to then subtract one from the other.
I'm making a stopwatch, and I need to output the seconds out like so: "9.743 seconds".
I have the start time, the end time, and the difference measured out in clocks, and was planning on achieving the decimal by dividing the difference by 1000. However, no matter what I try, it will always output as a whole number. It's probably something small I'm overlooking, but I haven't a clue what.
Here's my code:
#include "Stopwatch.h"
#include <iostream>
#include <iomanip>
using namespace std;
Stopwatch::Stopwatch(){
clock_t startTime = 0;
clock_t endTime = 0;
clock_t elapsedTime = 0;
long miliseconds = 0;
}
void Stopwatch::Start(){
startTime = clock();
}
void Stopwatch::Stop(){
endTime = clock();
}
void Stopwatch::DisplayTimerInfo(){
long formattedSeconds;
setprecision(4);
seconds = (endTime - startTime) / CLOCKS_PER_SEC;
miliseconds = (endTime - startTime) / (CLOCKS_PER_SEC / 1000);
formattedSeconds = miliseconds / 1000;
cout << formattedSeconds << endl;
system("pause");
}
Like I said, the output is integer. Say it timed 5892 clocks: the output would be "5".
Division between integers is still an integer. Cast one of your division parameters to a real type (double or float) and assign to another variable that is a real type.
double elapsedSeconds = (endTime - startTime) / (double)(CLOCKS_PER_SEC);
cout << elapsedSeconds << endl;
formattedSeconds =(double) miliseconds / 1000;
it will give you real number output
I have app, where i must count time of executing part of C++ function and ASM function. Actually i have problem, times which i get are weird - 0 or about 15600. O ocurs more often. And sometimes, after executing, times looks good, and values are different than 0 and ~15600. Anybody knows why it occurs ? And how to fix it ?
Fragment of counting time for executing my app for C++:
auto start = chrono::system_clock::now();
for (int i = 0; i < nThreads; i++)
xThread[i]->Start(i);
for (int i = 0; i < nThreads; i++)
xThread[i]->Join();
auto elapsed = chrono::system_clock::now() - start;
long long milliseconds = chrono::duration_cast<std::chrono::microseconds>(elapsed).count();
cppTimer = milliseconds;
What you're seeing there is the resolution of your timer. Apparently, chrono::system_clock ticks every 1/64th of a second, or 15,625 microseconds, on your system.
Since you're in C++/CLI and have the .Net library available, I'd switch to using the Stopwatch class. It will generally have a much higher resolution than 1/64th of a second.
Looks good to me. Except for cast to std::chrono::microseconds and naming it milliseconds.
The snippet I have used for many months now is:
class benchmark {
private:
typedef std::chrono::high_resolution_clock clock;
typedef std::chrono::milliseconds milliseconds;
clock::time_point start;
public:
benchmark(bool startCounting = true) {
if(startCounting)
start = clock::now();
}
void reset() {
start = clock::now();
}
// in milliseconds
double elapsed() {
milliseconds ms = std::chrono::duration_cast<milliseconds>(clock::now() - start);
double elapsed_secs = ms.count() / 1000.0;
return elapsed_secs;
}
};
// usage
benchmark b;
...
cout << "took " << b.elapsed() << " ms" << endl;
I have this function prototype code for factorial calculation by iteration
How do I include a timer to produce total time spent for looping 100 times of the function?
for (unsigned long i=number; i>=1; i--) result *=i;
My C++ knowledge is barely basic, so not sure if "loop" is correctly mentioned here.
However, I was hinted to use .
Pls advice
thank you
Here's a proper C++11 version of some timing logic:
using namespace std;
using namespace chrono;
auto start_time = system_clock::now();
// your loop goes here:
for (unsigned long i=number; i>=1; i--) result *=i;
auto end_time = system_clock::now();
auto durationInMicroSeconds = duration_cast<microseconds>(end_time - start_time);
cout << "Looping " << number << " times took " << durationInMicroSeconds << "microseconds" << endl;
Just for sport, here's a simple RAII-based variation:
class Timer {
public:
explicit Timer(const string& name)
: name_(name)
, start_time_(system_clock::now()) {
}
~Timer() {
auto end_time = system_clock::now();
auto durationInMicroSeconds = duration_cast<microseconds>(end_time - start_time);
cout << "Timer: " << name << " took " << durationInMicroSeconds << "microseconds" << endl;
}
private:
string name_;
system_clock::time_point start_time_;
};
Sure, it's a bit more code, but once you have that, you can reuse it fairly efficiently:
{
Timer timer("loops");
// your loop goes here:
for (unsigned long i=number; i>=1; i--) result *=i;
}
If you looking for time spent in executing number of looping statements in the program code try making use of gettimeofday() as below,
#include <sys/time.h>
struct timeval tv1, tv2;
gettimeofday(&tv1, NULL);
/* Your loop code to execute here */
gettimeofday(&tv2, NULL);
printf("Time taken in execution = %f seconds\n",
(double) (tv2.tv_usec - tv1.tv_usec) / 1000000 +
(double) (tv2.tv_sec - tv1.tv_sec));
This solution is more towards C which can be employed in your case to calculate time spent.
This is a perfect situation for a lambda. Honestly I don't know the syntax in C++ but it should be something like this:
duration timer(function f) {
auto start = system_clock::now();
f();
return system_clock::now() - start;
}
To use it, you wrap your code in a lambda and pass it to the timer. The effect is very similar to #Martin J.'s code.
duration code_time = timer([] () {
// put any code that you want to time here
}
duration loop_time = timer([] () {
for (unsigned long i=number; i>=1; i--) {
result *=i;
}
}