How to measure CPU time
Asked Answered
M

4

19

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

Mihe answered 23/11, 2013 at 20:47 Comment(0)
L
25

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.

Lunseth answered 23/11, 2013 at 20:51 Comment(0)
B
11
#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

Beecham answered 5/5, 2017 at 8:53 Comment(0)
S
2

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);
}
Some answered 2/5, 2022 at 13:13 Comment(0)
H
0

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;
}
Hoffert answered 4/12, 2023 at 13:8 Comment(2)
clock() will measure CPU time on linux and wall time on windows Can you give an authoritative citation that this is what the function does on Windows?Equestrian
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Bezanson

© 2022 - 2024 — McMap. All rights reserved.