test-string-concatenation.nut 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. local count = 100000;
  2. local start = os.clock();
  3. for(var k=0; k < count; ++k){
  4. var text = "Hello";
  5. text += " "
  6. text += "World!";
  7. text += "Hello";
  8. text += " "
  9. text += "World!";
  10. text += "Hello";
  11. text += " "
  12. text += "World!";
  13. text += "Hello";
  14. text += " "
  15. text += "World!";
  16. text += "Hello";
  17. text += " "
  18. text += "World!";
  19. text += "Hello";
  20. text += " "
  21. text += "World!";
  22. text += "Hello";
  23. text += " "
  24. text += "World!";
  25. text += "Hello";
  26. text += " "
  27. text += "World!";
  28. }
  29. print("String concatenation with + took:", os.clock()-start);
  30. start = os.clock();
  31. for(var k=0; k < count; ++k){
  32. var text = "Hello" + " " + "World!" + "Hello" + " " + "World!" + "Hello" + " " + "World!" + "Hello" +
  33. " " + "World!" + "Hello" + " " + "World!" + "Hello" + " " + "World!" + "Hello" + " " + "World!" +
  34. "Hello" + " " + "World!";
  35. }
  36. print("String concatenation with + took:", os.clock()-start);
  37. start = os.clock();
  38. for(var k=0; k < count; ++k){
  39. var buffer = [];
  40. buffer.push("Hello");
  41. buffer.push(" ");
  42. buffer.push("World!");
  43. buffer.push("Hello");
  44. buffer.push(" ");
  45. buffer.push("World!");
  46. buffer.push("Hello");
  47. buffer.push(" ");
  48. buffer.push("World!");
  49. buffer.push("Hello");
  50. buffer.push(" ");
  51. buffer.push("World!");
  52. buffer.push("Hello");
  53. buffer.push(" ");
  54. buffer.push("World!");
  55. buffer.push("Hello");
  56. buffer.push(" ");
  57. buffer.push("World!");
  58. buffer.push("Hello");
  59. buffer.push(" ");
  60. buffer.push("World!");
  61. buffer.push("Hello");
  62. buffer.push(" ");
  63. buffer.push("World!");
  64. var text = buffer.concat();
  65. }
  66. print("String concatenation with array.concat1 took:", os.clock()-start);
  67. var buffer = array(24);
  68. start = os.clock();
  69. for(var k=0; k < count; ++k){
  70. buffer.clear();
  71. buffer.push("Hello");
  72. buffer.push(" ");
  73. buffer.push("World!");
  74. buffer.push("Hello");
  75. buffer.push(" ");
  76. buffer.push("World!");
  77. buffer.push("Hello");
  78. buffer.push(" ");
  79. buffer.push("World!");
  80. buffer.push("Hello");
  81. buffer.push(" ");
  82. buffer.push("World!");
  83. buffer.push("Hello");
  84. buffer.push(" ");
  85. buffer.push("World!");
  86. buffer.push("Hello");
  87. buffer.push(" ");
  88. buffer.push("World!");
  89. buffer.push("Hello");
  90. buffer.push(" ");
  91. buffer.push("World!");
  92. buffer.push("Hello");
  93. buffer.push(" ");
  94. buffer.push("World!");
  95. var text = buffer.concat();
  96. }
  97. print("String concatenation with array.concat2 took:", os.clock()-start);
  98. print(type(buffer))
  99. var push = buffer.push.bindenv(buffer);
  100. print(type(push), type(push.getenv()), type(push.getenv().ref()))
  101. {
  102. buffer.clear();
  103. push("Hello");
  104. push(" ");
  105. push("World!");
  106. push("Hello");
  107. push(" ");
  108. push("World!");
  109. push("Hello");
  110. push(" ");
  111. push("World!");
  112. push("Hello");
  113. push(" ");
  114. push("World!");
  115. push("Hello");
  116. push(" ");
  117. push("World!");
  118. push("Hello");
  119. push(" ");
  120. push("World!");
  121. push("Hello");
  122. push(" ");
  123. push("World!");
  124. push("Hello");
  125. push(" ");
  126. push("World!");
  127. print(buffer.concat());
  128. }
  129. start = os.clock();
  130. for(var k=0; k < count; ++k){
  131. buffer.clear();
  132. push("Hello");
  133. push(" ");
  134. push("World!");
  135. push("Hello");
  136. push(" ");
  137. push("World!");
  138. push("Hello");
  139. push(" ");
  140. push("World!");
  141. push("Hello");
  142. push(" ");
  143. push("World!");
  144. push("Hello");
  145. push(" ");
  146. push("World!");
  147. push("Hello");
  148. push(" ");
  149. push("World!");
  150. push("Hello");
  151. push(" ");
  152. push("World!");
  153. push("Hello");
  154. push(" ");
  155. push("World!");
  156. var text = buffer.concat();
  157. }
  158. print("String concatenation with array.concat3 took:", os.clock()-start);
  159. buffer.resize(24);
  160. start = os.clock();
  161. for(var k=0; k < count; ++k){
  162. var i = 0;
  163. buffer.clear();
  164. buffer.resize(24);
  165. buffer[i++] = "Hello";
  166. buffer[i++] = " ";
  167. buffer[i++] = "World!";
  168. buffer[i++] = "Hello";
  169. buffer[i++] = " ";
  170. buffer[i++] = "World!";
  171. buffer[i++] = "Hello";
  172. buffer[i++] = " ";
  173. buffer[i++] = "World!";
  174. buffer[i++] = "Hello";
  175. buffer[i++] = " ";
  176. buffer[i++] = "World!";
  177. buffer[i++] = "Hello";
  178. buffer[i++] = " ";
  179. buffer[i++] = "World!";
  180. buffer[i++] = "Hello";
  181. buffer[i++] = " ";
  182. buffer[i++] = "World!";
  183. buffer[i++] = "Hello";
  184. buffer[i++] = " ";
  185. buffer[i++] = "World!";
  186. buffer[i++] = "Hello";
  187. buffer[i++] = " ";
  188. buffer[i++] = "World!";
  189. var text = buffer.concat();
  190. }
  191. print("String concatenation with array2.concat took:", os.clock()-start);
  192. start = os.clock();
  193. for(var k=0; k < count; ++k){
  194. var i = -1;
  195. buffer.clear();
  196. buffer.resize(24);
  197. buffer[++i] = "Hello";
  198. buffer[++i] = " ";
  199. buffer[++i] = "World!";
  200. buffer[++i] = "Hello";
  201. buffer[++i] = " ";
  202. buffer[++i] = "World!";
  203. buffer[++i] = "Hello";
  204. buffer[++i] = " ";
  205. buffer[++i] = "World!";
  206. buffer[++i] = "Hello";
  207. buffer[++i] = " ";
  208. buffer[++i] = "World!";
  209. buffer[++i] = "Hello";
  210. buffer[++i] = " ";
  211. buffer[++i] = "World!";
  212. buffer[++i] = "Hello";
  213. buffer[++i] = " ";
  214. buffer[++i] = "World!";
  215. buffer[++i] = "Hello";
  216. buffer[++i] = " ";
  217. buffer[++i] = "World!";
  218. buffer[++i] = "Hello";
  219. buffer[++i] = " ";
  220. buffer[++i] = "World!";
  221. var text = buffer.concat();
  222. }
  223. print("String concatenation with array3.concat took:", os.clock()-start);
  224. var ary_set = buffer.set.bindenv(buffer);
  225. start = os.clock();
  226. for(var k=0; k < count; ++k){
  227. var i = -1;
  228. buffer.clear();
  229. buffer.resize(24);
  230. ary_set(++i, "Hello");
  231. ary_set(++i, " ");
  232. ary_set(++i, "World!");
  233. ary_set(++i, "Hello");
  234. ary_set(++i, " ");
  235. ary_set(++i, "World!");
  236. ary_set(++i, "Hello");
  237. ary_set(++i, " ");
  238. ary_set(++i, "World!");
  239. ary_set(++i, "Hello");
  240. ary_set(++i, " ");
  241. ary_set(++i, "World!");
  242. ary_set(++i, "Hello");
  243. ary_set(++i, " ");
  244. ary_set(++i, "World!");
  245. ary_set(++i, "Hello");
  246. ary_set(++i, " ");
  247. ary_set(++i, "World!");
  248. ary_set(++i, "Hello");
  249. ary_set(++i, " ");
  250. ary_set(++i, "World!");
  251. ary_set(++i, "Hello");
  252. ary_set(++i, " ");
  253. ary_set(++i, "World!");
  254. var text = buffer.concat();
  255. }
  256. print("String concatenation with array4.concat took:", os.clock()-start);
  257. start = os.clock();
  258. for(var k=0; k < count; ++k){
  259. var i = -1;
  260. buffer.clear();
  261. buffer.resize(24);
  262. buffer.set(++i, "Hello");
  263. buffer.set(++i, " ");
  264. buffer.set(++i, "World!");
  265. buffer.set(++i, "Hello");
  266. buffer.set(++i, " ");
  267. buffer.set(++i, "World!");
  268. buffer.set(++i, "Hello");
  269. buffer.set(++i, " ");
  270. buffer.set(++i, "World!");
  271. buffer.set(++i, "Hello");
  272. buffer.set(++i, " ");
  273. buffer.set(++i, "World!");
  274. buffer.set(++i, "Hello");
  275. buffer.set(++i, " ");
  276. buffer.set(++i, "World!");
  277. buffer.set(++i, "Hello");
  278. buffer.set(++i, " ");
  279. buffer.set(++i, "World!");
  280. buffer.set(++i, "Hello");
  281. buffer.set(++i, " ");
  282. buffer.set(++i, "World!");
  283. buffer.set(++i, "Hello");
  284. buffer.set(++i, " ");
  285. buffer.set(++i, "World!");
  286. var text = buffer.concat();
  287. }
  288. print("String concatenation with array5.concat took:", os.clock()-start);