benchNode.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. S = typeof S === 'undefined' ? require('..') : S;
  2. var now = typeof process === 'undefined' ? browserNow : nodeNow;
  3. var COUNT = 1e5;
  4. main();
  5. function main() {
  6. var total = 0;
  7. total += bench(createDataSignals, COUNT, COUNT);
  8. total += bench(createComputations0to1, COUNT, 0);
  9. total += bench(createComputations1to1, COUNT, COUNT);
  10. total += bench(createComputations2to1, COUNT / 2, COUNT);
  11. total += bench(createComputations4to1, COUNT / 4, COUNT);
  12. total += bench(createComputations1000to1, COUNT / 1000, COUNT);
  13. //total += bench1(createComputations8, COUNT, 8 * COUNT);
  14. total += bench(createComputations1to2, COUNT, COUNT / 2);
  15. total += bench(createComputations1to4, COUNT, COUNT / 4);
  16. total += bench(createComputations1to8, COUNT, COUNT / 8);
  17. total += bench(createComputations1to1000, COUNT, COUNT / 1000);
  18. console.log('---');
  19. total += bench(updateComputations1to1, COUNT * 4, 1);
  20. total += bench(updateComputations2to1, COUNT * 2, 2);
  21. total += bench(updateComputations4to1, COUNT, 4);
  22. total += bench(updateComputations1000to1, COUNT / 100, 1000);
  23. total += bench(updateComputations1to2, COUNT * 4, 1);
  24. total += bench(updateComputations1to4, COUNT * 4, 1);
  25. total += bench(updateComputations1to1000, COUNT * 4, 1);
  26. console.log(`total: ${total.toFixed(0)}`);
  27. }
  28. function bench(fn, count, scount) {
  29. var time = run(fn, count, scount);
  30. console.log(`${fn.name}: ${time.toFixed(0)}`);
  31. return time;
  32. }
  33. function run(fn, n, scount) {
  34. // prep n * arity sources
  35. var start,
  36. end;
  37. S.root(function () {
  38. // run 3 times to warm up
  39. var sources = createDataSignals(scount, []);
  40. fn(n / 100, sources);
  41. sources = createDataSignals(scount, []);
  42. fn(n / 100, sources);
  43. sources = createDataSignals(scount, []);
  44. %OptimizeFunctionOnNextCall(fn);
  45. fn(n / 100, sources);
  46. sources = createDataSignals(scount, []);
  47. for (var i = 0; i < scount; i++) {
  48. sources[i].current();
  49. sources[i].current();
  50. %OptimizeFunctionOnNextCall(sources[i]);
  51. sources[i].current();
  52. }
  53. // start GC clean
  54. %CollectGarbage(null);
  55. start = now();
  56. fn(n, sources);
  57. // end GC clean
  58. sources = null;
  59. %CollectGarbage(null);
  60. end = now();
  61. });
  62. return end - start;
  63. }
  64. function createDataSignals(n, sources) {
  65. for (var i = 0; i < n; i++) {
  66. sources[i] = S.makeDataNode(i);
  67. }
  68. return sources;
  69. }
  70. function createComputations0to1(n, sources) {
  71. for (var i = 0; i < n; i++) {
  72. createComputation0(i);
  73. }
  74. }
  75. function createComputations1to1000(n, sources) {
  76. for (var i = 0; i < n / 1000; i++) {
  77. for (var j = 0; j < 1000; j++) {
  78. createComputation1(sources[i]);
  79. }
  80. //sources[i] = null;
  81. }
  82. }
  83. function createComputations1to8(n, sources) {
  84. for (var i = 0; i < n / 8; i++) {
  85. createComputation1(sources[i]);
  86. createComputation1(sources[i]);
  87. createComputation1(sources[i]);
  88. createComputation1(sources[i]);
  89. createComputation1(sources[i]);
  90. createComputation1(sources[i]);
  91. createComputation1(sources[i]);
  92. createComputation1(sources[i]);
  93. //sources[i] = null;
  94. }
  95. }
  96. function createComputations1to4(n, sources) {
  97. for (var i = 0; i < n / 4; i++) {
  98. createComputation1(sources[i]);
  99. createComputation1(sources[i]);
  100. createComputation1(sources[i]);
  101. createComputation1(sources[i]);
  102. //sources[i] = null;
  103. }
  104. }
  105. function createComputations1to2(n, sources) {
  106. for (var i = 0; i < n / 2; i++) {
  107. createComputation1(sources[i]);
  108. createComputation1(sources[i]);
  109. //sources[i] = null;
  110. }
  111. }
  112. function createComputations1to1(n, sources) {
  113. for (var i = 0; i < n; i++) {
  114. createComputation1(sources[i]);
  115. //sources[i] = null;
  116. }
  117. }
  118. function createComputations2to1(n, sources) {
  119. for (var i = 0; i < n; i++) {
  120. createComputation2(
  121. sources[i * 2],
  122. sources[i * 2 + 1]
  123. );
  124. //sources[i * 2] = null;
  125. //sources[i * 2 + 1] = null;
  126. }
  127. }
  128. function createComputations4to1(n, sources) {
  129. for (var i = 0; i < n; i++) {
  130. createComputation4(
  131. sources[i * 4],
  132. sources[i * 4 + 1],
  133. sources[i * 4 + 2],
  134. sources[i * 4 + 3]
  135. );
  136. //sources[i * 4] = null;
  137. //sources[i * 4 + 1] = null;
  138. //sources[i * 4 + 2] = null;
  139. //sources[i * 4 + 3] = null;
  140. }
  141. }
  142. function createComputations8(n, sources) {
  143. for (var i = 0; i < n; i++) {
  144. createComputation8(
  145. sources[i * 8],
  146. sources[i * 8 + 1],
  147. sources[i * 8 + 2],
  148. sources[i * 8 + 3],
  149. sources[i * 8 + 4],
  150. sources[i * 8 + 5],
  151. sources[i * 8 + 6],
  152. sources[i * 8 + 7]
  153. );
  154. sources[i * 8] = null;
  155. sources[i * 8 + 1] = null;
  156. sources[i * 8 + 2] = null;
  157. sources[i * 8 + 3] = null;
  158. sources[i * 8 + 4] = null;
  159. sources[i * 8 + 5] = null;
  160. sources[i * 8 + 6] = null;
  161. sources[i * 8 + 7] = null;
  162. }
  163. }
  164. // only create n / 100 computations, as otherwise takes too long
  165. function createComputations1000to1(n, sources) {
  166. for (var i = 0; i < n; i++) {
  167. createComputation1000(sources, i * 1000);
  168. }
  169. }
  170. function createComputation0(i) {
  171. S.makeComputationNode(function () { return i; }, undefined);
  172. }
  173. function createComputation1(s1) {
  174. S.makeComputationNode(function () { return s1.current(); }, undefined);
  175. }
  176. function createComputation2(s1, s2) {
  177. S.makeComputationNode(function () { return s1.current() + s2.current(); }, undefined);
  178. }
  179. function createComputation4(s1, s2, s3, s4) {
  180. S.makeComputationNode(function () { return s1.current() + s2.current() + s3.current() + s4.current(); }, undefined);
  181. }
  182. function createComputation8(s1, s2, s3, s4, s5, s6, s7, s8) {
  183. S.makeComputationNode(function () { return s1.current() + s2.current() + s3.current() + s4.current() + s5.current() + s6.current() + s7.current() + s8.current(); }, undefined);
  184. }
  185. function createComputation1000(ss, offset) {
  186. S.makeComputationNode(function () {
  187. var sum = 0;
  188. for (var i = 0; i < 1000; i++) {
  189. sum += ss[offset + i].current();
  190. }
  191. return sum;
  192. }, undefined);
  193. }
  194. function updateComputations1to1(n, sources) {
  195. var s1 = sources[0],
  196. c = S.makeComputationNode(function () { return s1.current(); }, undefined);
  197. for (var i = 0; i < n; i++) {
  198. s1.next(i);
  199. }
  200. }
  201. function updateComputations2to1(n, sources) {
  202. var s1 = sources[0],
  203. s2 = sources[1],
  204. c = S.makeComputationNode(function () { return s1.current() + s2.current(); }, undefined);
  205. for (var i = 0; i < n; i++) {
  206. s1.next(i);
  207. }
  208. }
  209. function updateComputations4to1(n, sources) {
  210. var s1 = sources[0],
  211. s2 = sources[1],
  212. s3 = sources[2],
  213. s4 = sources[3],
  214. c = S.makeComputationNode(function () { return s1.current() + s2.current() + s3.current() + s4.current(); }, undefined);
  215. for (var i = 0; i < n; i++) {
  216. s1.next(i);
  217. }
  218. }
  219. function updateComputations1000to1(n, sources) {
  220. var s1 = sources[0],
  221. c = S.makeComputationNode(function () {
  222. var sum = 0;
  223. for (var i = 0; i < 1000; i++) {
  224. sum += sources[i].current();
  225. }
  226. return sum;
  227. }, undefined);
  228. for (var i = 0; i < n; i++) {
  229. s1.next(i);
  230. }
  231. }
  232. function updateComputations1to2(n, sources) {
  233. var s1 = sources[0],
  234. c1 = S.makeComputationNode(function () { return s1.current(); }, undefined),
  235. c2 = S.makeComputationNode(function () { return s1.current(); }, undefined);
  236. for (var i = 0; i < n / 2; i++) {
  237. s1.next(i);
  238. }
  239. }
  240. function updateComputations1to4(n, sources) {
  241. var s1 = sources[0],
  242. c1 = S.makeComputationNode(function () { return s1.current(); }, undefined),
  243. c2 = S.makeComputationNode(function () { return s1.current(); }, undefined),
  244. c3 = S.makeComputationNode(function () { return s1.current(); }, undefined),
  245. c4 = S.makeComputationNode(function () { return s1.current(); }, undefined);
  246. for (var i = 0; i < n / 4; i++) {
  247. s1.next(i);
  248. }
  249. }
  250. function updateComputations1to1000(n, sources) {
  251. var s1 = sources[0];
  252. for (var i = 0; i < 1000; i++) {
  253. S.makeComputationNode(function () { return s1.current(); }, undefined);
  254. }
  255. for (var i = 0; i < n / 1000; i++) {
  256. s1.next(i);
  257. }
  258. }
  259. function browserNow() {
  260. return performance.now();
  261. }
  262. function nodeNow() {
  263. var hrt = process.hrtime();
  264. return hrt[0] * 1000 + hrt[1] / 1e6;
  265. }
  266. function repeat(n, val) {
  267. var arr = [];
  268. for (var i = 0; i < n; i++) {
  269. arr[i] = val;
  270. }
  271. return arr;
  272. }