fork(6) download
  1. using System;
  2. using System.Diagnostics;
  3.  
  4. namespace fastexponent
  5. {
  6. class Program
  7. {
  8. static double[] ExpAdjustment = new double[256] {
  9. 1.040389835,
  10. 1.039159306,
  11. 1.037945888,
  12. 1.036749401,
  13. 1.035569671,
  14. 1.034406528,
  15. 1.033259801,
  16. 1.032129324,
  17. 1.031014933,
  18. 1.029916467,
  19. 1.028833767,
  20. 1.027766676,
  21. 1.02671504,
  22. 1.025678708,
  23. 1.02465753,
  24. 1.023651359,
  25. 1.022660049,
  26. 1.021683458,
  27. 1.020721446,
  28. 1.019773873,
  29. 1.018840604,
  30. 1.017921503,
  31. 1.017016438,
  32. 1.016125279,
  33. 1.015247897,
  34. 1.014384165,
  35. 1.013533958,
  36. 1.012697153,
  37. 1.011873629,
  38. 1.011063266,
  39. 1.010265947,
  40. 1.009481555,
  41. 1.008709975,
  42. 1.007951096,
  43. 1.007204805,
  44. 1.006470993,
  45. 1.005749552,
  46. 1.005040376,
  47. 1.004343358,
  48. 1.003658397,
  49. 1.002985389,
  50. 1.002324233,
  51. 1.001674831,
  52. 1.001037085,
  53. 1.000410897,
  54. 0.999796173,
  55. 0.999192819,
  56. 0.998600742,
  57. 0.998019851,
  58. 0.997450055,
  59. 0.996891266,
  60. 0.996343396,
  61. 0.995806358,
  62. 0.995280068,
  63. 0.99476444,
  64. 0.994259393,
  65. 0.993764844,
  66. 0.993280711,
  67. 0.992806917,
  68. 0.992343381,
  69. 0.991890026,
  70. 0.991446776,
  71. 0.991013555,
  72. 0.990590289,
  73. 0.990176903,
  74. 0.989773325,
  75. 0.989379484,
  76. 0.988995309,
  77. 0.988620729,
  78. 0.988255677,
  79. 0.987900083,
  80. 0.987553882,
  81. 0.987217006,
  82. 0.98688939,
  83. 0.98657097,
  84. 0.986261682,
  85. 0.985961463,
  86. 0.985670251,
  87. 0.985387985,
  88. 0.985114604,
  89. 0.984850048,
  90. 0.984594259,
  91. 0.984347178,
  92. 0.984108748,
  93. 0.983878911,
  94. 0.983657613,
  95. 0.983444797,
  96. 0.983240409,
  97. 0.983044394,
  98. 0.982856701,
  99. 0.982677276,
  100. 0.982506066,
  101. 0.982343022,
  102. 0.982188091,
  103. 0.982041225,
  104. 0.981902373,
  105. 0.981771487,
  106. 0.981648519,
  107. 0.981533421,
  108. 0.981426146,
  109. 0.981326648,
  110. 0.98123488,
  111. 0.981150798,
  112. 0.981074356,
  113. 0.981005511,
  114. 0.980944219,
  115. 0.980890437,
  116. 0.980844122,
  117. 0.980805232,
  118. 0.980773726,
  119. 0.980749562,
  120. 0.9807327,
  121. 0.9807231,
  122. 0.980720722,
  123. 0.980725528,
  124. 0.980737478,
  125. 0.980756534,
  126. 0.98078266,
  127. 0.980815817,
  128. 0.980855968,
  129. 0.980903079,
  130. 0.980955475,
  131. 0.981017942,
  132. 0.981085714,
  133. 0.981160303,
  134. 0.981241675,
  135. 0.981329796,
  136. 0.981424634,
  137. 0.981526154,
  138. 0.981634325,
  139. 0.981749114,
  140. 0.981870489,
  141. 0.981998419,
  142. 0.982132873,
  143. 0.98227382,
  144. 0.982421229,
  145. 0.982575072,
  146. 0.982735318,
  147. 0.982901937,
  148. 0.983074902,
  149. 0.983254183,
  150. 0.983439752,
  151. 0.983631582,
  152. 0.983829644,
  153. 0.984033912,
  154. 0.984244358,
  155. 0.984460956,
  156. 0.984683681,
  157. 0.984912505,
  158. 0.985147403,
  159. 0.985388349,
  160. 0.98563532,
  161. 0.98588829,
  162. 0.986147234,
  163. 0.986412128,
  164. 0.986682949,
  165. 0.986959673,
  166. 0.987242277,
  167. 0.987530737,
  168. 0.987825031,
  169. 0.988125136,
  170. 0.98843103,
  171. 0.988742691,
  172. 0.989060098,
  173. 0.989383229,
  174. 0.989712063,
  175. 0.990046579,
  176. 0.990386756,
  177. 0.990732574,
  178. 0.991084012,
  179. 0.991441052,
  180. 0.991803672,
  181. 0.992171854,
  182. 0.992545578,
  183. 0.992924825,
  184. 0.993309578,
  185. 0.993699816,
  186. 0.994095522,
  187. 0.994496677,
  188. 0.994903265,
  189. 0.995315266,
  190. 0.995732665,
  191. 0.996155442,
  192. 0.996583582,
  193. 0.997017068,
  194. 0.997455883,
  195. 0.99790001,
  196. 0.998349434,
  197. 0.998804138,
  198. 0.999264107,
  199. 0.999729325,
  200. 1.000199776,
  201. 1.000675446,
  202. 1.001156319,
  203. 1.001642381,
  204. 1.002133617,
  205. 1.002630011,
  206. 1.003131551,
  207. 1.003638222,
  208. 1.00415001,
  209. 1.004666901,
  210. 1.005188881,
  211. 1.005715938,
  212. 1.006248058,
  213. 1.006785227,
  214. 1.007327434,
  215. 1.007874665,
  216. 1.008426907,
  217. 1.008984149,
  218. 1.009546377,
  219. 1.010113581,
  220. 1.010685747,
  221. 1.011262865,
  222. 1.011844922,
  223. 1.012431907,
  224. 1.013023808,
  225. 1.013620615,
  226. 1.014222317,
  227. 1.014828902,
  228. 1.01544036,
  229. 1.016056681,
  230. 1.016677853,
  231. 1.017303866,
  232. 1.017934711,
  233. 1.018570378,
  234. 1.019210855,
  235. 1.019856135,
  236. 1.020506206,
  237. 1.02116106,
  238. 1.021820687,
  239. 1.022485078,
  240. 1.023154224,
  241. 1.023828116,
  242. 1.024506745,
  243. 1.025190103,
  244. 1.02587818,
  245. 1.026570969,
  246. 1.027268461,
  247. 1.027970647,
  248. 1.02867752,
  249. 1.029389072,
  250. 1.030114973,
  251. 1.030826088,
  252. 1.03155163,
  253. 1.032281819,
  254. 1.03301665,
  255. 1.033756114,
  256. 1.034500204,
  257. 1.035248913,
  258. 1.036002235,
  259. 1.036760162,
  260. 1.037522688,
  261. 1.038289806,
  262. 1.039061509,
  263. 1.039837792,
  264. 1.040618648
  265. };
  266.  
  267. static double FastExp(double x)
  268. {
  269. var tmp = (long)(1512775 * x + 1072632447);
  270. int index = (int)(tmp >> 12) & 0xFF;
  271. return BitConverter.Int64BitsToDouble(tmp << 32) * ExpAdjustment[index];
  272. }
  273.  
  274. static void Main(string[] args)
  275. {
  276. double[] x = new double[1000000];
  277. double[] ex = new double[x.Length];
  278. double[] fx = new double[x.Length];
  279. Random r = new Random();
  280. for (int i = 0; i < x.Length; ++i)
  281. x[i] = r.NextDouble() * 40;
  282.  
  283. Stopwatch sw = new Stopwatch();
  284. sw.Start();
  285. for (int j = 0; j < x.Length; ++j)
  286. ex[j] = Math.Exp(x[j]);
  287. sw.Stop();
  288. double builtin = sw.Elapsed.TotalMilliseconds;
  289. sw.Reset();
  290. sw.Start();
  291. for (int k = 0; k < x.Length; ++k)
  292. fx[k] = FastExp(x[k]);
  293. sw.Stop();
  294. double custom = sw.Elapsed.TotalMilliseconds;
  295.  
  296. double min = 1, max = 1;
  297. for (int m = 0; m < x.Length; ++m) {
  298. double ratio = fx[m] / ex[m];
  299. if (min > ratio) min = ratio;
  300. if (max < ratio) max = ratio;
  301. }
  302.  
  303. Console.WriteLine("minimum ratio = " + min.ToString() + ", maximum ratio = " + max.ToString() + ", speedup = " + (builtin / custom).ToString());
  304. }
  305. }
  306. }
  307.  
Success #stdin #stdout 0.41s 80192KB
stdin
Standard input is empty
stdout
minimum ratio = 0.999397250057029, maximum ratio = 1.0005929587234, speedup = 0.272655868851219