aboutsummaryrefslogtreecommitdiff
path: root/src/tests/speed.cpp
blob: b8924d7ea99050dc0d3e053ed67b748f39aa4db8 (plain)
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include "bu/fstring.h"
#include <sys/time.h>

template<typename a>
struct tstCopy
{
	tstCopy( const a &src ) :
		src( src )
	{
	}

	void operator()()
	{
		a tmp = src;
	}

	a src;
};

template<typename f>
double runTest( f fnc )
{
	struct timeval tStart, tEnd;
	int j = 0;
	gettimeofday( &tStart, NULL );
	for(; j < 500000; j++ )
		fnc();
	gettimeofday( &tEnd, NULL );

	return (double)(tEnd.tv_sec-tStart.tv_sec)+
		(double)(tEnd.tv_usec-tStart.tv_usec)/1000000.0;
}

template<typename tst>
void fullTest( tst t )
{
	double dTotal;
	int iCount = 10;
	for( int j = 0; j < iCount; j++ )
		dTotal += runTest( t );
	printf("Average time:  %f\n", dTotal/iCount );
}

int main()
{
	fullTest( tstCopy<Bu::FString>("This is a test string.") );
}