Skip to content

Instantly share code, notes, and snippets.

@be5invis
Last active October 13, 2015 14:57
Show Gist options
  • Save be5invis/4212730 to your computer and use it in GitHub Desktop.
Save be5invis/4212730 to your computer and use it in GitHub Desktop.
Javascript Engines DO optimize integer operations.
function bench1(n){
for(var i = 0; i < n; i++) ;
}
function bench2(n){
for(var i = 0.5; i < n; i++) ;
}
function bench3(n){
for(var i = Math.random(); i < n; i++) ;
}
function bench4(n){
for(var i = Math.random(), top = n + Math.random(); i < top; i++) ;
}
function bench5(n){
for(var i = 5000000000 + Math.floor(1 + Math.random() * 10), top = 5000000000 + n; i < top; i++)
}
function bench6(n){
for(var i = 5000000000 + Math.floor(1 + Math.random() * 10), top = 5000000000 + n + Math.floor(1 + Math.random() * 10); i < top; i++)
}
function bench7(n){
for(var i = 5000000000 + Math.random(), top = 5000000000 + n; i < top; i++)
}
function bench8(n){
for(var i = 5000000000 + Math.random(), top = 5000000000 + n + Math.random(); i < top; i++)
}
def measureTime(args, f) =
var multiplier = 1
var estimatedTime = time args, f
while(estimatedTime < 100)
if(estimatedTime < 1)
multiplier *= 110
else
multiplier *= (100 / estimatedTime + 10)
estimatedTime = time args, { for(i <- 1...multiplier) f.apply null, arguments }
estimatedTime / multiplier
def bench1(n) =
var i = 0; var top = n; while(i < top) i += 1
def bench2(n) =
var i = 0.5; var top = n; while(i < top) i += 1
def bench3(n) =
var i = math.random(); var top = n
while(i < top) i += 1
def bench4(n) =
var i = math.random(); var top = n + math.random()
while(i < top) i += 1
def bench5(n) =
var i = 5000000000 + math.randInt(1, 10)
var top = 5000000000 + n
while(i < top) i += 1
def bench6(n) =
var i = 5000000000 + math.randInt(1, 10)
var top = 5000000000 + n + math.randInt(1, 10)
while(i < top) i += 1
def bench7(n) =
var i = 5000000000 + math.random()
var top = 5000000000 + n
while(i < top) i += 1
def bench8(n) =
var i = 5000000000 + math.random()
var top = 5000000000 + n + math.random()
while(i < top) i += 1
def benchmarks = [bench1, bench2, bench3, bench4, bench5, bench6, bench7, bench8]
start async :>
for(() <- 1...1000)
var n = math.randInt 1000000, 10000000
[n].concat benchmarks.map ((m) => math.round((measureTime [n], m) * 1000)) |.join "\t" |trace
sleep ! 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment