summaryrefslogtreecommitdiff
path: root/src/tests/speed.cpp
blob: c6770de513760acfae524d1d01e964cbd7a142c0 (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
49
50
51
52
53
54
55
56
57
58
/*
 * Copyright (C) 2007-2010 Xagasoft, All rights reserved.
 *
 * This file is part of the libbu++ library and is released under the
 * terms of the license contained in the file LICENSE.
 */

#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()
{
	Bu::FString str;
	for( int j = 0; j < 500; j++ )
		str.append("Hey, this is a test string.  It will be reapeated many, many times.  How's that?");
	fullTest( tstCopy<Bu::FString>( str ) );
}