Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here is what I use for c++ (not 11), but many libraries might have more elaborate solutions. For the code you need Qt but it could easily be done without. Might be you also need to replace CLOCK_MONOTONIC depending on your operating system.</p> <pre><code>#ifndef PROFILER_H #define PROFILER_H #include &lt;sys/time.h&gt; #include &lt;QString&gt; class Profiler { public: Profiler(QString const&amp; name); long measure() const; long measureNs() const; double measureMs() const; double measureS() const; void printNs() const; void printMs() const; void printS() const; private: QString mName; timespec mTime; }; #endif // PROFILER_H #include "profiler.h" #include &lt;QDebug&gt; #include &lt;assert.h&gt; #include &lt;iostream&gt; Profiler::Profiler(QString const&amp; name):mName(name){ clock_gettime(CLOCK_MONOTONIC, &amp;mTime); // Works on Linux } long int Profiler::measureNs() const{ timespec end; clock_gettime(CLOCK_MONOTONIC, &amp;end); // Works on Linux long int diff = (end.tv_sec-mTime.tv_sec) * 1000000000 + (end.tv_nsec - mTime.tv_nsec); assert(diff&gt;0); return diff; } double Profiler::measureMs() const{ return measureNs()/1000000.0; } double Profiler::measureS() const{ return measureMs()/1000.0; } void Profiler::printNs() const{ qDebug() &lt;&lt; mName &lt;&lt; "Time elapsed:" &lt;&lt; measureNs() &lt;&lt; "ns"; } void Profiler::printMs() const{ qDebug() &lt;&lt; mName &lt;&lt; "Time elapsed:" &lt;&lt; measureMs() &lt;&lt; "ms"; } void Profiler::printS() const{ qDebug() &lt;&lt; mName &lt;&lt; "Time elapsed:" &lt;&lt; measureS() &lt;&lt; "S"; } </code></pre> <p>Usage:</p> <pre><code>Profiler pro("Tag you want"); function(); pro.printMs(); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload