Created
March 30, 2013 04:25
-
-
Save jsimmons/5275363 to your computer and use it in GitHub Desktop.
Why you so slow JS???
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Interval = function(lower, upper) { | |
this.lower = lower; | |
this.upper = upper; | |
} | |
function interval_add(a, b) { | |
return new Interval(a.lower + b.lower, a.upper + b.upper); | |
} | |
function interval_sub(a, b) { | |
return new Interval(a.lower - b.lower, a.upper - b.upper); | |
} | |
function interval_mul(a, b) { | |
var lower = Math.min(a.lower * b.lower, a.lower * b.upper, a.upper * b.upper); | |
var upper = Math.max(a.lower * b.lower, a.lower * b.upper, a.upper * b.upper); | |
return new Interval(lower, upper); | |
} | |
var intervals = new Array(); | |
function indigo(constraints, variables) { | |
for(var i = 0; i < 20; i++) { | |
intervals[i] = new Interval(Math.random(), Math.random()); | |
} | |
var iv = new Interval(0,0); | |
for(var i = 0; i < 1e7; i++) { | |
for(var j = 0; j < 19; j++) { | |
iv = interval_add(iv, interval_mul(intervals[j], intervals[j + 1])); | |
} | |
} | |
console.log(iv.lower, iv.upper); | |
} | |
indigo() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local max = math.max | |
local min = math.min | |
local interval_mt | |
local interval_new | |
interval_mt = { | |
__index = interval_mt; | |
__add = function(a, b) | |
return interval_new(a.lower + b.lower, a.upper + b.upper) | |
end; | |
__sub = function(a, b) | |
return interval_new(a.lower - b.lower, a.upper - b.upper) | |
end; | |
__mul = function(a, b) | |
local lower = min(a.lower * b.lower, a.lower * b.upper, a.upper * b.upper) | |
local upper = max(a.lower * b.lower, a.lower * b.upper, a.upper * b.upper) | |
return interval_new(lower, upper) | |
end; | |
__div = function(a, b) | |
reciprocal = interval_new(1 / b.upper, 1 / a.upper) | |
return a * reciprocal | |
end; | |
} | |
interval_new = function(lower, upper) | |
return setmetatable({upper = upper; lower = lower}, interval_mt) | |
end | |
local intervals = {} | |
local function indigo(constraints, variables) | |
for i = 1, 20 do | |
intervals[i] = interval_new(math.random(), math.random()) | |
end | |
local iv = interval_new(0,0) | |
for ii = 1, 1e7 do | |
for i = 1, 19 do | |
iv = iv + intervals[i] * intervals[i + 1] | |
end | |
end | |
print(iv.lower, iv.upper) | |
end | |
indigo() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[josh@archprime ~]$ perf stat -r 5 node indigo.js | |
18821829.261309687 78935338.29054944 | |
21653322.733843245 73055958.5489864 | |
17679419.27493577 64168128.62364557 | |
7719459.937230684 86016052.58084385 | |
11796860.698572649 46822903.31803182 | |
Performance counter stats for 'node indigo.js' (5 runs): | |
19157.580691 task-clock # 1.002 CPUs utilized ( +- 0.36% ) | |
22,720 context-switches # 0.001 M/sec ( +- 2.22% ) | |
38 cpu-migrations # 0.002 K/sec ( +- 17.55% ) | |
3,461 page-faults # 0.181 K/sec ( +- 0.01% ) | |
69,308,982,775 cycles # 3.618 GHz ( +- 0.30% ) [83.33%] | |
9,341,780,073 stalled-cycles-frontend # 13.48% frontend cycles idle ( +- 1.48% ) [83.28%] | |
5,584,916,732 stalled-cycles-backend # 8.06% backend cycles idle ( +- 1.69% ) [66.70%] | |
162,788,891,026 instructions # 2.35 insns per cycle | |
# 0.06 stalled cycles per insn ( +- 0.07% ) [83.33%] | |
44,059,476,773 branches # 2299.846 M/sec ( +- 0.16% ) [83.31%] | |
147,080,377 branch-misses # 0.33% of all branches ( +- 2.19% ) [83.38%] | |
19.109933486 seconds time elapsed ( +- 0.40% ) | |
[josh@archprime ~]$ perf stat -r 5 luajit indigo.lua | |
18545291.446756 81362168.189728 | |
18545291.446756 81362168.189728 | |
18545291.446756 81362168.189728 | |
18545291.446756 81362168.189728 | |
18545291.446756 81362168.189728 | |
Performance counter stats for 'luajit indigo.lua' (5 runs): | |
3018.966034 task-clock # 0.996 CPUs utilized ( +- 0.17% ) | |
310 context-switches # 0.103 K/sec ( +- 0.13% ) | |
4 cpu-migrations # 0.001 K/sec ( +- 51.67% ) | |
331 page-faults # 0.110 K/sec ( +- 0.06% ) | |
11,127,199,502 cycles # 3.686 GHz ( +- 0.08% ) [83.28%] | |
2,745,288,236 stalled-cycles-frontend # 24.67% frontend cycles idle ( +- 0.30% ) [83.30%] | |
1,226,851,059 stalled-cycles-backend # 11.03% backend cycles idle ( +- 0.15% ) [66.69%] | |
25,684,644,759 instructions # 2.31 insns per cycle | |
# 0.11 stalled cycles per insn ( +- 0.03% ) [83.37%] | |
6,823,459,833 branches # 2260.198 M/sec ( +- 0.01% ) [83.38%] | |
5,885,114 branch-misses # 0.09% of all branches ( +- 0.18% ) [83.35%] | |
3.030054202 seconds time elapsed ( +- 0.16% ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment