user3025872
user3025872

Reputation: 191

How to measure CPU time

If I had the following code:

clock_t t;
t = clock();
//algorithm
t = clock() - t;

t would equal the number of ticks to run the program. Is this the same is CPU time?

How do I compare CPU time of two algorithms?

OS: Debian GNU/Linux

Upvotes: 19

Views: 40642

Answers (4)

user6547518
user6547518

Reputation:

#include <ctime>

std::clock_t c_start = std::clock();
// your_algorithm
std::clock_t c_end = std::clock();

long_double time_elapsed_ms = 1000.0 * (c_end-c_start) / CLOCKS_PER_SEC;
std::cout << "CPU time used: " 
          << time_elapsed_ms 
          << " ms\n";

Of course, if you display time in seconds :

std::cout << "CPU time used: " 
          << time_elapsed_ms / 1000.0 
          << " s\n";

Source : http://en.cppreference.com/w/cpp/chrono/c/clock

Upvotes: 11

radarist
radarist

Reputation: 41

clock() will measure CPU time on linux and wall time on windows. To measure the CPU time in windows, you can use the "processthreadsapi" :

#include <stdio.h>
#include <processthreadsapi.h>

double get_cpu_time(){
    FILETIME a,b,c,d;
    if (GetProcessTimes(GetCurrentProcess(),&a,&b,&c,&d) != 0){
        //  Returns total user time.
        //  Can be tweaked to include kernel times as well.
        return
            (double)(d.dwLowDateTime |
            ((unsigned long long)d.dwHighDateTime << 32)) * 0.0000001;
    }else{
        //  Handle error
        return 0;
    }
}

int main () {
    double begin = get_cpu_time();
    // Algorithm
    double end = get_cpu_time();
    double elapsed = (end - begin);

    return 0;
}

Upvotes: 0

Papa Smurf
Papa Smurf

Reputation: 21

A non-standard way to do this using pthreads is:

#include <pthread.h>
#include <time.h>

timespec cpu_time()
{
    thread_local bool initialized(false);
    thread_local clockid_t clock_id;

    if (!initialized)
    {
        pthread_getcpuclockid(pthread_self(), &clock_id);
        initialized = true;
    }

    timespec result;
    clock_gettime(clock_id, &result);
    return result;    
}

Addition: A more standard way is:

#include <sys/resource.h>
#include <stdio.h>

int main(int argc, char* argv[])
{
    struct rusage r;

    getrusage(RUSAGE_SELF, &r);

    printf("CPU usage: %d.%06d\n", r.ru_utime.tv_sec, r.ru_utime.tv_usec);
}

Upvotes: 2

bames53
bames53

Reputation: 88235

clock() is specified to measure CPU time however not all implementations do this. In particular Microsoft's implementation in VS does not count additional time when multiple threads are running, or count less time when the program's threads are sleeping/waiting.

Also note that clock() should measure the CPU time used by the entire program, so while CPU time used by multiple threads in //algorithm will be measured, other threads that are not part of //algorithm also get counted.

clock() is the only method specified in the standard to measure CPU time, however there are certainly other, platform specific, methods for measuring CPU time.

std::chrono does not include any clock for measuring CPU time. It only has a clock synchronized to the system time, a clock that advances at a steady rate with respect to real time, and a clock that is 'high resolution' but which does not necessarily measure CPU time.

Upvotes: 26

Related Questions