Spads 出品,作者 WangXP 与 Shane

目录
---------- ---------- ---------- ----------
1 【综述】
2 【程序】
3 【功能】
4 【性能】 测试程序
_      测试分析
5 【附录】
因为篇幅原因,51CTO 只允许单篇 80000 个字符,所以本文将拆分成几个部分发布。
其中 5【附录】是针对 1【综述】内容的,所以放在了一起。
4 【性能】
---------- ---------- ---------- ----------
依照之前提到的性能测试方法,每种方法循环量 loopNum 设为 12001000 ,各种情况性能表现如下。

 
  1. Performance test...
  2. Loop times = 1200 1000
  3. ---------- ---------- ---------- ----------
  4. String: 15
  5. tool:1201ms. --:1810ms. ap:2014ms. lib:3058ms. reg:5508ms.
  6. tool:982ms. --:1577ms. ap:1780ms. lib:2746ms. reg:5166ms.
  7. tool:967ms. --:1577ms. ap:1812ms. lib:2810ms. reg:5136ms.
  8. tool:983ms. --:1498ms. ap:1779ms. lib:2590ms. reg:5009ms.
  9. tool:920ms. --:1452ms. ap:1701ms. lib:2590ms. reg:5118ms.
  10. tool:998ms. --:1514ms. ap:1795ms. lib:2668ms. reg:5165ms.
  11. tool:998ms. --:1545ms. ap:1764ms. lib:2700ms. reg:5088ms.
  12. StringTool: 7049 ms.
  13. Direct: 10973 ms.
  14. WholeApacheUtils: 12645 ms.
  15. JavaLib: 19162 ms.
  16. WholeRegex: 36190 ms.
  17. ---------- ---------- ---------- ----------
  18. String: 28
  19. tool:1154ms. --:1654ms. ap:1966ms. lib:2684ms. reg:5353ms.
  20. tool:1123ms. --:1670ms. ap:1889ms. lib:2762ms. reg:5259ms.
  21. tool:1139ms. --:1764ms. ap:1889ms. lib:2747ms. reg:5460ms.
  22. tool:1168ms. --:1715ms. ap:1996ms. lib:2776ms. reg:5413ms.
  23. tool:1092ms. --:1842ms. ap:1842ms. lib:2794ms. reg:5447ms.
  24. tool:1170ms. --:1655ms. ap:1998ms. lib:2793ms. reg:5415ms.
  25. tool:1170ms. --:1671ms. ap:1858ms. lib:2685ms. reg:5338ms.
  26. StringTool: 8016 ms.
  27. Direct: 11971 ms.
  28. WholeApacheUtils: 13438 ms.
  29. JavaLib: 19241 ms.
  30. WholeRegex: 37685 ms.
  31. ---------- ---------- ---------- ----------
  32. String: 3.8
  33. tool:2121ms. --:2793ms. ap:3885ms. lib:4775ms. reg:7942ms.
  34. tool:2075ms. --:2778ms. ap:3808ms. lib:4760ms. reg:7724ms.
  35. tool:2137ms. --:2840ms. ap:4135ms. lib:5230ms. reg:8803ms.
  36. tool:2044ms. --:2606ms. ap:3480ms. lib:4713ms. reg:7959ms.
  37. tool:2013ms. --:2451ms. ap:3620ms. lib:4760ms. reg:8193ms.
  38. tool:1856ms. --:2777ms. ap:4150ms. lib:5009ms. reg:8223ms.
  39. tool:1872ms. --:2403ms. ap:3729ms. lib:4887ms. reg:8382ms.
  40. StringTool: 14118 ms.
  41. Direct: 18648 ms.
  42. WholeApacheUtils: 26807 ms.
  43. JavaLib: 34134 ms.
  44. WholeRegex: 57226 ms.
  45. ---------- ---------- ---------- ----------
  46. String: -99
  47. tool:1186ms. --:1780ms. ap:2248ms. lib:3325ms. reg:6649ms.
  48. tool:1108ms. --:1826ms. ap:2107ms. lib:2981ms. reg:6102ms.
  49. tool:1139ms. --:1717ms. ap:2170ms. lib:3247ms. reg:6446ms.
  50. tool:1154ms. --:1701ms. ap:1998ms. lib:3074ms. reg:6039ms.
  51. tool:1139ms. --:1686ms. ap:2029ms. lib:3199ms. reg:6508ms.
  52. tool:1123ms. --:1655ms. ap:1920ms. lib:2902ms. reg:5914ms.
  53. tool:1154ms. --:1748ms. ap:2123ms. lib:3215ms. reg:6773ms.
  54. StringTool: 8003 ms.
  55. Direct: 12113 ms.
  56. WholeApacheUtils: 14595 ms.
  57. JavaLib: 21943 ms.
  58. WholeRegex: 44431 ms.
  59. ---------- ---------- ---------- ----------
  60. String: .45
  61. tool:1763ms. --:2435ms. ap:3777ms. lib:4651ms. reg:7662ms.
  62. tool:1888ms. --:2263ms. ap:3823ms. lib:4963ms. reg:8131ms.
  63. tool:2153ms. --:2965ms. ap:4182ms. lib:5321ms. reg:8271ms.
  64. tool:1903ms. --:2513ms. ap:3667ms. lib:4713ms. reg:8130ms.
  65. tool:2075ms. --:2684ms. ap:3948ms. lib:5041ms. reg:8396ms.
  66. tool:2231ms. --:2809ms. ap:3995ms. lib:5041ms. reg:8208ms.
  67. tool:2230ms. --:3183ms. ap:4197ms. lib:5306ms. reg:8193ms.
  68. StringTool: 14243 ms.
  69. Direct: 18852 ms.
  70. WholeApacheUtils: 27589 ms.
  71. JavaLib: 35036 ms.
  72. WholeRegex: 56991 ms.
  73. ---------- ---------- ---------- ----------
  74. String: -.1
  75. tool:1825ms. --:2434ms. ap:4088ms. lib:4963ms. reg:7661ms.
  76. tool:2481ms. --:3028ms. ap:4525ms. lib:5665ms. reg:8084ms.
  77. tool:2153ms. --:2529ms. ap:4338ms. lib:5072ms. reg:7131ms.
  78. tool:2308ms. --:3292ms. ap:4322ms. lib:5446ms. reg:8224ms.
  79. tool:2543ms. --:2918ms. ap:4385ms. lib:5415ms. reg:8209ms.
  80. tool:2418ms. --:3012ms. ap:4213ms. lib:5446ms. reg:7959ms.
  81. tool:2247ms. --:2700ms. ap:3745ms. lib:4823ms. reg:7554ms.
  82. StringTool: 15975 ms.
  83. Direct: 19913 ms.
  84. WholeApacheUtils: 29616 ms.
  85. JavaLib: 36830 ms.
  86. WholeRegex: 54822 ms.
  87. ---------- ---------- ---------- ----------
  88. String: null
  89. --:47ms. tool:125ms. lib:140ms. reg:156ms. ap:156ms.
  90. --:31ms. lib:94ms. tool:94ms. ap:78ms. reg:125ms.
  91. --:16ms. ap:95ms. tool:95ms. reg:126ms. lib:126ms.
  92. --:31ms. lib:94ms. tool:94ms. ap:94ms. reg:109ms.
  93. --:47ms. reg:94ms. lib:94ms. tool:94ms. ap:94ms.
  94. --:47ms. lib:94ms. reg:110ms. ap:63ms. tool:125ms.
  95. --:15ms. reg:78ms. tool:78ms. lib:110ms. ap:110ms.
  96. Direct: 234 ms.
  97. WholeApacheUtils: 690 ms.
  98. StringTool: 705 ms.
  99. JavaLib: 752 ms.
  100. WholeRegex: 798 ms.
  101. ---------- ---------- ---------- ----------
  102. String: 0x3AFF0
  103. tool:406ms. ap:579ms. lib:1608ms. reg:4760ms. --:18146ms.
  104. tool:359ms. ap:578ms. lib:1763ms. reg:5180ms. --:17146ms.
  105. tool:328ms. ap:530ms. lib:1685ms. reg:4775ms. --:17303ms.
  106. tool:390ms. ap:562ms. lib:1607ms. reg:5009ms. --:15977ms.
  107. tool:359ms. ap:546ms. lib:1700ms. reg:4728ms. --:18519ms.
  108. tool:437ms. ap:702ms. lib:2107ms. reg:5494ms. --:16086ms.
  109. tool:374ms. ap:515ms. lib:1653ms. reg:5055ms. --:17224ms.
  110. StringTool: 2653 ms.
  111. WholeApacheUtils: 4012 ms.
  112. JavaLib: 12123 ms.
  113. WholeRegex: 35001 ms.
  114. Direct: 120401 ms.
  115. ---------- ---------- ---------- ----------
  116. String: 042
  117. tool:1170ms. --:1795ms. ap:2014ms. lib:2934ms. reg:7100ms.
  118. tool:1482ms. --:2154ms. ap:2481ms. lib:3559ms. reg:7788ms.
  119. tool:1373ms. --:2029ms. ap:2154ms. lib:3261ms. reg:7381ms.
  120. tool:1279ms. --:1935ms. ap:2201ms. lib:3339ms. reg:7459ms.
  121. tool:1279ms. --:1936ms. ap:2294ms. lib:3450ms. reg:7507ms.
  122. tool:1373ms. --:1826ms. ap:2122ms. lib:3402ms. reg:7583ms.
  123. tool:1513ms. --:2356ms. ap:2529ms. lib:3668ms. reg:7912ms.
  124. StringTool: 9469 ms.
  125. Direct: 14031 ms.
  126. WholeApacheUtils: 15795 ms.
  127. JavaLib: 23613 ms.
  128. WholeRegex: 52730 ms.
  129. ---------- ---------- ---------- ----------
  130. String: Spads
  131. tool:374ms. ap:577ms. lib:1825ms. reg:3480ms. --:15945ms.
  132. tool:281ms. ap:405ms. lib:1545ms. reg:3090ms. --:15930ms.
  133. tool:343ms. ap:655ms. lib:1716ms. reg:3324ms. --:16023ms.
  134. tool:265ms. ap:546ms. lib:1450ms. reg:3136ms. --:16148ms.
  135. tool:296ms. ap:452ms. lib:1482ms. reg:3012ms. --:18254ms.
  136. tool:297ms. ap:468ms. lib:1514ms. reg:2903ms. --:17131ms.
  137. tool:297ms. ap:484ms. lib:1482ms. reg:2981ms. --:15072ms.
  138. StringTool: 2153 ms.
  139. WholeApacheUtils: 3587 ms.
  140. JavaLib: 11014 ms.
  141. WholeRegex: 21926 ms.
  142. Direct: 114503 ms.
  143. ---------- ---------- ---------- ----------
  144. String:
  145. tool:93ms. lib:125ms. ap:110ms. reg:2231ms. --:16334ms.
  146. tool:125ms. lib:156ms. ap:140ms. reg:2683ms. --:15539ms.
  147. tool:156ms. lib:172ms. ap:156ms. reg:2746ms. --:15570ms.
  148. tool:125ms. ap:140ms. lib:172ms. reg:2418ms. --:16272ms.
  149. tool:125ms. lib:125ms. ap:141ms. reg:2153ms. --:14431ms.
  150. lib:140ms. tool:140ms. ap:125ms. reg:2589ms. --:18627ms.
  151. tool:141ms. ap:156ms. lib:172ms. reg:2777ms. --:17909ms.
  152. StringTool: 905 ms.
  153. WholeApacheUtils: 968 ms.
  154. JavaLib: 1062 ms.
  155. WholeRegex: 17597 ms.
  156. Direct: 114682 ms.
  157. ---------- ---------- ---------- ----------
  158. String:      
  159. lib:405ms. tool:421ms. ap:406ms. reg:2854ms. --:18175ms.
  160. tool:452ms. ap:468ms. lib:530ms. reg:3182ms. --:18833ms.
  161. lib:499ms. tool:499ms. ap:530ms. reg:2964ms. --:15086ms.
  162. lib:406ms. tool:421ms. ap:405ms. reg:2683ms. --:17005ms.
  163. ap:375ms. tool:421ms. lib:453ms. reg:3089ms. --:18752ms.
  164. tool:514ms. lib:500ms. ap:546ms. reg:3151ms. --:17254ms.
  165. lib:484ms. tool:484ms. ap:546ms. reg:3183ms. --:14962ms.
  166. StringTool: 3212 ms.
  167. WholeApacheUtils: 3276 ms.
  168. JavaLib: 3277 ms.
  169. WholeRegex: 21106 ms.
  170. Direct: 120067 ms.
  171. ---------- ---------- ---------- ----------
  172. String: 5 - 3
  173. tool:530ms. ap:905ms. lib:1904ms. reg:8487ms. --:17490ms.
  174. tool:484ms. ap:733ms. lib:1483ms. reg:7583ms. --:18051ms.
  175. tool:514ms. ap:686ms. lib:1467ms. reg:7707ms. --:18285ms.
  176. tool:640ms. ap:905ms. lib:1904ms. reg:8519ms. --:17474ms.
  177. tool:437ms. ap:702ms. lib:1546ms. reg:7942ms. --:16881ms.
  178. tool:453ms. ap:702ms. lib:1546ms. reg:8332ms. --:18941ms.
  179. tool:593ms. ap:749ms. lib:1857ms. reg:8582ms. --:17335ms.
  180. StringTool: 3651 ms.
  181. WholeApacheUtils: 5382 ms.
  182. JavaLib: 11707 ms.
  183. WholeRegex: 57152 ms.
  184. Direct: 124457 ms.
  185. ---------- ---------- ---------- ----------
  186. String: 三十八
  187. tool:328ms. ap:655ms. lib:1857ms. reg:3449ms. --:17537ms.
  188. tool:265ms. ap:483ms. lib:1466ms. reg:2731ms. --:18020ms.
  189. tool:265ms. ap:593ms. lib:1685ms. reg:3230ms. --:16101ms.
  190. tool:265ms. ap:468ms. lib:1420ms. reg:2981ms. --:17287ms.
  191. tool:328ms. ap:546ms. lib:1591ms. reg:3137ms. --:17318ms.
  192. tool:390ms. ap:624ms. lib:1685ms. reg:3012ms. --:17568ms.
  193. tool:265ms. ap:515ms. lib:1513ms. reg:2996ms. --:18005ms.
  194. StringTool: 2106 ms.
  195. WholeApacheUtils: 3884 ms.
  196. JavaLib: 11217 ms.
  197. WholeRegex: 21536 ms.
  198. Direct: 121836 ms.
  199. ---------- ---------- ---------- ----------
  200. String: 3.1415926.5358
  201. tool:1248ms. ap:1951ms. lib:3558ms. --:21202ms. reg:46397ms.
  202. tool:1092ms. ap:1873ms. lib:3245ms. --:20578ms. reg:44931ms.
  203. tool:1310ms. ap:2247ms. lib:3963ms. --:22497ms. reg:46475ms.
  204. tool:1154ms. ap:2013ms. lib:3791ms. --:21280ms. reg:46428ms.
  205. tool:1186ms. ap:1951ms. lib:3371ms. --:22139ms. reg:49457ms.
  206. tool:1404ms. ap:2310ms. lib:3776ms. --:21233ms. reg:46881ms.
  207. tool:1108ms. ap:1889ms. lib:3277ms. --:20968ms. reg:43433ms.
  208. StringTool: 8502 ms.
  209. WholeApacheUtils: 14234 ms.
  210. JavaLib: 24981 ms.
  211. Direct: 149897 ms.
  212. WholeRegex: 324002 ms.
  213. ---------- ---------- ---------- ----------
  214. String: 39501.50
  215. tool:3744ms. --:4088ms. ap:5929ms. lib:7318ms. reg:15728ms.
  216. tool:3728ms. --:4353ms. ap:5835ms. lib:7240ms. reg:14511ms.
  217. tool:3292ms. --:4104ms. ap:5914ms. lib:7225ms. reg:15166ms.
  218. tool:3198ms. --:3994ms. ap:5773ms. lib:6991ms. reg:15775ms.
  219. tool:3417ms. --:3698ms. ap:5633ms. lib:7038ms. reg:15198ms.
  220. tool:3260ms. --:4151ms. ap:6132ms. lib:7474ms. reg:15993ms.
  221. tool:3214ms. --:3620ms. ap:5399ms. lib:6788ms. reg:15603ms.
  222. StringTool: 23853 ms.
  223. Direct: 28008 ms.
  224. WholeApacheUtils: 40615 ms.
  225. JavaLib: 50074 ms.
  226. WholeRegex: 107974 ms.
  227. ---------- ---------- ---------- ----------
  228. String: 0.000385427
  229. tool:4228ms. --:4603ms. ap:6444ms. lib:8177ms. reg:17944ms.
  230. tool:4087ms. --:4431ms. ap:6678ms. lib:8301ms. reg:17834ms.
  231. tool:3775ms. --:4306ms. ap:6241ms. lib:7849ms. reg:18801ms.
  232. tool:3994ms. --:4307ms. ap:6709ms. lib:7771ms. reg:19269ms.
  233. tool:3526ms. --:4307ms. ap:6053ms. lib:7693ms. reg:18177ms.
  234. tool:3884ms. --:4073ms. ap:6381ms. lib:7880ms. reg:19269ms.
  235. tool:3447ms. --:3760ms. ap:5711ms. lib:7225ms. reg:17959ms.
  236. StringTool: 26941 ms.
  237. Direct: 29787 ms.
  238. WholeApacheUtils: 44217 ms.
  239. JavaLib: 54896 ms.
  240. WholeRegex: 129253 ms.
  241. ---------- ---------- ---------- ----------
  242. String: .33796678
  243. tool:3525ms. --:3916ms. ap:5648ms. lib:6928ms. reg:15712ms.
  244. tool:3292ms. --:3745ms. ap:5711ms. lib:7178ms. reg:15556ms.
  245. tool:3775ms. --:4119ms. ap:6007ms. lib:7287ms. reg:16071ms.
  246. tool:3463ms. --:3823ms. ap:5601ms. lib:7069ms. reg:14246ms.
  247. tool:3401ms. --:4026ms. ap:5991ms. lib:7162ms. reg:15884ms.
  248. tool:3806ms. --:4291ms. ap:5898ms. lib:7396ms. reg:15915ms.
  249. tool:3884ms. --:4088ms. ap:5960ms. lib:7553ms. reg:15946ms.
  250. StringTool: 25146 ms.
  251. Direct: 28008 ms.
  252. WholeApacheUtils: 40816 ms.
  253. JavaLib: 50573 ms.
  254. WholeRegex: 109330 ms.
  255. ---------- ---------- ---------- ----------
  256. String: 300011110000
  257. tool:3213ms. --:4322ms. ap:4869ms. lib:6522ms. reg:16897ms.
  258. tool:3354ms. --:4010ms. ap:4899ms. lib:6647ms. reg:18614ms.
  259. tool:3260ms. --:3792ms. ap:4463ms. lib:6117ms. reg:17178ms.
  260. tool:3276ms. --:3776ms. ap:4696ms. lib:6444ms. reg:17678ms.
  261. tool:3057ms. --:3838ms. ap:4853ms. lib:6382ms. reg:17351ms.
  262. tool:3448ms. --:4197ms. ap:5040ms. lib:6492ms. reg:18489ms.
  263. tool:3588ms. --:4322ms. ap:5727ms. lib:7880ms. reg:20377ms.
  264. StringTool: 23196 ms.
  265. Direct: 28257 ms.
  266. WholeApacheUtils: 34547 ms.
  267. JavaLib: 46484 ms.
  268. WholeRegex: 126584 ms.
  269. ---------- ---------- ---------- ----------
  270. String: 300 1500 1010
  271. tool:3432ms. --:3855ms. ap:5134ms. lib:6476ms. reg:18739ms.
  272. tool:3604ms. --:3963ms. ap:5055ms. lib:6367ms. reg:19409ms.
  273. tool:3432ms. --:3526ms. ap:4822ms. lib:5945ms. reg:18754ms.
  274. tool:2683ms. --:3371ms. ap:4712ms. lib:6258ms. reg:18239ms.
  275. tool:3198ms. --:3604ms. ap:4900ms. lib:6132ms. reg:19472ms.
  276. tool:3213ms. --:3620ms. ap:4790ms. lib:6133ms. reg:17444ms.
  277. tool:3135ms. --:3823ms. ap:5367ms. lib:6679ms. reg:18941ms.
  278. StringTool: 22697 ms.
  279. Direct: 25762 ms.
  280. WholeApacheUtils: 34780 ms.
  281. JavaLib: 43990 ms.
  282. WholeRegex: 130998 ms.
  283. ---------- ---------- ---------- ----------
  284. String: -570 0015 6726
  285. tool:3323ms. --:3527ms. ap:5290ms. lib:6508ms. reg:18911ms.
  286. --:3588ms. tool:3667ms. ap:5273ms. lib:6554ms. reg:18224ms.
  287. tool:3588ms. --:4072ms. ap:5071ms. lib:6616ms. reg:18926ms.
  288. tool:3697ms. --:3948ms. ap:5196ms. lib:6304ms. reg:18910ms.
  289. tool:3698ms. --:4135ms. ap:5086ms. lib:6632ms. reg:17382ms.
  290. tool:3760ms. --:3932ms. ap:5445ms. lib:6726ms. reg:19612ms.
  291. tool:3448ms. --:3776ms. ap:5103ms. lib:6227ms. reg:18365ms.
  292. StringTool: 25181 ms.
  293. Direct: 26978 ms.
  294. WholeApacheUtils: 36464 ms.
  295. JavaLib: 45567 ms.
  296. WholeRegex: 130330 ms.
  297. ---------- ---------- ---------- ----------
  298. String: -2378957832975
  299. tool:3354ms. --:4041ms. ap:5243ms. lib:6897ms. reg:18660ms.
  300. tool:3338ms. --:3886ms. ap:5086ms. lib:7068ms. reg:19347ms.
  301. tool:3978ms. --:3962ms. ap:5414ms. lib:7006ms. reg:18848ms.
  302. tool:3884ms. --:4525ms. ap:5352ms. lib:6928ms. reg:18551ms.
  303. tool:3962ms. --:4525ms. ap:5508ms. lib:6959ms. reg:19159ms.
  304. tool:4196ms. --:4306ms. ap:5164ms. lib:7161ms. reg:19018ms.
  305. tool:3931ms. --:3979ms. ap:5118ms. lib:6913ms. reg:17943ms.
  306. StringTool: 26643 ms.
  307. Direct: 29224 ms.
  308. WholeApacheUtils: 36885 ms.
  309. JavaLib: 48932 ms.
  310. WholeRegex: 131526 ms.
  311. ---------- ---------- ---------- ----------
  312. String: 1370 0000
  313. tool:2262ms. --:2825ms. ap:3621ms. lib:5041ms. reg:13778ms.
  314. tool:2559ms. --:3184ms. ap:4010ms. lib:5213ms. reg:15432ms.
  315. tool:2434ms. --:3199ms. ap:4057ms. lib:5259ms. reg:14028ms.
  316. tool:2153ms. --:2778ms. ap:3964ms. lib:5228ms. reg:13778ms.
  317. tool:2480ms. --:2965ms. ap:3979ms. lib:4947ms. reg:15009ms.
  318. tool:2309ms. --:2918ms. ap:3761ms. lib:4916ms. reg:13872ms.
  319. tool:2465ms. --:2965ms. ap:3714ms. lib:5041ms. reg:14760ms.
  320. StringTool: 16662 ms.
  321. Direct: 20834 ms.
  322. WholeApacheUtils: 27106 ms.
  323. JavaLib: 35645 ms.
  324. WholeRegex: 100657 ms.
  325. ---------- ---------- ---------- ----------
  326. String: 2015773
  327. tool:1872ms. --:2341ms. ap:3012ms. lib:4463ms. reg:11422ms.
  328. tool:2387ms. --:3449ms. ap:3466ms. lib:4854ms. reg:11641ms.
  329. tool:1919ms. --:2840ms. ap:3121ms. lib:4401ms. reg:12264ms.
  330. tool:1685ms. --:2325ms. ap:2934ms. lib:4635ms. reg:12030ms.
  331. tool:2012ms. --:2497ms. ap:3214ms. lib:4666ms. reg:11172ms.
  332. tool:2184ms. --:3043ms. ap:3495ms. lib:4854ms. reg:12780ms.
  333. tool:2496ms. --:3152ms. ap:3667ms. lib:4932ms. reg:13076ms.
  334. StringTool: 14555 ms.
  335. Direct: 19647 ms.
  336. WholeApacheUtils: 22909 ms.
  337. JavaLib: 32805 ms.
  338. WholeRegex: 84385 ms.
  339. ---------- ---------- ---------- ----------
  340. String: 3850
  341. tool:1778ms. --:2653ms. ap:3137ms. lib:4401ms. reg:9878ms.
  342. tool:1669ms. --:2466ms. ap:2824ms. lib:4105ms. reg:8739ms.
  343. tool:1326ms. --:2107ms. ap:2513ms. lib:3621ms. reg:8817ms.
  344. tool:1794ms. --:2501ms. ap:2844ms. lib:4000ms. reg:8603ms.
  345. tool:1326ms. --:1904ms. ap:2326ms. lib:3527ms. reg:8957ms.
  346. tool:1731ms. --:2342ms. ap:2810ms. lib:4059ms. reg:9116ms.
  347. tool:1623ms. --:2560ms. ap:2746ms. lib:3840ms. reg:9364ms.
  348. StringTool: 11247 ms.
  349. Direct: 16533 ms.
  350. WholeApacheUtils: 19200 ms.
  351. JavaLib: 27553 ms.
  352. WholeRegex: 63474 ms.
  353. ---------- ---------- ---------- ----------
  354. String: -80058
  355. tool:2199ms. --:2871ms. ap:3370ms. lib:4510ms. reg:9768ms.
  356. tool:1809ms. --:2232ms. ap:2809ms. lib:4337ms. reg:10516ms.
  357. tool:2200ms. --:2669ms. ap:3371ms. lib:4433ms. reg:10611ms.
  358. tool:1779ms. --:2326ms. ap:2684ms. lib:3761ms. reg:9643ms.
  359. tool:2106ms. --:2809ms. ap:3370ms. lib:4729ms. reg:10424ms.
  360. tool:1654ms. --:2326ms. ap:3215ms. lib:4651ms. reg:10362ms.
  361. tool:1716ms. --:1966ms. ap:2762ms. lib:4261ms. reg:10393ms.
  362. StringTool: 13463 ms.
  363. Direct: 17199 ms.
  364. WholeApacheUtils: 21581 ms.
  365. JavaLib: 30682 ms.
  366. WholeRegex: 71717 ms.
  367. ---------- ---------- ---------- ----------
  368. String: -1 2853 2781
  369. tool:3073ms. --:3370ms. ap:4525ms. lib:5633ms. reg:17475ms.
  370. tool:2558ms. --:3027ms. ap:4634ms. lib:5556ms. reg:16101ms.
  371. tool:3292ms. --:3590ms. ap:4478ms. lib:5915ms. reg:16009ms.
  372. tool:2762ms. --:2918ms. ap:4728ms. lib:5821ms. reg:16727ms.
  373. tool:2730ms. --:2996ms. ap:4650ms. lib:5758ms. reg:16102ms.
  374. tool:2949ms. --:3386ms. ap:4354ms. lib:5743ms. reg:15447ms.
  375. tool:2762ms. --:3792ms. ap:5634ms. lib:6743ms. reg:16696ms.
  376. StringTool: 20126 ms.
  377. Direct: 23079 ms.
  378. WholeApacheUtils: 33003 ms.
  379. JavaLib: 41169 ms.
  380. WholeRegex: 114557 ms.
  381. ---------- ---------- ---------- ----------
  382. String: 3
  383. tool:795ms. --:1483ms. ap:1935ms. lib:2810ms. reg:6179ms.
  384. tool:874ms. --:1733ms. ap:1810ms. lib:2638ms. reg:6102ms.
  385. tool:921ms. --:1733ms. ap:1810ms. lib:2903ms. reg:6476ms.
  386. tool:983ms. --:1717ms. ap:1858ms. lib:2716ms. reg:5978ms.
  387. tool:1030ms. --:1702ms. ap:1764ms. lib:2778ms. reg:6040ms.
  388. tool:842ms. --:1842ms. ap:1904ms. lib:2966ms. reg:6274ms.
  389. tool:858ms. --:1515ms. ap:1655ms. lib:2513ms. reg:5884ms.
  390. StringTool: 6303 ms.
  391. Direct: 11725 ms.
  392. WholeApacheUtils: 12736 ms.
  393. JavaLib: 19324 ms.
  394. WholeRegex: 42933 ms.
  395. ---------- ---------- ---------- ----------
  396. String: 0
  397. tool:967ms. --:1795ms. ap:1966ms. lib:3043ms. reg:6226ms.
  398. tool:921ms. --:1733ms. ap:2014ms. lib:3029ms. reg:6212ms.
  399. tool:764ms. --:1452ms. ap:1608ms. lib:2357ms. reg:5883ms.
  400. tool:921ms. --:1515ms. ap:1701ms. lib:2575ms. reg:6040ms.
  401. tool:764ms. --:1499ms. ap:1576ms. lib:2762ms. reg:5980ms.
  402. tool:811ms. --:1452ms. ap:1671ms. lib:2466ms. reg:5696ms.
  403. tool:842ms. --:1576ms. ap:1733ms. lib:2747ms. reg:6070ms.
  404. StringTool: 5990 ms.
  405. Direct: 11022 ms.
  406. WholeApacheUtils: 12269 ms.
  407. JavaLib: 18979 ms.
  408. WholeRegex: 42107 ms.
  409. ---------- ---------- ---------- ----------
  410. String: -597213859710830758190748937102794
  411. tool:24039ms. ap:26162ms. lib:28955ms. --:31283ms. reg:56618ms.
  412. tool:23681ms. ap:25897ms. lib:29876ms. --:31235ms. reg:56960ms.
  413. tool:25194ms. ap:26927ms. --:29237ms. lib:29441ms. reg:57038ms.
  414. tool:24367ms. ap:25367ms. lib:29034ms. --:31250ms. reg:55431ms.
  415. tool:26567ms. ap:28689ms. lib:30859ms. --:34152ms. reg:61110ms.
  416. tool:25225ms. ap:26085ms. lib:29657ms. --:31266ms. reg:58692ms.
  417. tool:25366ms. ap:27332ms. lib:29611ms. --:30813ms. reg:58005ms.
  418. StringTool: 174439 ms.
  419. WholeApacheUtils: 186459 ms.
  420. JavaLib: 207433 ms.
  421. Direct: 219236 ms.
  422. WholeRegex: 403854 ms.
  423. ---------- ---------- ---------- ----------
  424. String: 78 9254 3253 2452 9572 8532
  425. tool:21731ms. ap:23136ms. lib:23949ms. --:28848ms. reg:47928ms.
  426. tool:20467ms. ap:22403ms. lib:23636ms. --:27942ms. reg:49237ms.
  427. tool:19625ms. ap:22964ms. lib:24385ms. --:27849ms. reg:48177ms.
  428. tool:21653ms. ap:23775ms. lib:24650ms. --:27614ms. reg:49236ms.
  429. tool:20795ms. ap:23073ms. lib:24088ms. --:29283ms. reg:48347ms.
  430. tool:21279ms. ap:23167ms. lib:23761ms. --:29143ms. reg:48925ms.
  431. tool:20795ms. ap:22247ms. lib:24619ms. --:27725ms. reg:48864ms.
  432. StringTool: 146345 ms.
  433. WholeApacheUtils: 160765 ms.
  434. JavaLib: 169088 ms.
  435. Direct: 198404 ms.
  436. WholeRegex: 340714 ms.
  437. ---------- ---------- ---------- ----------
  438. String: 13205972138597109830758190748937102794
  439. --:29078ms. tool:47706ms. ap:49001ms. lib:49564ms. reg:80750ms.
  440. --:34102ms. tool:49079ms. ap:49392ms. lib:51841ms. reg:82761ms.
  441. --:28828ms. tool:45677ms. ap:47051ms. lib:49782ms. reg:81139ms.
  442. --:28407ms. tool:45553ms. ap:48003ms. lib:49829ms. reg:79642ms.
  443. --:27175ms. tool:46208ms. ap:47800ms. lib:48956ms. reg:79704ms.
  444. --:34757ms. tool:54679ms. ap:56864ms. lib:57816ms. reg:88221ms.
  445. --:28486ms. tool:47721ms. ap:48892ms. lib:49065ms. reg:81014ms.
  446. Direct: 210833 ms.
  447. StringTool: 336623 ms.
  448. WholeApacheUtils: 347003 ms.
  449. JavaLib: 356853 ms.
  450. WholeRegex: 573231 ms.
  451. ---------- ---------- ---------- ----------

