It is currently Mon Nov 20, 2017 11:40 pm

All times are UTC - 5 hours




 Page 1 of 1 [ 3 posts ] 
Author Message
 Post subject: Bench Marking utility class.
PostPosted: Wed Apr 06, 2016 1:53 am 

Joined: Sat Aug 16, 2008 7:58 am
Posts: 449
I have discovered this class from here: http://code-blacksmith.com/benchmark-timer/ and think this would be a great incorporation into the ShaderEngine or any other project you maybe working on. I have slightly modified it in only that instead of printing directly to std::cout it is using the Logger class from the Shader Engine series. This is a header only, and instead of having to include this in every class or file you would like to use it in you can include this in the stdfax.h file right next to the ExceptionHandler.

Here is what it looks like:

#ifndef EXECUTION_TIMER_H
#define EXECUTION_TIMER_H

#include "Logger.h"

namespace vmk {

template<class Resolution = std::chrono::milliseconds>
class ExecutionTimer {
public:
   using Clock = std::conditional_t<std::chrono::high_resolution_clock::is_steady,
                                   std::chrono::high_resolution_clock,
                                   std::chrono::steady_clock>;
private:
   const Clock::time_point mStart = Clock::now();

public:
   ExecutionTimer() = default;
   ~ExecutionTimer() {
      const auto end = Clock::now();
      std::ostringstream strStream;
      strStream << "Elapsed: "
               << std::chrono::duration_cast<Resolution>( end - mStart ).count()
               << std::endl;
      Logger::log( strStream, Logger::TYPE_CONSOLE );
   }

}; // ExecutionTimer

} // namespace vmk

#endif // EXECUTION_TIMER_H


To use this is as simple as this:

void SomeClass::foo() {
    ExecutionTimer<> timer1; // milliseconds
    ExecutionTimer<std::chrono::nanoseconds> timer2.

    {
         // Code Block
    }

}


This will run for the length of the scope of this function.


Offline
 Profile  
 
 Post subject: Re: Bench Marking utility class.
PostPosted: Thu Apr 07, 2016 7:35 am 
Site Admin

Joined: Sun Feb 11, 2007 8:59 am
Posts: 1105
Location: Ontario Canada
nice find. Thanks for sharing it!


Offline
 Profile  
 
 Post subject: Re: Bench Marking utility class.
PostPosted: Thu Apr 07, 2016 12:14 pm 

Joined: Sat Aug 16, 2008 7:58 am
Posts: 449
I was looking for something that was simple to use and with a reusable design. Now you don't have to write the code to calculate the time before & after your code block. Also, this method seems to be accurate without any interference from any other processes running, and it isn't machine architecture independent.


Offline
 Profile  
 
Display posts from previous:  Sort by  
 Page 1 of 1 [ 3 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Jump to:  

cron