SajithP
SajithP

Reputation: 587

Best C++11 way to measure code execution time for an Embedded system

I was trying to find a way to measure code execution time without using the below listed constraints.

In my requirement it's for an embedded system which has very strict conformances.

All I could found was using C headers, unapproved headers by Google C++ coding standards or boost which are excluded from the project conformances.

I looked through the suggested posts which looked similar but couldn't find an answer for what is looked for. Please help!

Constraints are not to use the following,

This style checker has list down chrono as unapproved.. https://github.com/google/styleguide/blob/gh-pages/cpplint/cpplint.py

Upvotes: 4

Views: 1308

Answers (4)

Charlie
Charlie

Reputation: 653

Something like this you mean? (for windows platform)

#include <Windows.h>

class stopwatch
{
    double PCFreq = 0.0;
    double CounterStart = 0; //google style compliant, less accurate
    //__int64 CounterStart = 0; //for accuracy
    LARGE_INTEGER li;

public:

    void StartCounter()
    {
        if (!QueryPerformanceFrequency(&li))ExitProcess(0);
        PCFreq = double(li.QuadPart) / 1000.0;
        QueryPerformanceCounter(&li); CounterStart = li.QuadPart;
    }

    double GetCounter() { QueryPerformanceCounter(&li); return double(li.QuadPart - CounterStart) / PCFreq; }
};

usage:

stopwatch aclock;
aclock.StartCounter(); //start
//....///
cout<<aclock.GetcCounter()<<endl; //output time passed in milliseconds

Upvotes: -1

CodeMonkey
CodeMonkey

Reputation: 4738

Is the time measurement necessary in the final product? If not I'd suggest using whatever you like and use a switch to not compile these measurement routines into the final product.

Upvotes: 2

Alexey Guseynov
Alexey Guseynov

Reputation: 5304

For embedded systems there is a common practice to change GPIO state in your code and then hook an oscilloscope to the pin and look for resulting waveform. This has minimal impact on runtime because changing GPIO is a cheap operation. It does not require any libraries and additional code. But it requires additional hardware.

Note: embedded is quite stretchable notion. GPIO trick is more related for microcontrollers.

Upvotes: 3

Starl1ght
Starl1ght

Reputation: 4493

If we are talking about C++11 - the only way is to use std::chrono. Google style guide is not an some kind of final authority here (sometimes it is highly questionable).

std::chrono is proven to be good and stable, and even used in game engines in AAA games, see for yourself HERE. Good example for exactly what you need is available HERE

In case if you still don't want it, there are no other C++11 way to do it, but you, probably, want to look on C-style measure, like HERE.

Just for your information - all methods are using system API, and so, <time.h> is included, no way to avoid it.

Upvotes: 9

Related Questions