Skip to content

Commit

Permalink
+ README.md
Browse files Browse the repository at this point in the history
+ lib/Profiler.{h,cpp}
+ examples/simple.cpp
  • Loading branch information
green-anger committed Sep 22, 2018
1 parent 7a3c9a8 commit d2d3031
Show file tree
Hide file tree
Showing 7 changed files with 215 additions and 0 deletions.
18 changes: 18 additions & 0 deletions CMakeLists.txt
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()
7 changes: 7 additions & 0 deletions README.md
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.
2 changes: 2 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
add_executable( simple simple.cpp )
target_link_libraries( simple profiler )
42 changes: 42 additions & 0 deletions examples/simple.cpp
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;
}
19 changes: 19 additions & 0 deletions lib/CMakeLists.txt
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}
)
91 changes: 91 additions & 0 deletions lib/Profiler.cpp
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() );
}


}
36 changes: 36 additions & 0 deletions lib/Profiler.h
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_;
};


}

0 comments on commit d2d3031

Please sign in to comment.