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