我们可以看到,除了被转换数字太长的情况(30位数字以上)外,Spads 出品的 StringTool 都有非常明显的速度优势。 null 本身处理速度远远快于其它情况,即便是 StringTool ,也没有耽误多少执行时间。

┌────────────┐
│ 以下是51CTO独家 │
└────────────┘
为什么 Spads 提供的 StringTool 能具有显著的性能优势呢?
首先,正则表达式本身就是一个功能强大、通用但执行速度较慢的东西。其优势在于复杂匹配;对于转换数值这种简单匹配,其性能测试稳居最末的结果足以说明一切。
结合程序,我们能发现我在这个方法中将去除空格和验证格式合二为一了。这种安排避免了异常。生成异常需要读取方法调用栈的全部内容,所以执行速度很慢。直接处理方法就是通过是否产生异常来判断转型效果。
在优化过程中,我发现 Character#isDigit(char) 方法具有极其快的执行效率,甚至要快于直接数值比较。我对于 CPU 缓存的利用方式也不太了解,不知道它们咋调度的。但是 Character#isDigit(char) 配合过滤空白字符、负号和小数点,就变得慢下来了。测试程序如下。

 
  1. static private int[] charType = {
  2. 0,0,0,0,0, 0,0,0,0,1,
  3. 0,0,0,0,0, 0,0,0,0,0,
  4. 0,0,0,0,0, 0,0,0,0,0,
  5. 0,0,1,0,0, 0,0,0,0,0,
  6. 0,0,0,0,0, 2,3,0,4,4,
  7. 4,4,4,4,4, 4,4,4
  8. };
  9.  
  10. public void testIsCharacter()
  11. {
  12. Runnable r;
  13. System.out.println("Loop times = " + this.loopNum);
  14. System.out.println("---------- ---------- ---------- ----------");
  15.  
  16. final Map<String, Long> timeUsedCount =
  17. Collections.synchronizedMap(new HashMap<String, Long>());
  18.  
  19. final char[] charSet = {
  20. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  21. 'a', 'B', ' ', '\t', '中', '垚'
  22. };
  23.  
  24. for (int loop = -1; ++loop != 7; )
  25. {
  26. final AtomicInteger runThreadNum = new AtomicInteger(0);
  27.  
  28. // StringTool
  29. r = new Runnable()
  30. {
  31. public void run()
  32. {
  33. Random ran = new Random();
  34. boolean hasEx = false;
  35. if (timeUsedCount.get("StringTool") == null)
  36. timeUsedCount.put("StringTool", 0L);
  37. long start = System.currentTimeMillis();
  38. boolean result = false;
  39. for (int index = -1; ++index != CharTest.this.loopNum; )
  40. {
  41. try
  42. {
  43. char testChar = CharTest.charSet[ran.nextInt(16)];
  44. result = CharTest.charType[testChar] == 4;
  45. }
  46. catch (Exception ex) { hasEx = true; }
  47. }
  48. long use = System.currentTimeMillis() - start;
  49. System.out.print("\ttool:" + use + "ms" + (hasEx ? "(EX)" : "") + '.');
  50. use += timeUsedCount.get("StringTool");
  51. timeUsedCount.put("StringTool", use);
  52. runThreadNum.addAndGet(-1);
  53. Thread.yield();
  54. }
  55. };
  56. runThreadNum.addAndGet(1);
  57. new Thread(r).start();
  58.  
  59. // JavaLib
  60. r = new Runnable()
  61. {
  62. public void run()
  63. {
  64. Random ran = new Random();
  65. boolean hasEx = false;
  66. if (timeUsedCount.get("Character#isDigit(char)") == null)
  67. timeUsedCount.put("Character#isDigit(char)", 0L);
  68. long start = System.currentTimeMillis();
  69. boolean result = false;
  70. for (int index = -1; ++index != CharTest.this.loopNum; )
  71. {
  72. try
  73. {
  74. char testChar = CharTest.charSet[ran.nextInt(16)];
  75. result = Character.isDigit(testChar);
  76. }
  77. catch (Exception ex) { hasEx = true; }
  78. }
  79. long use = System.currentTimeMillis() - start;
  80. System.out.print("\tjava:" + use + "ms" + (hasEx ? "(EX)" : "") + '.');
  81. use += timeUsedCount.get("Character#isDigit(char)");
  82. timeUsedCount.put("Character#isDigit(char)", use);
  83. runThreadNum.addAndGet(-1);
  84. Thread.yield();
  85. }
  86. };
  87. runThreadNum.addAndGet(1);
  88. new Thread(r).start();
  89.  
  90. while (runThreadNum.get() != 0)
  91. {
  92. try { Thread.sleep(10); } catch (Exception ex) { }
  93. Thread.yield();
  94. }
  95.  
  96. for (int index = -1; ++index != 3; )
  97. {
  98. try { Thread.sleep(100); } catch (Exception ex) { }
  99. Thread.yield();
  100. }
  101. System.out.println();
  102. }
  103. SortedMap<Long, String> thisNumStrTimeUsed =
  104. Collections.synchronizedSortedMap(new TreeMap<Long, String>());
  105. for (String name: timeUsedCount.keySet())
  106. thisNumStrTimeUsed.put(timeUsedCount.get(name), name);
  107.  
  108. for (Long time: thisNumStrTimeUsed.keySet())
  109. System.out.println(thisNumStrTimeUsed.get(time) + ": " + time + " ms.");
  110.  
  111. System.out.println("---------- ---------- ---------- ----------");
  112. }

测试结果如下。

 
  1. Loop times = 5000 0000
  2. ---------- ---------- ---------- ----------
  3. java:860ms. tool:868ms.
  4. java:874ms. tool:882ms.
  5. java:901ms. tool:926ms.
  6. java:888ms. tool:904ms.
  7. java:892ms. tool:906ms.
  8. java:853ms. tool:876ms.
  9. java:834ms. tool:914ms.
  10. Character#isDigit(char): 6102 ms.
  11. StringTool: 6276 ms.
  12. ---------- ---------- ---------- ----------

由于在 Spads 提供的字符串转换成数值方法中,对字符的处理是判断数值与空格一并进行的,所以没有采用系统提供的 Character#isDigit(char) 方法。而通过系统判断以及阿帕奇工具,都受牵制于这个高效方法,没有办法更大改变程序结构了。

本文还发表在我的其它技术日志
CSDN :
ITeye :
中国开源社区: