MarekKnows Discussion Forum
http://marekknows.com/phpBB3/

Bench Marking utility class.
http://marekknows.com/phpBB3/viewtopic.php?f=12&t=927
Page 1 of 1

Author:  skilz80 [ Wed Apr 06, 2016 1:53 am ]
Post subject:  Bench Marking utility class.

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.

Author:  Marek [ Thu Apr 07, 2016 7:35 am ]
Post subject:  Re: Bench Marking utility class.

nice find. Thanks for sharing it!

Author:  skilz80 [ Thu Apr 07, 2016 12:14 pm ]
Post subject:  Re: Bench Marking utility class.

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.

Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/