runner frank
runner frank

Reputation: 341

how to get clock ticks for a C++ code block on Linux ? clock() results make no sense.

I am trying to test clock ticks of pow().

clock_t startTime = clock();
for (int ii = 0 ; ii < moreIter ; ++ii)
{
   for (int i = 0 ; i < totalIte ; ++i)
   P0Table[i] = pow( 1 + baseTable[i] + baseTable1[i]  , expTablr[i]);
}
clock_t endTime = clock();
clock_t powTime = endTime - startTime;

If moreIter is 1 and totalIte is 5000 , the powTime is always 0.

I just followed : http://www.cplusplus.com/reference/clibrary/ctime/clock/

why ?

Any help will be appreciated.

Upvotes: 0

Views: 3711

Answers (2)

moswald
moswald

Reputation: 11677

Try using the C++11 <chrono> header.

typedef std::chrono::high_resolution_clock Clock;

auto start = Clock::now();
/* do work */
auto end = Clock::now();

auto timeTaken = end - start;

Upvotes: 3

janneb
janneb

Reputation: 37208

The clock() resolution isn't particularly high, you may need more iterations to get a non-zero result.

On Linux, cpu time is iirc accounted depending on the CONFIG_HZ setting, usually 100-1000 Hz these days. So 1-10 ms resolution is the best you can get.

With clock_gettime(..., CLOCK_MONOTONIC) you can get nanosecond resolution wallclock time, though of course at that kind of resolution you might want to take into account that the call itself takes a bit of time (IIRC around 20 ns last I checked).

Upvotes: 2

Related Questions