4

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

SajithP
  • 592
  • 8
  • 19
  • What about ``? – Gonmator Nov 23 '16 at 07:22
  • 1
    Sorry, I am very lazy to read all the Google C++ style. In which part says `` is not approved? Or where says the standard approved header files to use? – Gonmator Nov 23 '16 at 07:25
  • About my previous comment, I guess because `` depends on `` – Gonmator Nov 23 '16 at 07:29
  • 3
    Why can't you use `` which is in standard C++11? Notice that Google style guide is not for embedded systems, so I guess you'll need to adapt.., and you certainly should not follow it blindly. If you cannot discuss with those imposing such constraints, you probably are in trouble (but the issue is then social or political or managerial, not technical). – Basile Starynkevitch Nov 23 '16 at 07:55
  • Sorry, if I mislead you by not adding the exact link for where it says is not approved. https://github.com/google/styleguide/blob/gh-pages/cpplint/cpplint.py – SajithP Nov 23 '16 at 10:00
  • [reason Google Style Guide “ is an unapproved C++11 header”](http://stackoverflow.com/q/33653326/995714). But in this case [MISRA](https://en.wikipedia.org/wiki/MISRA_C) would be better because it was designed for robustness, safety, security, portability and reliability of embedded systems – phuclv Nov 23 '16 at 15:20
  • Any way every code checker tools has some debatable rules. Even MISRA is so strict that sometimes people find it hard to fix the warning. Google Style guide is no way the best. [Why Google Style Guide for C++ is a deal-breaker](https://www.linkedin.com/pulse/20140503193653-3046051-why-google-style-guide-for-c-is-a-deal-breaker) – phuclv Nov 23 '16 at 15:22

4 Answers4

9

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.

Community
  • 1
  • 1
Starl1ght
  • 4,422
  • 1
  • 21
  • 49
  • Still have to #include right? You refer to the same in the below link? http://en.cppreference.com/w/cpp/chrono – SajithP Nov 23 '16 at 10:07
  • @SajithP yes. This is C++11-way. – Starl1ght Nov 23 '16 at 10:09
  • Unfortunately cannot include as it's listed as unapproved by google c++ standards. https://github.com/google/styleguide/blob/gh-pages/cpplint/cpplint.py Could find a link why it's not approved.. http://stackoverflow.com/questions/33653326/google-style-guide-chrono-is-an-unapproved-c11-header – SajithP Nov 23 '16 at 10:16
  • 2
    @SajithP this is not approved, because they have their own Time library in chromium, which is not your case. As I said earlier - google style guide is based on some internal google stuff. It is, definately, NOT the some common guide. It's good as starting point, but their reasoning about chrono is not applicable for anyone, except google themselves. – Starl1ght Nov 23 '16 at 10:18
  • Yeah it seems minor. I will show this post and ask to remove that constraint from the project :) – SajithP Nov 23 '16 at 10:21
  • @SajithP, please accept answer, since your problem is solved ;) – Starl1ght Nov 23 '16 at 10:26
  • Given an upvote for the clarifications. But I have mentioned that I need a way without . Will do if I could convince my team :) – SajithP Nov 23 '16 at 10:36
3

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.

Alexey Guseynov
  • 5,116
  • 1
  • 19
  • 29
  • I've used this successfully in optimizing some routines. Usually the hardware designers leave open a GPIO or two for such testing purposes. – CodeMonkey Nov 23 '16 at 07:45
2

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.

CodeMonkey
  • 4,067
  • 1
  • 31
  • 43
  • It's not needed in the final product, but I want to go it to the testers via the official releasing mechanism which has these check everywhere till the test release. It should be bench marked in the testing environments. – SajithP Nov 23 '16 at 10:02
  • What kind of operating system are you on? Is there any tick counter you can use to roll your own time measurement system? – CodeMonkey Nov 23 '16 at 11:07
-1

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
Charlie
  • 585
  • 9
  • 17
  • 2
    How do you know that the target platform is Windows ? – Gonmator Nov 23 '16 at 07:27
  • I am afraid this is not a valid answer because "Nonstandard extensions" (`__int64`) is not allowed in Google C++ Style Guide. – Gonmator Nov 23 '16 at 07:32
  • are you certain? https://msdn.microsoft.com/en-us/library/29dh1w7z.aspx – Charlie Nov 23 '16 at 07:34
  • 1
    Yes, I am: "Microsoft Specific" -- "END Microsoft Specific". They are extensions for Microsoft compilers, no standard types. If you want to use standard 64 bits integer, use `int64_t` from ``. – Gonmator Nov 23 '16 at 07:38
  • how many embedded systems do actually use Windows? And please read the `Microsoft Specific` text at the begin of the page – phuclv Nov 23 '16 at 08:03