Skip to content

Instantly share code, notes, and snippets.

@mortennobel
Created January 23, 2013 08:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mortennobel/4603345 to your computer and use it in GitHub Desktop.
Save mortennobel/4603345 to your computer and use it in GitHub Desktop.
//
// PerformanceTool.h
//
// Created by Morten Nobel-Jørgensen on 1/23/13.
// Copyright (c) 2013 Morten Nobel-Joergensen. All rights reserved.
//
// Create simple performance meassures in ms
//
// Usage:
//
// PERF_INIT();
// PERF(someHeavyFunctionCall());
//
// Result:
//
// file: file.cpp line: 123 time: 123 ms
//
#ifndef PerformanceTool_h
#define PerformanceTool_h
#include <iostream>
#if !defined(_WIN64) && !defined(_WIN32)
# include <sys/time.h>
typedef timeval sys_time_t;
inline void system_time(sys_time_t* t) {
gettimeofday(t, NULL);
}
inline long long time_to_msec(const sys_time_t& t) {
return t.tv_sec * 1000LL + t.tv_usec / 1000;
}
#else
# include <sys/timeb.h>
typedef _timeb sys_time_t;
inline void system_time(sys_time_t* t) { _ftime(t); }
inline long long time_to_msec(const sys_time_t& t) {
return t.time * 1000LL + t.millitm;
}
#endif
#define PERF_INIT() sys_time_t t; long long startTimeMs; long long endTimeMs;
#define PERF_(x,file,line) system_time(&t); \
startTimeMs = time_to_msec(t); \
x; \
system_time(&t); \
endTimeMs = time_to_msec(t); \
std::cout << "file:\t"<<file << "\tline:\t"<<line<< "\ttime:\t"<<(endTimeMs - startTimeMs)<<"\tms"<<std::endl;
#define PERF(x) PERF_(x,__FILE__,__LINE__);
#endif // PerformanceTool_h
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment