-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathbenchmark2.js
More file actions
executable file
·137 lines (117 loc) · 2.58 KB
/
benchmark2.js
File metadata and controls
executable file
·137 lines (117 loc) · 2.58 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/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([
'cflib/tailcall',
'webtoolkit/sprintf'
], function(
TailCall,
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 arr = [], i;
for (i=0; i<20; i++) {
arr.push(i);
}
suite.add('loop', function(){
var acc = 0, i;
for (i=0; i<arr.length; i++) {
acc += arr[i];
}
}, {onComplete: report});
suite.add('empty', function(){
'empty';
}, {onComplete: report});
suite.add('typeof function overhead', function(){
!!(typeof report === "function");
}, {onComplete: report});
suite.add('tailcall function', function(){
function loop(){
var i = 0, acc = 0;
function body(){
var item = arr[i++];
if (item === undefined) {
return acc;
}
acc += item;
return body;
}
return body;
}
var next = loop;
while (typeof next === "function") {
next = next();
}
}, {onComplete: report});
suite.add('tailcall function, 1 less', function(){
function loop(){
var i = 0, acc = 0;
return function body(){
var item = arr[i++];
acc += item;
if (i === arr.length) {
return acc;
}
return body;
};
}
var next = loop;
while (typeof next === "function") {
next = next();
}
}, {onComplete: report});
suite.add('tailcall function shift', function(){
function loop(){
var acc = 0, remaining = arr.slice();
function body(){
var item = remaining.shift();
if (item === undefined) {
return acc;
}
acc += item;
return body;
}
return body;
}
var next = loop;
while (typeof next === "function") {
next = next();
}
}, {onComplete: report});
suite.add('TailCall trampoline', function(){
function loop(){
var i=0, acc=0;
function body(){
var item = arr[i++];
if (item === undefined) {
return acc;
}
acc += item;
return new TailCall(body);
}
return new TailCall(body);
}
var res = loop();
while (res instanceof TailCall) {
res = res.invoke();
}
}, {onComplete: report});
suite.run();
});