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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
#include <bu/string.h>
#include <sys/time.h>
#include <stdio.h>
void timeit( void (*func)(), const char *sName, int iIter )
{
struct timeval ts, te;
gettimeofday( &ts, NULL );
for( int j = 0; j < iIter; j++ )
{
func();
}
gettimeofday( &te, NULL );
double dTotal = (double)(te.tv_sec-ts.tv_sec) + (double)(te.tv_usec-ts.tv_usec)/1000000.0;
printf("%10s: %f spi (%f total)\n", sName, dTotal/iIter, dTotal );
}
void append1()
{
Bu::String sTmp;
for( int c = 0; c < 5000; c++ )
{
sTmp += (char)(c%256);
}
}
void append2()
{
Bu::String sTmp;
for( int c = 0; c < 5000; c++ )
{
sTmp.append( (char)(c%256) );
}
}
void append3()
{
Bu::String sTmp;
for( int c = 0; c < 5000; c++ )
{
sTmp += "Test";
}
}
void append4()
{
Bu::String sTmp;
for( int c = 0; c < 5000; c++ )
{
sTmp.append("Test");
}
}
void append5()
{
Bu::String sTmp, sAdd("Test");
for( int c = 0; c < 5000; c++ )
{
sTmp += sAdd;
}
}
void append6()
{
Bu::String sTmp, sAdd("Test");
for( int c = 0; c < 5000; c++ )
{
sTmp.append( sAdd );
}
}
void prepend1()
{
Bu::String sTmp;
for( int c = 0; c < 5000; c++ )
{
sTmp.prepend('c');
}
}
void copy1()
{
Bu::String sSrc;
for( int c = 0; c < 1000; c++ )
{
sSrc += (char)(c%256);
Bu::String sTmp = sSrc;
sTmp.append( '-' );
}
}
void copy2()
{
Bu::String sSrc;
for( int c = 0; c < 1000; c++ )
{
sSrc += (char)(c%256);
Bu::String sTmp = sSrc;
}
}
void replace1()
{
Bu::String s("Hey, this is a replacement test, what do you thing of it?");
s.replace("e", "X").replace("a", "X").replace("what","XXX");
}
int main()
{
timeit( append1, "Append1", 5000 );
timeit( append2, "Append2", 5000 );
timeit( append3, "Append3", 2000 );
timeit( append4, "Append4", 2000 );
timeit( append4, "Prepend1", 2000 );
timeit( copy1, "Copy1", 2000 );
timeit( copy2, "Copy2", 2000 );
timeit( replace1, "Replace1", 10000 );
}
|