-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbenchmark5.js
executable file
·106 lines (96 loc) · 2.18 KB
/
benchmark5.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env node
/*jslint white: true, sloppy: true, plusplus: true */
var Benchmark = require('benchmark');
var suite = new Benchmark.Suite('tcl', {});
var requirejs = require('requirejs');
requirejs.config({
baseUrl: '/Users/cyan/git/js',
paths: {
tcl: 'tcl/amd',
sop: 'tcl/amd',
cfcrypto: 'crypto/src/amd',
webtoolkit: '3rd_party/webtoolkit',
ds: 'datasource/amd',
cflib: 'cflib/amd'
},
nodeRequire: require
});
requirejs([
'tcl/utils',
'webtoolkit/sprintf'
], function(
utils,
sprintf
){
function report(result){
var usec = 1000000.0/result.currentTarget.hz;
console.log(sprintf('%25s: %15.5f / sec ± %f, usec/it: %.4f', result.currentTarget.name, result.currentTarget.hz, result.currentTarget.stats.deviation, usec));
}
var l = [], i, pointless = 0;
for (i=0; i<30; i++) {
l.push(i);
}
function tail_recursive(){
var res;
res = function sum() {
var i = 0, acc = 0;
return function loop(){
var next = l[i++];
if (next === undefined) {return acc;}
acc += next;
if (i % 3 === 0) {
pointless++;
}
return loop;
};
};
do {res = res();} while (typeof res === "function");
return res;
}
function while_loop(){
var res;
res = function sum() {
var i = 0, acc = 0, next;
while (i<l.length) {
next = l[i++];
acc += next;
if (i % 3 === 0) {
pointless++;
}
}
return acc;
};
do {res = res();} while (typeof res === "function");
return res;
}
function interuptable(){
var res;
res = function sum() {
var i = 0, acc = 0, next;
return function loop(){
while (i<l.length) {
next = l[i++];
acc += next;
if (i % 3 === 0) {
pointless++;
return loop;
}
}
return acc;
}
};
do {res = res();} while (typeof res === "function");
return res;
}
console.log('tail_recursive: '+tail_recursive()+', while_loop: '+while_loop()+', interuptable: '+interuptable());
suite.add('pure tail recursive loop', function(){
tail_recursive();
}, {onComplete: report});
suite.add('pure loop', function(){
while_loop();
}, {onComplete: report});
suite.add('interuptable loop', function(){
interuptable();
}, {onComplete: report});
suite.run();
});