I have to calculate speed of my algorithm in base of milliseconds. In C++/C , how can i do this? I need to write smth before input and after output but what exactly??
-
Why do you need absolute numbers? Are you going to run it on different computers to compare? – Eugen Martynov Jul 23 '12 at 08:39
-
possible duplicate of [Time difference in C++](http://stackoverflow.com/questions/307596/time-difference-in-c) – jogojapan Jul 23 '12 at 08:39
-
1I have two different solutions of a problem. So they have exactly same Big-O value but in implementation it must be different. So I want to compare them in one computer and different times. – Yilmaz Paçariz Jul 23 '12 at 08:43
4 Answers
You could use clock()
function from <time.h>
clock()
shows how many ticks have passed since your program started. The macro CLOCKS_PER_SEC
contains the number of ticks per second, so you can actually get time.
//We start measuring here. Remember what was the amount of ticks in the
//beginning of the part of code you want to test:
int start = clock();
//<...>
//Do your stuff here
//<...>
int end = clock();//Now check what amount of ticks we have now.
//To get the time, just subtract start from end, and divide by CLOCKS_PER_SEC.
std::cout << "it took " << end - start << "ticks, or " << ((float)end - start)/CLOCKS_PER_SEC << "seconds." << std::endl;

- 29,228
- 8
- 68
- 105
-
2This is probably system-dependent, but on Linux, I find that clock() is very coarse-grained (inaccurate), so I avoid using it in preference to other system functions, such as times() from
– Oleg2718281828 Jul 23 '12 at 09:01 -
Not to forget the invocation of clock itself will consume some millis, so you won't get exact millis, so time taken by your algo = output_millis - invocation_clock_millis(). The invocation_clock_millis() is system dependent. – Mohan Jul 23 '12 at 09:57
-
And you also need to take clock skew, for example if the sytem is loaded (either by CPU or by network), then the system clock might go up/down w.r.t base clock. But this will be mostly 1 to 3 ms, so not to worry much. – Mohan Jul 23 '12 at 09:59
There is no general way to measure the exact time or ticks. The method of measurement, the operating system and the other things happening on your computer (other application, graphical output, background processes) will influence the result. There is different ways to do "good-enough" (in many cases) measurements:
library functions
clock(...), clock_gettime(...)
from the standard lib (in time.h
) and
gettimeofday(..) // for elapsed (wallclock) time
times(..) // process times
for linux and other unix systems (in sys/time.h
) (edited according to Oleg's comment)
hardware counter:
__inline__ uint64_t rdtsc(void) { uint32_t lo, hi; __asm__ __volatile__( // serialize "xorl %%eax,%%eax \n cpuid":::"%rax", "%rbx", "%rcx", "%rdx"); __asm__ __volatile__("rdtsc":"=a"(lo), "=d"(hi)); return (uint64_t) hi << 32 | lo; } /*...*/ uint64_t t0 = rdtsc(); code_to_be_tested(); uint64_t t1 = rdtsc();
I prefer this method as it reads directly the hardware counter.
for C++11:
std:chrono::highresolution_clock
typedef std::chrono::high_resolution_clock Clock; auto t0 = Clock::now(); code_to_be_tested(); auto t1 = Clock::now();
Keep in mind, that the measurements will not be exact to the clockcycle. i.e. nanosecond. I always calculate microseconds (10e-6 s) as the smallest reasonable time unit.

- 8,572
- 11
- 52
- 90
-
Even if you are not using windows, here is [a good read about rdtsc](http://msdn.microsoft.com/en-us/library/windows/desktop/ee417693%28v=vs.85%29.aspx). – Jesse Good Jul 23 '12 at 09:10
-
@JesseGood: Thanks for the link! Very interesting. Are you aware of better ways in *ix systems? – steffen Jul 23 '12 at 09:17
-
`gettimeofday`, and I'm assuming the hardware counter, will give you the wall time, which because of the OS multitasking, may not be what you want. If you want the user time on Linux, I suggest `times()` from `
` – Oleg2718281828 Jul 23 '12 at 09:17
Note that you can use date and time utilities from C++11 chrono library. From cppreference.com:
The chrono library defines three main types (durations, clocks, and time points) as well as utility functions and common typedefs.
See the sample from the article compiled in GCC 4.5.1 here

- 1,814
- 25
- 17
You can use this function library :
// clock.c
#include <time.h>
#include "clock.h"
struct clock { clock_t c1, c2; };
void start(clock *this) { this->c1 = clock(); }
void stop (clock *this) { this->c2 = clock(); }
double print(clock *this) { return (double)(c1 - c2) / CLOCKS_PER_SEC; }
// clock.h
#ifndef CLOCK_H_INCLUDED
# define CLOCK_H_INCLUDED
typedef struct clock clock;
extern void start(clock *);
extern void stop (clock *);
extern double print(clock *);
#endif // CLOCK_H_INCLUDED
But sometimes clock
isn't very adapted: you can use your system functions, which can be more accurate.