-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
+ lib/Profiler.{h,cpp} + examples/simple.cpp
- Loading branch information
1 parent
7a3c9a8
commit d2d3031
Showing
7 changed files
with
215 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
cmake_minimum_required( VERSION 3.0 ) | ||
project( Profiler ) | ||
|
||
if ( MSVC ) | ||
add_compile_options( "/std:c++14" ) | ||
else() | ||
set( CMAKE_CXX_STANDARD 14) | ||
set( CMAKE_CXX_STANDARD_REQUIRED ON ) | ||
add_compile_options( -std=c++14 ) | ||
endif() | ||
|
||
option( BUILD_EXAMPLES OFF ) | ||
|
||
add_subdirectory( lib ) | ||
|
||
if ( ${BUILD_EXAMPLES} ) | ||
add_subdirectory( examples ) | ||
endif() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Profiler | ||
|
||
This two files library is designed for simple profiling. | ||
|
||
To use it just copy files from folder **lib/** to your project. | ||
|
||
You can find usage examples in examples/simple.cpp. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
add_executable( simple simple.cpp ) | ||
target_link_libraries( simple profiler ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#include <sstream> | ||
|
||
#include "Profiler.h" | ||
|
||
|
||
using namespace alt; | ||
|
||
|
||
int main( int argc, char** argv ) | ||
{ | ||
Profiler profMain( "total time", true ); | ||
|
||
{ // block-1 | ||
Profiler profBlock1( "block-1", true ); | ||
|
||
for ( int i = 0; i < 1000000; ++i ) // 1M | ||
int n = i * i - i / 3; | ||
} | ||
|
||
{ // block-2 | ||
Profiler profBlock1( "block-2", true ); | ||
|
||
for ( int i = 0; i < 1000000; ++i ) // 1M | ||
int n = i / 3 + i * 10 - i / 5 + 19; | ||
} | ||
|
||
for ( int k = 0; k < 19; ++k ) | ||
{ | ||
Profiler profOp( "operation" ); | ||
|
||
for ( int i = 0; i < 1000000; ++i ) // 1M | ||
int n = i + i * i; | ||
} | ||
|
||
Profiler::printStatistics(); | ||
|
||
std::ostringstream oss; | ||
Profiler::printStatistics( oss ); | ||
std::cout << "Statistics from stream\n" << oss.str() << std::endl; | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
set( lib profiler ) | ||
|
||
set( CMAKE_INCLUDE_CURRENT_DIR ON ) | ||
|
||
set( HEADERS_ROOT "${CMAKE_CURRENT_SOURCE_DIR}" ) | ||
|
||
set( HDRS | ||
${HEADERS_ROOT}/Profiler.h | ||
) | ||
|
||
set( SRCS | ||
${HEADERS_ROOT}/Profiler.cpp | ||
) | ||
|
||
add_library( ${lib} ${SRCS} ${HDRS} ) | ||
|
||
target_include_directories( ${lib} INTERFACE | ||
${HEADERS_ROOT} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
#include <Profiler.h> | ||
|
||
|
||
std::map<std::string, std::pair<int, int>> alt::Profiler::map_; | ||
|
||
|
||
namespace alt | ||
{ | ||
|
||
|
||
Profiler::Profiler( const std::string& name, bool print, std::ostream& out ) | ||
: name_( name ) | ||
, print_( print ) | ||
, out_( out ) | ||
, tick_( std::chrono::steady_clock::now() ) | ||
{ | ||
if ( print_ ) | ||
out_ << "\nBegin profile. " << name_ << std::endl; | ||
} | ||
|
||
|
||
Profiler::~Profiler() | ||
{ | ||
const auto milli = currentMilli(); | ||
if ( print_ ) | ||
out_ << "End profile [" << milli << " ms]. " << name_ << "\n" << std::endl; | ||
addStatistic( name_, milli ); | ||
} | ||
|
||
|
||
void Profiler::printStatistics( std::ostream& os ) | ||
{ | ||
getStatistics( os ); | ||
} | ||
|
||
|
||
void Profiler::getStatistics( std::ostream& os ) | ||
{ | ||
if ( map_.empty() ) | ||
{ | ||
os | ||
<< "============================\n" | ||
<< "== Profiler map is EMPTY! ==\n" | ||
<< std::endl; | ||
return; | ||
} | ||
|
||
os | ||
<< "==================\n" | ||
<< "== Profiler map ==\n" | ||
<< "\n"; | ||
|
||
for ( const auto data : map_ ) | ||
{ | ||
int ms = std::get<0>( data.second ); | ||
int count = std::get<1>( data.second ); | ||
os | ||
<< data.first << "\n" | ||
<< "total ms: " << ms << "\n" | ||
<< "count: " << count << "\n" | ||
<< "avg: " << static_cast< double >( ms ) / count << "\n" | ||
<< std::endl; | ||
} | ||
} | ||
|
||
|
||
void Profiler::addStatistic( const std::string& name, int milli ) | ||
{ | ||
auto it = map_.find( name ); | ||
if ( it == map_.end() ) | ||
{ | ||
bool ok; | ||
std::tie( it, ok ) = map_.emplace( name, std::make_pair( 0, 0 ) ); | ||
if ( !ok ) | ||
return; | ||
} | ||
|
||
auto& par = it->second; | ||
std::get<0>( par ) += milli; | ||
++std::get<1>( par ); | ||
} | ||
|
||
|
||
std::size_t Profiler::currentMilli() const | ||
{ | ||
const auto tick = std::chrono::steady_clock::now(); | ||
return static_cast<std::size_t>( std::chrono::duration_cast<std::chrono::milliseconds>( tick - tick_ ).count() ); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#pragma once | ||
|
||
#include <chrono> | ||
#include <map> | ||
#include <iostream> | ||
#include <string> | ||
|
||
|
||
namespace alt | ||
{ | ||
|
||
|
||
class Profiler | ||
{ | ||
public: | ||
Profiler( const std::string& name, bool print = false, std::ostream& = std::cout ); | ||
~Profiler(); | ||
|
||
static void printStatistics( std::ostream& = std::cout ); | ||
static void getStatistics( std::ostream& ); | ||
|
||
private: | ||
static void addStatistic( const std::string&, int ); | ||
std::size_t currentMilli() const; | ||
|
||
const std::string name_; | ||
bool print_; | ||
std::ostream& out_; | ||
std::chrono::time_point<std::chrono::steady_clock> tick_; | ||
|
||
// name -> (total time, count) | ||
static std::map<std::string, std::pair<int, int>> map_; | ||
}; | ||
|
||
|
||
} |