3

I am wanting to print the running time of my functions. For some reason my timer always returns 0. Can anyone tell me why?

double RunningTime(clock_t time1, clock_t time2)
{
    double t=time1 - time2;
    double time = (t*1000)/CLOCKS_PER_SEC;
    return time;
}

int main()
{
     clock_t start_time = clock();


     // some code.....


    clock_t end_time = clock();

    std::cout << "Time elapsed: " << double(RunningTime(end_time, start_time)) << " ms";

    return 0;
}

I attempted to use gettimeofday and it still returned 0.

double get_time()
{
    struct timeval t;
    gettimeofday(&t, NULL);
    double d = t.tv_sec + (double) t.tv_usec/100000;
    return d;
}

int main()
{
        double time_start = get_time();

        //Some code......

        double time_end = get_time();

        std::cout << time_end - time_start;

    return 0;
}

Also tried using chrono and it gave me all kinds of build errors:

  • error: #error This file requires compiler and library support for the upcoming ISO C++ standard, C++0x. This support is currently
    experimental, and must be enabled with the -std=c++0x or -std=gnu++0x compiler options.
  • warning: 'auto' will change meaning in C++0x; please remove it
  • error: ISO C++ forbids declaration of 't1' with no type error: 'std::chrono' has not been declared
  • error: request for member 'count' in '(t2 - t1)', which is of non-class type 'int'

    int main() { auto t1 = std::chrono::high_resolution_clock::now();

                //Some code......
    
                auto t2 = std::chrono::high_resolution_clock::now();
    
                std::cout << "Time elapsed: " << std::chrono::duration_cast<std::chrono::milliseconds>(t2-t1).count() << " milliseconds\n";
    
            return 0;
        }
    
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Jmh2013
  • 2,625
  • 2
  • 26
  • 42
  • 1
    Consider using `` if you want good resolution. You can easily specify milliseconds for the units instead of calculating it as well. – chris Jun 20 '12 at 00:10
  • On *nix systems, try `gettimeofday()` for high-resolution time (microseconds). – gavinb Jun 20 '12 at 00:26
  • If you don't have C++11, you can consider `clock_gettime` on Linux (using `CLOCK_MONOTONIC_HR`), or `gethrtime` for most other UNIX variants, and `QueryPerformanceCounter` on Windows. – jxh Jun 20 '12 at 00:27
  • @chris How would I implement `` ? I googled it real quick but can't quickly figure out how to put it in my program without causing build errors. I used `std::chrono::time_point time_point` – Jmh2013 Jun 20 '12 at 00:37
  • 1
    @Fourthmeal70, I see, you need the `-std=c++11` or `-std=c++0x` flag in your compiler options. – chris Jun 20 '12 at 01:32
  • @chris thanks for that. I edited my code above to show the changes I made. I don't think my compiler agrees with `std::chrono` or I'm still using it wrong. My attempt at `chrono` is the last code block. – Jmh2013 Jun 20 '12 at 01:32
  • time taken to execute a complete function http://stackoverflow.com/a/40380118/6180077 – Abdullah Farweez May 17 '17 at 05:04

3 Answers3

4

A timer tick is approximately equal to 1/CLOCKS_PER_SEC second, which is a millisecond resolution. To see a real (non-zero) number, you should either invoke a very long-time function or use another library with a higher time resolution facility:

  • new c++11x library chrono (use MSVS 2012)
  • boost::chrono (unfortunately, the library refers to a lot of others)
  • POSIX function gettimeofday, which gives you a 1 microsecond time resolution
Sergei Danielian
  • 4,938
  • 4
  • 36
  • 58
  • The weakness of `gettimeofday` is that it is not monotonic, and will generate strange results when something (like `ntpd`) adjusts the system time. – jxh Jun 20 '12 at 00:30
  • True, if a machine is attempting to keep synchronisation with a network time server, for example. But in this case it can be suited. – Sergei Danielian Jun 20 '12 at 00:33
  • Can you insert a 1 millisecond delay to your code and measure again? Or just repeat the code you want to measure for 100 times using loop, for example. I think your code performs very fast so you can't figure out how much time does it take to complete. – Sergei Danielian Jun 20 '12 at 00:51
  • What is the value of `timeval` structure? I mean both fields. – Sergei Danielian Jun 20 '12 at 00:56
  • I'm timing a sort function and used a file with 100,000 numbers and the timer says it took 0.312 ms. That seems short. I edited my code above to show all the ways I have tried it. – Jmh2013 Jun 20 '12 at 01:24
  • the values of `timeeval` : `tv_sec = 4686336` and `tv_usec = 4665580` – Jmh2013 Jun 20 '12 at 01:30
  • You were right. I was trying to time a function that was too fast. Thanks for your help. – Jmh2013 Jun 20 '12 at 01:52
0

After lots of trial and error I went with gettimeofday. Here is my code that I finally got to work properly.

double get_time()
{
    struct timeval t;
    gettimeofday(&t, NULL);
    double d = t.tv_sec + (double) t.tv_usec/1000000;
    return d;
}

int main()
{
    double time_start = get_time();

    //Some code.........

    double time_end = get_time();

    std::cout << time_end - time_start;

    return 0;
}
Jmh2013
  • 2,625
  • 2
  • 26
  • 42
0

A solution I have been using lately uses C++11's lambda functionality to time any arbitrary function call or series of actions.

#include <ctime>
#include <iostream>
#include <functional>

void timeit(std::function<void()> func) {
    std::clock_t start = std::clock();

    func();

    int ms = (std::clock() - start) / (double) (CLOCKS_PER_SEC / 1000);

    std::cout << "Finished in " << ms << "ms" << std::endl;
}

int main() {
    timeit([] {
        for (int i = 0; i < 10; ++i) {
            std::cout << "i = " << i << std::endl;
        } 
    });

    return 0;
}