sqlite
[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.
Brought by drakestapleton
| 1 | /* |
| 2 | ** Performance testing of floating-point decimal-to-binary conversion for |
| 3 | ** SQLite versus the standard library. |
| 4 | ** |
| 5 | ** This module compares library atof() against SQLite's sqlite3AtoF(). |
| 6 | ** To go the other direction (binary-to-decimal) see fp-speed-1.c. |
| 7 | ** |
| 8 | ** To compile: |
| 9 | ** |
| 10 | ** make sqlite3.c |
| 11 | ** gcc -Os -c sqlite3.c |
| 12 | ** gcc -I. -Os test/fp-speed-2.c sqlite3.o -ldl -lm -lpthread |
| 13 | ** |
| 14 | ** To run the test: |
| 15 | ** |
| 16 | ** ./a.out 10000000 |
| 17 | ** |
| 18 | ** Notes: |
| 19 | ** |
| 20 | ** * A first loop is run to measure the testing overhead. This |
| 21 | ** overhead is then subtracted from the times of subsequent loops |
| 22 | ** so that the estimates are for the calls to atof() or |
| 23 | ** sqlite3AtoF() only. |
| 24 | */ |
| 25 | #include "sqlite3.h" |
| 26 | #include <string.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <stdio.h> |
| 29 | #ifdef _WIN32 |
| 30 | # include <windows.h> |
| 31 | #else |
| 32 | # include <sys/time.h> |
| 33 | #endif |
| 34 | |
| 35 | static const char *aVal[] = { |
| 36 | "-1.01638304862856430", |
| 37 | "+0.00492438073915869", |
| 38 | "+7.38187324073439948", |
| 39 | "+7.06785952192257171", |
| 40 | "+9.28072663198500256", |
| 41 | "+5.88710508619333946", |
| 42 | "-2.29980236212596628", |
| 43 | "-1.59035819249108474", |
| 44 | "+2.43134418168449782", |
| 45 | "-3.82909873289453990", |
| 46 | "+1.87879140627440013", |
| 47 | "+0.72706535604871466", |
| 48 | "+0.06395776979131836", |
| 49 | "+5.22923388793158619", |
| 50 | "+6.37476826728722313", |
| 51 | "+8.69723395383291069", |
| 52 | "-9.50744860515976919", |
| 53 | "-8.64802578453687530", |
| 54 | "-3.56575957757974703", |
| 55 | "-7.83231061797317613", |
| 56 | "+7.78138752741208003", |
| 57 | "-1.87397189283601965", |
| 58 | "+8.68981459155933572", |
| 59 | "+6.05667663598778378", |
| 60 | "+4.17033792171481606", |
| 61 | "+2.12838712887466515", |
| 62 | "-6.83952360838918106", |
| 63 | "-6.21142997639395291", |
| 64 | "-2.07535257426146373", |
| 65 | "+5.87274598039442902", |
| 66 | "+8.58889910620021018", |
| 67 | "+6.86244610313559176", |
| 68 | "-3.30539867566709051", |
| 69 | "-4.35968431527634449", |
| 70 | "+0.08341395201040996", |
| 71 | "-8.85819865489042224", |
| 72 | "-3.66220954287276982", |
| 73 | "-6.69658522970250632", |
| 74 | "+1.82041693474064884", |
| 75 | "+6.52345080386490003", |
| 76 | "+1.59230060182190114", |
| 77 | "+1.73625552916563894", |
| 78 | "+7.28754319768547858", |
| 79 | "+1.28358801059589267", |
| 80 | "+8.05162533203208194", |
| 81 | "+6.63246333993811454", |
| 82 | "-1.71265000702800620", |
| 83 | "+1.69957383415837123", |
| 84 | "+7.60487669237386637", |
| 85 | "+0.61591172354494550", |
| 86 | "+5.75448943554159432", |
| 87 | "+8.29702285926905818", |
| 88 | "-6.55319253601630674", |
| 89 | "+5.83213346061870300", |
| 90 | "+5.65571665095718917", |
| 91 | "+0.33227897084384087", |
| 92 | "-7.12106487766986866", |
| 93 | "-9.67212625267063433", |
| 94 | "-3.45839165713773950", |
| 95 | "+4.78960943232147507", |
| 96 | "-9.69260280400041378", |
| 97 | "+7.06838482753813854", |
| 98 | "-5.29701141821629619", |
| 99 | "-4.42870212009053932", |
| 100 | "+0.07288911557328087", |
| 101 | "-9.18554620258794474", |
| 102 | "+3.72941262341310077", |
| 103 | "+2.68574218827927192", |
| 104 | "-4.70706073336246853", |
| 105 | "+7.21758207682793342", |
| 106 | "-8.36784125342611634", |
| 107 | "+2.21748443042418221", |
| 108 | "+0.19498245886068610", |
| 109 | "-9.73340529556720719", |
| 110 | "-9.77938877669369998", |
| 111 | "-5.15611645874169315", |
| 112 | "-7.50489935777651747", |
| 113 | "+7.35560765686877845", |
| 114 | "-5.06816285755335998", |
| 115 | "+1.52097056420277478", |
| 116 | "-7.59897825350482960", |
| 117 | "+1.36541372033897758", |
| 118 | "-1.64417205546513720", |
| 119 | "-4.90424331961411259", |
| 120 | "-7.70636119616491307", |
| 121 | "+0.16994274609307662", |
| 122 | "+8.33743178495722168", |
| 123 | "-5.23553304804695800", |
| 124 | "-3.85100459421941479", |
| 125 | "-6.35136225443263398", |
| 126 | "+2.38693034844544289", |
| 127 | "+3.83527158716203602", |
| 128 | "-3.12631204931368879", |
| 129 | "-5.57947970025564908", |
| 130 | "-8.81098744795956043", |
| 131 | "-4.37273601202032169", |
| 132 | "-3.11099511896685399", |
| 133 | "-9.48418780317042682", |
| 134 | "-3.73984516683044072", |
| 135 | "+4.89840420089159599", |
| 136 | }; |
| 137 | #define NN (sizeof(aVal)/sizeof(aVal[0])) |
| 138 | |
| 139 | /* Return the current wall-clock time in microseconds since the |
| 140 | ** Unix epoch (1970-01-01T00:00:00Z) |
| 141 | */ |
| 142 | static sqlite3_int64 timeOfDay(void){ |
| 143 | #if defined(_WIN64) && _WIN32_WINNT >= _WIN32_WINNT_WIN8 |
| 144 | sqlite3_uint64 t; |
| 145 | FILETIME tm; |
| 146 | GetSystemTimePreciseAsFileTime(&tm); |
| 147 | t = ((sqlite3_uint64)tm.dwHighDateTime<<32) | |
| 148 | (sqlite3_uint64)tm.dwLowDateTime; |
| 149 | t += 116444736000000000LL; |
| 150 | t /= 10; |
| 151 | return t; |
| 152 | #elif defined(_WIN32) |
| 153 | static sqlite3_vfs *clockVfs = 0; |
| 154 | sqlite3_int64 t; |
| 155 | if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0); |
| 156 | if( clockVfs==0 ) return 0; /* Never actually happens */ |
| 157 | if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){ |
| 158 | clockVfs->xCurrentTimeInt64(clockVfs, &t); |
| 159 | }else{ |
| 160 | double r; |
| 161 | clockVfs->xCurrentTime(clockVfs, &r); |
| 162 | t = (sqlite3_int64)(r*86400000.0); |
| 163 | } |
| 164 | return t*1000; |
| 165 | #else |
| 166 | struct timeval sNow; |
| 167 | (void)gettimeofday(&sNow,0); |
| 168 | return ((sqlite3_int64)sNow.tv_sec)*1000000 + sNow.tv_usec; |
| 169 | #endif |
| 170 | } |
| 171 | |
| 172 | /* |
| 173 | ** Generate text of the i-th test floating-point literal. |
| 174 | */ |
| 175 | static int fpLiteral(int i, char *z){ |
| 176 | int e, ex, len, ix; |
| 177 | ex = i%401; |
| 178 | e = ex - 200; |
| 179 | len = (i/401)%16 + 4; |
| 180 | ix = (i/(401*16))%NN; |
| 181 | memcpy(z, aVal[ix], len); |
| 182 | z[len++] = 'e'; |
| 183 | if( e<0 ){ |
| 184 | z[len++] = '-'; |
| 185 | e = -e; |
| 186 | }else{ |
| 187 | z[len++] = '+'; |
| 188 | } |
| 189 | z[len++] = e/100 + '0'; |
| 190 | z[len++] = (e/10)%10 + '0'; |
| 191 | z[len++] = e%10 + '0'; |
| 192 | z[len] = 0; |
| 193 | return ex; |
| 194 | } |
| 195 | |
| 196 | int main(int argc, char **argv){ |
| 197 | int i; |
| 198 | int cnt; |
| 199 | sqlite3_int64 tm[3]; |
| 200 | double arSum[401]; |
| 201 | char z[1000]; |
| 202 | |
| 203 | if( argc!=2 ){ |
| 204 | printf("Usage: %s COUNT\n", argv[0]); |
| 205 | printf("Suggested value for COUNT is 10 million\n"); |
| 206 | return 1; |
| 207 | } |
| 208 | cnt = atoi(argv[1]); |
| 209 | if( cnt<100 ){ |
| 210 | printf("Minimum COUNT value is 100"); |
| 211 | return 1; |
| 212 | } |
| 213 | |
| 214 | printf("test-overhead: "); |
| 215 | fflush(stdout); |
| 216 | memset(arSum, 0, sizeof(arSum)); |
| 217 | tm[2] = timeOfDay(); |
| 218 | for(i=0; i<cnt; i++){ |
| 219 | double r = (double)i; |
| 220 | int ex = fpLiteral(i,z); |
| 221 | arSum[ex] += r; |
| 222 | } |
| 223 | tm[2] = timeOfDay() - tm[2]; |
| 224 | for(i=1; i<=400; i++) arSum[0] += arSum[i]; |
| 225 | printf("%6.1f ns/test, %9.6f sec total\n", tm[2]*1e3/cnt, tm[2]*1e-6); |
| 226 | |
| 227 | printf("C-lib atof(): "); |
| 228 | fflush(stdout); |
| 229 | memset(arSum, 0, sizeof(arSum)); |
| 230 | tm[0] = timeOfDay(); |
| 231 | for(i=0; i<cnt; i++){ |
| 232 | int ex = fpLiteral(i,z); |
| 233 | arSum[ex] += atof(z); |
| 234 | } |
| 235 | tm[0] = timeOfDay() - tm[0] - tm[2]; |
| 236 | for(i=1; i<=400; i++) arSum[0] += arSum[i]; |
| 237 | printf("%6.1f ns/test, %9.6f sec net", tm[0]*1e3/cnt, tm[0]*1e-6); |
| 238 | printf(", cksum: %g\n", arSum[0]); |
| 239 | |
| 240 | printf("sqlite3AtoF(): "); |
| 241 | fflush(stdout); |
| 242 | memset(arSum, 0, sizeof(arSum)); |
| 243 | tm[1] = timeOfDay(); |
| 244 | for(i=0; i<cnt; i++){ |
| 245 | double r = 0.0; |
| 246 | int ex = fpLiteral(i,z); |
| 247 | sqlite3_test_control(SQLITE_TESTCTRL_ATOF, z, &r); |
| 248 | arSum[ex] += r; |
| 249 | } |
| 250 | tm[1] = timeOfDay() - tm[1] - tm[2]; |
| 251 | for(i=1; i<=400; i++) arSum[0] += arSum[i]; |
| 252 | printf("%6.1f ns/test, %9.6f sec net", tm[1]*1e3/cnt, tm[1]*1e-6); |
| 253 | printf(", cksum: %g\n", arSum[0]); |
| 254 | |
| 255 | if( tm[0] < tm[1] ){ |
| 256 | printf("atof() is about %g times faster than sqlite3AtoF()\n", |
| 257 | (double)tm[1]/(double)tm[0]); |
| 258 | }else{ |
| 259 | printf("sqlite3AtoF() is about %g times faster than atof()\n", |
| 260 | (double)tm[0]/(double)tm[1]); |
| 261 | } |
| 262 | return 0; |
| 263 | } |