Debian Codegraph @

test/fp-speed-1.c

raw
1 /*
2 ** Performance testing of floating-point binary-to-decimal conversion for
3 ** SQLite versus the standard library.
4 **
5 ** This module compares library sprintf() against SQLite's sqlite3_snprintf().
6 ** To go the other direction (decimal-to-binary) see fp-speed-2.c.
7 **
8 ** To compile:
9 **
10 ** make sqlite3.c
11 ** gcc -Os -c sqlite3.c
12 ** gcc -I. -Os test/fp-speed-1.c sqlite3.o -ldl -lm -lpthread
13 **
14 ** To run the test:
15 **
16 ** ./a.out 10000000
17 */
18 #include "sqlite3.h"
19 #include <stdio.h>
20 #include <stdlib.h>
21 #ifdef _WIN32
22 # include <windows.h>
23 #else
24 # include <sys/time.h>
25 #endif
26
27 static double aVal[] = {
28 -1.0163830486285643089e+063,
29 +0.0049243807391586981e-019,
30 +7.3818732407343994867e-095,
31 +7.0678595219225717143e+000,
32 +9.2807266319850025655e+120,
33 +5.8871050861933394663e+135,
34 -2.2998023621259662805e-168,
35 -1.5903581924910847482e+181,
36 +2.4313441816844978216e-001,
37 -3.8290987328945399091e-147,
38 +1.8787914062744001349e-009,
39 +0.7270653560487146653e-014,
40 +0.0639577697913183665e+107,
41 +5.2292338879315861930e-103,
42 +6.3747682672872231319e-021,
43 +8.6972339538329106984e-129,
44 -9.5074486051597691937e-026,
45 -8.6480257845368753018e-005,
46 -3.5657595775797470332e+017,
47 -7.8323106179731761351e+161,
48 +7.7813875274120800372e+077,
49 -1.8739718928360196596e-050,
50 +8.6898145915593357218e-051,
51 +6.0566766359877837871e+002,
52 +4.1703379217148160678e+199,
53 +2.1283871288746651560e+150,
54 -6.8395236083891810637e+128,
55 -6.2114299763939529155e-147,
56 -2.0753525742614637350e-149,
57 +5.8727459803944290257e-007,
58 +8.5888991062002101817e+010,
59 +6.8624461031355917632e+026,
60 -3.3053986756670905167e-075,
61 -4.3596843152763444945e-108,
62 +0.0834139520104099673e+098,
63 -8.8581986548904222440e-192,
64 -3.6622095428727698202e+038,
65 -6.6965852297025063260e+005,
66 +1.8204169347406488441e-054,
67 +6.5234508038649000384e-065,
68 +1.5923006018219011453e+083,
69 +1.7362555291656389480e+018,
70 +7.2875431976854785882e+160,
71 +1.2835880105958926748e-146,
72 +8.0516253320320819420e-113,
73 +6.6324633399381145440e-030,
74 -1.7126500070280062058e-149,
75 +1.6995738341583712335e+042,
76 +7.6048766923738663725e-112,
77 +0.6159117235449455043e-004,
78 +5.7544894355415943289e-135,
79 +8.2970228592690581890e-023,
80 -6.5531925360163067447e+020,
81 +5.8321334606187030050e+120,
82 +5.6557166509571891727e+095,
83 +0.3322789708438408704e-114,
84 -7.1210648776698686623e-050,
85 -9.6721262526706343301e+179,
86 -3.4583916571377395084e-106,
87 +4.7896094323214750793e-172,
88 -9.6926028040004137875e-056,
89 +7.0683848275381385483e-198,
90 -5.2970114182162961964e-007,
91 -4.4287021200905393271e-170,
92 +0.0728891155732808799e-189,
93 -9.1855462025879447465e+175,
94 +3.7294126234131007796e+015,
95 +2.6857421882792719241e+003,
96 -4.7070607333624685339e+039,
97 +7.2175820768279334274e+136,
98 -8.3678412534261163481e-115,
99 +2.2174844304241822163e+019,
100 +0.1949824588606861016e+112,
101 -9.7334052955672071912e+151,
102 -9.7793887766936999879e-142,
103 -5.1561164587416931561e+139,
104 -7.5048993577765174789e-022,
105 +7.3556076568687784568e+107,
106 -5.0681628575533599865e-144,
107 +1.5209705642027747811e+165,
108 -7.5989782535048296040e-101,
109 +1.3654137203389775871e-016,
110 -1.6441720554651372066e+087,
111 -4.9042433196141125923e+000,
112 -7.7063611961649130777e+118,
113 +0.1699427460930766201e+174,
114 +8.3374317849572216870e-145,
115 -5.2355330480469580072e+081,
116 -3.8510045942194147919e+141,
117 -6.3513622544326339887e-147,
118 +2.3869303484454428988e+049,
119 +3.8352715871620360268e-165,
120 -3.1263120493136887902e+035,
121 -5.5794797002556490823e+051,
122 -8.8109874479595604379e+142,
123 -4.3727360120203216922e+070,
124 -3.1109951189668539974e+170,
125 -9.4841878031704268232e+011,
126 -3.7398451668304407277e+067,
127 +4.8984042008915959963e-091,
128 };
129 #define NN (sizeof(aVal)/sizeof(aVal[0]))
130
131 /* Return the current wall-clock time in microseconds since the
132 ** Unix epoch (1970-01-01T00:00:00Z)
133 */
134 static sqlite3_int64 timeOfDay(void){
135 #if defined(_WIN64) && _WIN32_WINNT >= _WIN32_WINNT_WIN8
136 sqlite3_uint64 t;
137 FILETIME tm;
138 GetSystemTimePreciseAsFileTime(&tm);
139 t = ((sqlite3_uint64)tm.dwHighDateTime<<32) |
140 (sqlite3_uint64)tm.dwLowDateTime;
141 t += 116444736000000000LL;
142 t /= 10;
143 return t;
144 #elif defined(_WIN32)
145 static sqlite3_vfs *clockVfs = 0;
146 sqlite3_int64 t;
147 if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0);
148 if( clockVfs==0 ) return 0; /* Never actually happens */
149 if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){
150 clockVfs->xCurrentTimeInt64(clockVfs, &t);
151 }else{
152 double r;
153 clockVfs->xCurrentTime(clockVfs, &r);
154 t = (sqlite3_int64)(r*86400000.0);
155 }
156 return t*1000;
157 #else
158 struct timeval sNow;
159 (void)gettimeofday(&sNow,0);
160 return ((sqlite3_int64)sNow.tv_sec)*1000000 + sNow.tv_usec;
161 #endif
162 }
163
164 int main(int argc, char **argv){
165 int i;
166 int cnt;
167 static const char *zFmt = "%.17g";
168 sqlite3_int64 tm1, tm2;
169 char zBuf[1000];
170
171 if( argc!=2 ){
172 printf("Usage: %s COUNT\n", argv[0]);
173 printf("Suggested value for COUNT is 10 million\n");
174 return 1;
175 }
176 cnt = atoi(argv[1]);
177 if( cnt<100 ){
178 printf("Minimum COUNT value is 100");
179 return 1;
180 }
181
182 printf("C-library sprintf(\"%s\"): ", zFmt);
183 fflush(stdout);
184 tm1 = timeOfDay();
185 for(i=0; i<cnt; i++){
186 sprintf(zBuf, zFmt, aVal[i%NN]);
187 }
188 tm1 = timeOfDay() - tm1;
189 printf("%6.1f ns/call, %9.6f sec total\n", tm1*1.0e+3/(double)cnt,tm1*1.0e-6);
190
191 printf("sqlite3_snprintf(\"%s\"): ", zFmt);
192 tm2 = timeOfDay();
193 for(i=0; i<cnt; i++){
194 sqlite3_snprintf(sizeof(zBuf), zBuf, zFmt, aVal[i%NN]);
195 }
196 tm2 = timeOfDay() - tm2;
197 printf("%6.1f ns/call, %9.6f sec total\n", tm2*1.0e+3/(double)cnt,tm2*1.0e-6);
198
199 if( tm1 < tm2 ){
200 printf("sprintf() is about %g times faster than sqlite3_snprintf()\n",
201 (double)tm2/(double)tm1);
202 }else{
203 printf("sqlite3_snprintf() is about %g times faster than sprintf()\n",
204 (double)tm1/(double)tm2);
205 }
206 return 0;
207 }