user3794186
user3794186

Reputation: 709

C++ Getting more precise tickcount values

I am timing the performance of some operations in c++ using Tick Count:

DWORD tstart = GetTickCount();                  
{                                           
    //...Some Operation...
}                                           
DWORD tend = GetTickCount();                        
double tt = (double) (tend-tstart)/(double) 1000;   
std::cout << "Exec Time: " << tt << " Seconds" << std::endl;

However, this only gives me value >= 0.015...

Any value below 0.015 seems to be printed as simply 0..

Is there any other way to get more precision?

Upvotes: 0

Views: 1884

Answers (2)

pokey909
pokey909

Reputation: 1837

Use std::chrono::high_resolution_clock for highest resolution

typedef std::chrono::high_resolution_clock Clock;
auto start = Clock::now();
... do some work ...
auto stop = Clock::now();

std::cout << stop-start << '\n';

Upvotes: 1

NetVipeC
NetVipeC

Reputation: 4432

Using cout formatters.

include <iomanip>

std::cout << "Exec Time: " << std::setprecision(12) << tt << " Seconds" << std::endl;

http://www.cplusplus.com/reference/iomanip/setprecision/

I recommend also use of chrono new in C++11.

#include <chrono>
#include <iomanip>

std::chrono::system_clock::time_point begin_time = std::chrono::system_clock::now();

... work ...

std::chrono::system_clock::time_point end_time = std::chrono::system_clock::now();
long long elapsed_seconds = std::chrono::duration_cast<std::chrono::seconds>(end_time-begin_time).count();
std::cout << "Duration (min:seg): " << std::setw(2) << std::setfill('0') << (elapsed_seconds / 60) << ":" << std::setw(2) << std::setfill('0') << (elapsed_seconds % 60) << std::endl;

Upvotes: 0

Related Questions