Skip to content

Instantly share code, notes, and snippets.

@triwav
Last active June 7, 2019 23:03
Show Gist options
  • Save triwav/ef3bfdf518b550f5cae6c7e9a5075021 to your computer and use it in GitHub Desktop.
Save triwav/ef3bfdf518b550f5cae6c7e9a5075021 to your computer and use it in GitHub Desktop.
Roku Performance Comparisons
function BenchmarkSuite() as Object
this = {
runs: 3
}
this.benchmark = sub(description as String, versions as Object, context as Object, iterations = 10000 as Integer, runs = 3 as Integer)
print
print "----- " description "(" iterations " iterations) -------------------------------------------"
t = CreateObject("roTimespan")
for r = 1 to runs
print "Run" r
fastest = invalid
secondFastest = invalid
for each version in versions
t.Mark()
for i=0 to iterations
version.func(context)
end for
version.duration = t.TotalMilliseconds()
print "" version.name " ====> took:" version.duration "ms"
if fastest = invalid or fastest.duration >= version.duration then
secondFastest = fastest
fastest = version
else if secondFastest = invalid or secondFastest.duration >= version.duration then
secondFastest = version
end if
end for
timeDiff = secondFastest.duration - fastest.duration
percent = timeDiff / secondFastest.duration * 100
print
print fastest.name " was the fastest by " timeDiff "ms " percent "%"
print
end for
end sub
this.benchmarkAll = sub()
sections = [
"Iteration"
"AssociativeArrayAccess"
"Operators"
]
for each section in sections
print "============================================"
print "Benchmarking " section
print "============================================"
print
m["benchmark" + section]()
print
end for
end sub
this.benchmarkIteration = sub()
context = []
for i = 0 to 1000
context.push(i.toStr())
end for
versions = [{
"name": "for"
"func": function(context)
searchFor = "200"
containsValue = false
for i = 0 to context.count() - 1
item = context[i]
if item = searchFor then
containsValue = true
end if
end for
return containsValue
end function
},{
"name": "for each"
"func": function(context)
searchFor = "200"
containsValue = false
for each item in context
if item = searchFor then
containsValue = true
end if
end for
return containsValue
end function
}]
m.benchmark("for vs foreach", versions, context, 1000, m.runs)
versions = [{
"name": "for"
"func": function(context)
searchFor = "200"
matchingIndex = -1
for i = 0 to context.count() - 1
item = context[i]
if item = searchFor then
matchingIndex = i
end if
end for
return matchingIndex
end function
}, {
"name": "for each"
"func": function(context)
searchFor = "200"
matchingIndex = -1
i = 0
for each item in context
if item = searchFor then
matchingIndex = i
end if
i++
end for
return matchingIndex
end function
}]
m.benchmark("for vs for each when index is needed (1k array)", versions, context, 1000, m.runs)
versions = [{
"name": "reversed foreach"
"func": function(context)
t = 0
for i = context.count() - 1 to 0 step -1
t++
end for
return t
end function
}, {
"name": "for step -1"
"func": function(context)
t = 0
context.reverse()
for each item in context
t++
end for
return t
end function
}]
m.benchmark("reverse iteration", versions, context, 1000, m.runs)
end sub
this.benchmarkAssociativeArrayAccess = sub()
context = {
"levelOne": {
"levelTwo": {
"one": 1
"two": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque id quam et nisl sodales porta a sed massa. Sed pharetra, felis eu suscipit bibendum, justo justo egestas metus, sit amet pulvinar metus lorem nec tortor. Aenean aliquet sodales massa. Aliquam a elementum augue. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas pulvinar pellentesque purus, at ullamcorper orci sodales consectetur. Phasellus euismod dolor interdum dolor condimentum condimentum. Donec sem odio, pellentesque sodales diam sit amet, fringilla lacinia nibh. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Quisque rhoncus, tortor eget rhoncus posuere, tellus diam sollicitudin ex, ut rutrum nibh nisi a ante. Fusce convallis nisl at nunc ultricies accumsan. Aliquam vel dui tellus. Quisque pretium eros vel euismod euismod. Pellentesque nec quam lacus. Donec tincidunt quam sed quam molestie mattis. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas."
"three": [
0
1
2
3
]
}
}
}
versions = [{
"name": "dot"
"func": function(context)
total = 0
total += context.levelOne.levelTwo.one
total += context.levelOne.levelTwo.two.len()
total += context.levelOne.levelTwo.three.count()
return total
end function
}, {
"name":"[]"
"func": function(context)
total = 0
total += context["levelOne"]["levelTwo"]["one"]
total += context["levelOne"]["levelTwo"]["two"].len()
total += context["levelOne"]["levelTwo"]["three"].count()
return total
end function
}]
m.benchmark("AA read options", versions, context, 50000, m.runs)
versions = [{
"name": "dot write access"
"func": function(context)
aa = {
"levelOne": {
"levelTwo": {
}
}
}
aa.levelOne.levelTwo.one = 1
aa.levelOne.levelTwo.two = 2
aa.levelOne.levelTwo.three = 3
aa.levelOne.levelTwo.four = 4
aa.levelOne.levelTwo.five = 5
aa.levelOne.levelTwo.six = 6
end function
}, {
"name":"[] write access"
"func": function(context)
aa = {
"levelOne": {
"levelTwo": {
}
}
}
aa["levelOne"]["levelTwo"]["one"] = 1
aa["levelOne"]["levelTwo"]["two"] = 2
aa["levelOne"]["levelTwo"]["three"] = 3
aa["levelOne"]["levelTwo"]["four"] = 4
aa["levelOne"]["levelTwo"]["five"] = 5
aa["levelOne"]["levelTwo"]["six"] = 6
end function
}]
m.benchmark("AA write options", versions, context, 50000, m.runs)
end sub
this.benchmarkOperators = sub()
versions = [{
"name": "i += 1"
"func": function(context)
i = 0
for _ = 0 to 1000
i += 1
end for
return i
end function
}, {
"name": "i++"
"func": function(context)
i = 0
for _ = 0 to 1000
i++
end for
return i
end function
}]
m.benchmark("plus one (1k times)", versions, invalid, 1000, m.runs)
versions = [{
"name": "i = i + x"
"func": function(context)
i = 0
for _ = 0 to 1000
i = i + 2
end for
return i
end function
}, {
"name": "i += 2"
"func": function(context)
i = 0
for _ = 0 to 1000
i += 2
end for
return i
end function
}]
m.benchmark("plus two (1k times)", versions, invalid, 1000, m.runs)
end sub
return this
end function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment