diff options
Diffstat (limited to 'src/tests')
| -rw-r--r-- | src/tests/stringspeed.cpp | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/src/tests/stringspeed.cpp b/src/tests/stringspeed.cpp new file mode 100644 index 0000000..5636de7 --- /dev/null +++ b/src/tests/stringspeed.cpp | |||
| @@ -0,0 +1,122 @@ | |||
| 1 | #include <bu/string.h> | ||
| 2 | |||
| 3 | #include <sys/time.h> | ||
| 4 | #include <stdio.h> | ||
| 5 | |||
| 6 | void timeit( void (*func)(), const char *sName, int iIter ) | ||
| 7 | { | ||
| 8 | struct timeval ts, te; | ||
| 9 | |||
| 10 | gettimeofday( &ts, NULL ); | ||
| 11 | for( int j = 0; j < iIter; j++ ) | ||
| 12 | { | ||
| 13 | func(); | ||
| 14 | } | ||
| 15 | gettimeofday( &te, NULL ); | ||
| 16 | |||
| 17 | double dTotal = (double)(te.tv_sec-ts.tv_sec) + (double)(te.tv_usec-ts.tv_usec)/1000000.0; | ||
| 18 | printf("%10s: %f spi (%f total)\n", sName, dTotal/iIter, dTotal ); | ||
| 19 | } | ||
| 20 | |||
| 21 | void append1() | ||
| 22 | { | ||
| 23 | Bu::String sTmp; | ||
| 24 | for( int c = 0; c < 5000; c++ ) | ||
| 25 | { | ||
| 26 | sTmp += (char)(c%256); | ||
| 27 | } | ||
| 28 | } | ||
| 29 | |||
| 30 | void append2() | ||
| 31 | { | ||
| 32 | Bu::String sTmp; | ||
| 33 | for( int c = 0; c < 5000; c++ ) | ||
| 34 | { | ||
| 35 | sTmp.append( (char)(c%256) ); | ||
| 36 | } | ||
| 37 | } | ||
| 38 | |||
| 39 | void append3() | ||
| 40 | { | ||
| 41 | Bu::String sTmp; | ||
| 42 | for( int c = 0; c < 5000; c++ ) | ||
| 43 | { | ||
| 44 | sTmp += "Test"; | ||
| 45 | } | ||
| 46 | } | ||
| 47 | |||
| 48 | void append4() | ||
| 49 | { | ||
| 50 | Bu::String sTmp; | ||
| 51 | for( int c = 0; c < 5000; c++ ) | ||
| 52 | { | ||
| 53 | sTmp.append("Test"); | ||
| 54 | } | ||
| 55 | } | ||
| 56 | |||
| 57 | void append5() | ||
| 58 | { | ||
| 59 | Bu::String sTmp, sAdd("Test"); | ||
| 60 | for( int c = 0; c < 5000; c++ ) | ||
| 61 | { | ||
| 62 | sTmp += sAdd; | ||
| 63 | } | ||
| 64 | } | ||
| 65 | |||
| 66 | void append6() | ||
| 67 | { | ||
| 68 | Bu::String sTmp, sAdd("Test"); | ||
| 69 | for( int c = 0; c < 5000; c++ ) | ||
| 70 | { | ||
| 71 | sTmp.append( sAdd ); | ||
| 72 | } | ||
| 73 | } | ||
| 74 | |||
| 75 | void prepend1() | ||
| 76 | { | ||
| 77 | Bu::String sTmp; | ||
| 78 | for( int c = 0; c < 5000; c++ ) | ||
| 79 | { | ||
| 80 | sTmp.prepend('c'); | ||
| 81 | } | ||
| 82 | } | ||
| 83 | |||
| 84 | void copy1() | ||
| 85 | { | ||
| 86 | Bu::String sSrc; | ||
| 87 | for( int c = 0; c < 1000; c++ ) | ||
| 88 | { | ||
| 89 | sSrc += (char)(c%256); | ||
| 90 | Bu::String sTmp = sSrc; | ||
| 91 | sTmp.append( '-' ); | ||
| 92 | } | ||
| 93 | } | ||
| 94 | |||
| 95 | void copy2() | ||
| 96 | { | ||
| 97 | Bu::String sSrc; | ||
| 98 | for( int c = 0; c < 1000; c++ ) | ||
| 99 | { | ||
| 100 | sSrc += (char)(c%256); | ||
| 101 | Bu::String sTmp = sSrc; | ||
| 102 | } | ||
| 103 | } | ||
| 104 | |||
| 105 | void replace1() | ||
| 106 | { | ||
| 107 | Bu::String s("Hey, this is a replacement test, what do you thing of it?"); | ||
| 108 | s.replace("e", "X").replace("a", "X").replace("what","XXX"); | ||
| 109 | } | ||
| 110 | |||
| 111 | int main() | ||
| 112 | { | ||
| 113 | timeit( append1, "Append1", 5000 ); | ||
| 114 | timeit( append2, "Append2", 5000 ); | ||
| 115 | timeit( append3, "Append3", 2000 ); | ||
| 116 | timeit( append4, "Append4", 2000 ); | ||
| 117 | timeit( append4, "Prepend1", 2000 ); | ||
| 118 | timeit( copy1, "Copy1", 2000 ); | ||
| 119 | timeit( copy2, "Copy2", 2000 ); | ||
| 120 | timeit( replace1, "Replace1", 10000 ); | ||
| 121 | } | ||
| 122 | |||
