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