Easily measure elapsed time

前端 未结 26 2050
栀梦
栀梦 2020-11-22 04:13

I am trying to use time() to measure various points of my program.

What I don\'t understand is why the values in the before and after are the same? I understand thi

相关标签:
26条回答
  • 2020-11-22 05:04

    The reason both values are the same is because your long procedure doesn't take that long - less than one second. You can try just adding a long loop (for (int i = 0; i < 100000000; i++) ; ) at the end of the function to make sure this is the issue, then we can go from there...

    In case the above turns out to be true, you will need to find a different system function (I understand you work on linux, so I can't help you with the function name) to measure time more accurately. I am sure there is a function simular to GetTickCount() in linux, you just need to find it.

    0 讨论(0)
  • 2020-11-22 05:05

    You can use SFML library, which is Simple and Fast Multimedia Library. It includes many useful and well-defined classes like Clock, Socket, Sound, Graphics, etc. It's so easy to use and highly recommended.

    This is an example for this question.

    sf::Clock clock;
    ...
    Time time1 = clock.getElapsedTime();
    ...
    Time time2 = clock.restart();
    
    0 讨论(0)
  • 2020-11-22 05:06

    I needed to measure the execution time of individual functions within a library. I didn't want to have to wrap every call of every function with a time measuring function because its ugly and deepens the call stack. I also didn't want to put timer code at the top and bottom of every function because it makes a mess when the function can exit early or throw exceptions for example. So what I ended up doing was making a timer that uses its own lifetime to measure time.

    In this way I can measure the wall-time a block of code took by just instantiating one of these objects at the beginning of the code block in question (function or any scope really) and then allowing the instances destructor to measure the time elapsed since construction when the instance goes out of scope. You can find the full example here but the struct is extremely simple:

    template <typename clock_t = std::chrono::steady_clock>
    struct scoped_timer {
      using duration_t = typename clock_t::duration;
      const std::function<void(const duration_t&)> callback;
      const std::chrono::time_point<clock_t> start;
    
      scoped_timer(const std::function<void(const duration_t&)>& finished_callback) :
          callback(finished_callback), start(clock_t::now()) { }
      scoped_timer(std::function<void(const duration_t&)>&& finished_callback) :
          callback(finished_callback), start(clock_t::now()) { }
      ~scoped_timer() { callback(clock_t::now() - start); }
    };
    

    The struct will call you back on the provided functor when it goes out of scope so you can do something with the timing information (print it or store it or whatever). If you need to do something even more complex you could even use std::bind with std::placeholders to callback functions with more arguments.

    Here's a quick example of using it:

    void test(bool should_throw) {
      scoped_timer<> t([](const scoped_timer<>::duration_t& elapsed) {
        auto e = std::chrono::duration_cast<std::chrono::duration<double, std::milli>>(elapsed).count();
        std::cout << "took " << e << "ms" << std::endl;
      });
    
      std::this_thread::sleep_for(std::chrono::seconds(1));
    
      if (should_throw)
        throw nullptr;
    
      std::this_thread::sleep_for(std::chrono::seconds(1));
    }
    

    If you want to be more deliberate, you can also use new and delete to explicitly start and stop the timer without relying on scoping to do it for you.

    0 讨论(0)
  • 2020-11-22 05:07
    #include <ctime>
    
    void f() {
      using namespace std;
      clock_t begin = clock();
    
      code_to_time();
    
      clock_t end = clock();
      double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
    }
    

    The time() function is only accurate to within a second, but there are CLOCKS_PER_SEC "clocks" within a second. This is an easy, portable measurement, even though it's over-simplified.

    0 讨论(0)
  • 2020-11-22 05:09

    The time(NULL) function call will return the number of seconds elapsed since epoc: January 1 1970. Perhaps what you mean to do is take the difference between two timestamps:

    size_t start = time(NULL);
    doSomthing();
    doSomthingLong();
    
    printf ("**MyProgram::time elapsed= %lds\n", time(NULL) - start);
    
    0 讨论(0)
  • 2020-11-22 05:11

    Matlab flavored!

    tic starts a stopwatch timer to measure performance. The function records the internal time at execution of the tic command. Display the elapsed time with the toc function.

    #include <iostream>
    #include <ctime>
    #include <thread>
    using namespace std;
    
    clock_t START_TIMER;
    
    clock_t tic()
    {
        return START_TIMER = clock();
    }
    
    void toc(clock_t start = START_TIMER)
    {
        cout
            << "Elapsed time: "
            << (clock() - start) / (double)CLOCKS_PER_SEC << "s"
            << endl;
    }
    
    int main()
    {
        tic();
        this_thread::sleep_for(2s);
        toc();
    
        return 0;
    }
    
    0 讨论(0)
提交回复
热议问题