Increasing Percision of Timer Functions

Hello good people,

I was just wondering if anyone had any advice as to how I can increase the precision of my timer functions? I simply call start timer before an arbitrary function I want timed, and then call stop timer.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <time.h>

using namespace std;

class Battle{
public:
    void StartTimer();
    void StopTimer();
    //some arbitrary function
private:
    clock_t start;
    clock_t stop;
    double elapsed;
};
void Battle::StartTimer(){
    start=clock();
}

void Battle::StopTimer(){
    stop=clock();
    elapsed = ((double) (stop - start) * 1000) / CLOCKS_PER_SEC;
        cout<<"ELAPSED TIME: "<<elapsed<<endl;
    start=0;
    stop=0;
    elapsed=0;
}


It seems that my precision is limited to milliseconds with time.h and the clock() (or is it?):
elapsed = ((double) (stop - start) * 1000) / CLOCKS_PER_SEC;
Last edited on
I now understand that timer resolution is dependant on hardware/OS. Sorry for the silly question.
Topic archived. No new replies allowed.