How do you benchmark code?

Aug 24, 2008 at 11:11am
I wrote my own time system to help me benchmark code. (below)

But I'm not sure it is the best system. Have a better one? Please share.

timeGetTime()
returns system time since windows started in milliseconds.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//.h
#ifndef _TIME_H_
#define _TIME_H_

#pragma comment (lib,"Winmm.lib")

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <mmsystem.h>

class cTime
{
public:
	static cTime*	Instance();
	static void		Destroy();

	void	operator()();
	UINT64	GetDTime(){return mDTime;}

private:
	cTime();
	~cTime();

	static cTime *mInstance;
	UINT64 mLastGameTime;
	UINT64 mDTime;
};

#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//.cpp
#include "Time.hpp"

cTime *cTime::mInstance	=	0;

cTime *cTime::Instance()
{
	if(mInstance)
		return mInstance;
	return mInstance = new cTime;
}

void cTime::Destroy()
{
	if(mInstance)
		delete mInstance;
}

cTime::cTime():
mDTime(0)
{
	mLastGameTime = (UINT64)timeGetTime();
}

cTime::~cTime(){}

void cTime::operator()()
{
	UINT64 ticks = (UINT64)timeGetTime();

	mDTime = ticks - mLastGameTime;

	mLastGameTime = ticks;
}


Example usage
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "time.h"
#include <iostream>
using namespace std;

void main()
{
	cTime &TimeRef = *cTime::Instance();

	TimeRef();
	// ... code to benchmark here ... //
	TimeRef();

	cout<<"Benchmark: "<<TimeRef.GetDTime();
}
Last edited on Aug 24, 2008 at 11:17am
Aug 24, 2008 at 7:42pm
I use a profiling tool. There are some open source ones, but I use a commercial tool called "AQTime". It allows me to profile performance and memory usage etc of my code.
Aug 24, 2008 at 8:45pm
I let the compiler create the assembly code, insert a loop in the assembly code. At the start of the program I use clock() for the number of clock ticks since the program start and at the end of the program I return (startvalue-clock())/CLOCKS_PER_SECOND.
Aug 25, 2008 at 2:10am
Zaita:
are there any open source ones you recommend?
Aug 25, 2008 at 2:14am
Aug 25, 2008 at 2:30pm
I usually use gprof (with the kprof frontend) and valgrind (callgrind actually) with the kcachegrind frontend. Those are for Linux and BSD, though (all Free & Open Source Software).
Topic archived. No new replies allowed.