linux - [Solved-5 Solutions] Easily measure elapsed time in Linux - ubuntu - red hat - debian - linux server - linux pc



Linux - Problem :

To use time() to measure various points of your program.

printf("**MyProgram::before time= %ld\n", time(NULL));

doSomthing();
doSomthingLong();

printf("**MyProgram::after time= %ld\n", time(NULL));
click below button to copy the code. By - Linux tutorial - team

For Example:

struct timeval diff, startTV, endTV;

gettimeofday(&startTV, NULL); 

doSomething();
doSomethingLong();

gettimeofday(&endTV, NULL); 

timersub(&endTV, &startTV, &diff);

printf("**time taken = %ld %ld\n", diff.tv_sec, diff.tv_usec);
click below button to copy the code. By - Linux tutorial - team

How to read a result of **time taken = 0 26339? Does that mean 26,339 nanoseconds = 26.3 msec?

What about **time taken = 4 45025, does that mean 4 seconds and 25 msec?

Linux - Solution 1:

#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;
}
click below button to copy the code. By - Linux tutorial - team

The time() function is only accurate to within a second, but there are CLOCKS_PER_SEC "clocks" within a second.

Linux - Solution 2:

You can abstract the time measuring mechanism and have each callable's run time measured with minimal extra code, just by being called through a timer structure. At compile time you can parametrize the timing type (milliseconds, nanoseconds etc).

#include <iostream>
#include <chrono>

template<typename TimeT = std::chrono::milliseconds>
struct measure
{
    template<typename F, typename ...Args>
    static typename TimeT::rep execution(F&& func, Args&&... args)
    {
        auto start = std::chrono::steady_clock::now();
        std::forward<decltype(func)>(func)(std::forward<Args>(args)...);
        auto duration = std::chrono::duration_cast< TimeT> 
                            (std::chrono::steady_clock::now() - start);
        return duration.count();
    }
};

int main() {
    std::cout << measure<>::execution(functor(dummy)) << std::endl;
}
click below button to copy the code. By - Linux tutorial - team

The above class could give the user the choice to call count manually by providing an extra static method

template<typename F, typename ...Args>
static auto duration(F&& func, Args&&... args)
{
    auto start = std::chrono::steady_clock::now();
    std::forward<decltype(func)>(func)(std::forward<Args>(args)...);
    return std::chrono::duration_cast<TimeT>(std::chrono::steady_clock::now()-start);
} 

// call .count() manually later when needed (eg IO)
auto avg = (measure<>::duration(func) + measure<>::duration(func)) / 2.0;
click below button to copy the code. By - Linux tutorial - team

The most useful for clients that

"want to post-process a bunch of durations prior to I/O (e.g. average)"

If C++17's std::invoke is available, the invocation of the callable in execution could be done like this :

invoke(forward<decltype(func)>(func), forward<Args>(args)...);
click below button to copy the code. By - Linux tutorial - team

To provide for callables that are pointers to member functions.

Linux - Solution 3:

//***C++11 Style:***
std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
std::chrono::steady_clock::time_point end= std::chrono::steady_clock::now();

std::cout << "Time difference = " << std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count() <<std::endl;
std::cout << "Time difference = " << std::chrono::duration_cast<std::chrono::nanoseconds> (end - begin).count() <<std::endl;
click below button to copy the code. By - Linux tutorial - team

Linux - Solution 4:

#include <time.h>
#include <stdio.h>

time_t start,end;
time (&start);
.
.
.
<your code>
.
.
.
time (&end);
double dif = difftime (end,start);
printf ("Elasped time is %.2lf seconds.", dif );
click below button to copy the code. By - Linux tutorial - team

Linux - Solution 5:

Windows only:

You can use GetTickCount() to get the number of milliseconds that have elapsed since the system was started.

long int before = GetTickCount();

// Perform time-consuming operation

long int after = GetTickCount();
click below button to copy the code. By - Linux tutorial - team

Related Searches to - linux - linux tutorial - Easily measure elapsed time in Linux