diff options
Diffstat (limited to 'src/tests/string.cpp')
-rw-r--r-- | src/tests/string.cpp | 154 |
1 files changed, 154 insertions, 0 deletions
diff --git a/src/tests/string.cpp b/src/tests/string.cpp new file mode 100644 index 0000000..12ce8a8 --- /dev/null +++ b/src/tests/string.cpp | |||
@@ -0,0 +1,154 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2007-2011 Xagasoft, All rights reserved. | ||
3 | * | ||
4 | * This file is part of the libbu++ library and is released under the | ||
5 | * terms of the license contained in the file LICENSE. | ||
6 | */ | ||
7 | |||
8 | #include "bu/hash.h" | ||
9 | #include "bu/string.h" | ||
10 | #include <sys/time.h> | ||
11 | #include <string> | ||
12 | |||
13 | #ifndef WIN32 | ||
14 | inline double getTime() | ||
15 | { | ||
16 | struct timeval tv; | ||
17 | gettimeofday( &tv, NULL ); | ||
18 | return ((double)tv.tv_sec) + ((double)tv.tv_usec/1000000.0); | ||
19 | } | ||
20 | #else | ||
21 | #include "windows.h" | ||
22 | #include "winbase.h" | ||
23 | inline double getTime() | ||
24 | { | ||
25 | uint32_t t = (uint32_t) GetTickCount(); | ||
26 | return (double) t / 1000.0; | ||
27 | } | ||
28 | #endif | ||
29 | |||
30 | Bu::String genThing() | ||
31 | { | ||
32 | Bu::String bob; | ||
33 | bob.append("ab "); | ||
34 | bob += "cd "; | ||
35 | bob += "efg"; | ||
36 | |||
37 | printf("---bob------\n%08tX: %s\n", (ptrdiff_t)bob.getStr(), | ||
38 | bob.getStr() ); | ||
39 | return bob; | ||
40 | } | ||
41 | |||
42 | void thing( Bu::String str ) | ||
43 | { | ||
44 | printf("Hey: %s\n", str.getStr() ); | ||
45 | } | ||
46 | |||
47 | void copyfunc( std::string temp ) | ||
48 | { | ||
49 | temp += "Hi"; | ||
50 | } | ||
51 | |||
52 | void copyfunc( Bu::String temp ) | ||
53 | { | ||
54 | temp += "Hi"; | ||
55 | } | ||
56 | |||
57 | void doTimings() | ||
58 | { | ||
59 | Bu::String fs1, fs2; | ||
60 | std::string ss1, ss2; | ||
61 | double dStart, dEnd, tfs1, tfs2, tfs3, tss1, tss2, tss3; | ||
62 | int nChars = 500000, nChunks=5000, nCopies=5000000, nChunkSize=1024*4; | ||
63 | char *buf = new char[nChunkSize]; | ||
64 | memset( buf, '!', nChunkSize ); | ||
65 | |||
66 | printf("Timing Bu::String single chars...\n"); | ||
67 | dStart = getTime(); | ||
68 | for( int j = 0; j < nChars; j++ ) fs1 += (char)('a'+(j%26)); | ||
69 | fs1.getStr(); | ||
70 | dEnd = getTime(); | ||
71 | tfs1 = dEnd-dStart; | ||
72 | |||
73 | printf("Timing std::string single chars...\n"); | ||
74 | dStart = getTime(); | ||
75 | for( int j = 0; j < nChars; j++ ) ss1 += (char)('a'+(j%26)); | ||
76 | ss1.c_str(); | ||
77 | dEnd = getTime(); | ||
78 | tss1 = dEnd-dStart; | ||
79 | |||
80 | printf("Timing Bu::String %d char chunks...\n", nChunkSize); | ||
81 | dStart = getTime(); | ||
82 | for( int j = 0; j < nChunks; j++ ) fs2.append(buf, nChunkSize); | ||
83 | fs2.getStr(); | ||
84 | dEnd = getTime(); | ||
85 | tfs2 = dEnd-dStart; | ||
86 | |||
87 | printf("Timing std::string %d char chunks...\n", nChunkSize); | ||
88 | dStart = getTime(); | ||
89 | for( int j = 0; j < nChunks; j++ ) ss2.append(buf, nChunkSize); | ||
90 | ss2.c_str(); | ||
91 | dEnd = getTime(); | ||
92 | tss2 = dEnd-dStart; | ||
93 | |||
94 | fs2 = "Hello there."; | ||
95 | ss2 = "Hello there."; | ||
96 | printf("Timing Bu::String copies...\n"); | ||
97 | dStart = getTime(); | ||
98 | for( int j = 0; j < nCopies; j++ ) Bu::String stmp = fs2; | ||
99 | dEnd = getTime(); | ||
100 | tfs3 = dEnd-dStart; | ||
101 | |||
102 | printf("Timing std::string copies...\n"); | ||
103 | dStart = getTime(); | ||
104 | for( int j = 0; j < nCopies; j++ ) std::string stpm = ss2; | ||
105 | dEnd = getTime(); | ||
106 | tss3 = dEnd-dStart; | ||
107 | |||
108 | printf( | ||
109 | "Results: singles: chunks: copies:\n" | ||
110 | "Bu::String %10.2f/s %10.2f/s %10.2f/s\n" | ||
111 | "std::string %10.2f/s %10.2f/s %10.2f/s\n", | ||
112 | nChars/tfs1, nChunks/tfs2, nCopies/tfs3, | ||
113 | nChars/tss1, nChunks/tss2, nCopies/tss3 ); | ||
114 | |||
115 | delete[] buf; | ||
116 | } | ||
117 | |||
118 | #define pem printf("---------\n%08tX: %s\n%08tX: %s\n", (ptrdiff_t)str.getStr(), str.getStr(), (ptrdiff_t)str2.getStr(), str2.getStr() ); | ||
119 | int main( ) | ||
120 | { | ||
121 | Bu::String fs1; | ||
122 | for( int j = 0; j < 500000; j++ ) fs1 += (char)('a'+(j%26)); | ||
123 | return 0; | ||
124 | |||
125 | Bu::String str("th"); | ||
126 | |||
127 | str.prepend("Hello "); | ||
128 | str.append("ere."); | ||
129 | |||
130 | Bu::String str2( str ); | ||
131 | pem; | ||
132 | str += " What's up?"; | ||
133 | pem; | ||
134 | str2 += " How are you?"; | ||
135 | pem; | ||
136 | str = str2; | ||
137 | pem; | ||
138 | |||
139 | str2 = genThing(); | ||
140 | pem; | ||
141 | |||
142 | str = str2; | ||
143 | pem; | ||
144 | |||
145 | thing( str2 ); | ||
146 | thing("test."); | ||
147 | |||
148 | printf("%d == %d\n", Bu::__calcHashCode( str ), Bu::__calcHashCode( str.getStr() ) ); | ||
149 | |||
150 | doTimings(); | ||
151 | |||
152 | return 0; | ||
153 | } | ||
154 | |||