Search code examples
c++timer

C++ , Timer, Milliseconds


#include <iostream>
#include <conio.h>
#include <ctime>



using namespace std;

double diffclock(clock_t clock1,clock_t clock2)
{
    double diffticks=clock1-clock2;
    double diffms=(diffticks)/(CLOCKS_PER_SEC/1000);
    return diffms;
}
int main()
{
    clock_t start = clock();
    for(int i=0;;i++)
    {

    if(i==10000)break;
    }
    clock_t end = clock();

    cout << diffclock(start,end)<<endl;

    getch();
return 0;
}

So my problems comes to that it returns me a 0, well to be stright i want to check how much time my program does operate... I found tons of crap over the internet well mostly it comes to the same point of getting a 0 beacuse the start and the end is the same

This problems goes to C++ remeber : <


Solution

  • At a glance, it seems like you are subtracting the larger value from the smaller value. You call:

    diffclock( start, end );
    

    But then diffclock is defined as:

        double diffclock( clock_t clock1, clock_t clock2 ) {
    
            double diffticks = clock1 - clock2;
            double diffms    = diffticks / ( CLOCKS_PER_SEC / 1000 );
    
            return diffms;
        }
    

    Apart from that, it may have something to do with the way you are converting units. The use of 1000 to convert to milliseconds is different on this page:

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