Skip to content

Instantly share code, notes, and snippets.

@leafo
Last active August 29, 2015 13:56
Show Gist options
  • Save leafo/8838035 to your computer and use it in GitHub Desktop.
Save leafo/8838035 to your computer and use it in GitHub Desktop.
socket = require "socket"
bm_mix = (tests, iterations=100, ...) ->
totals = {k, 0 for k in pairs tests}
print "Running with", ...
for i=1,iterations
for test, fn in pairs tests
start = socket.gettime!
fn ...
elapsed = socket.gettime! - start
totals[test] += elapsed
times = [{test, time} for test, time in pairs totals]
table.sort times, (a, b) -> a[2] < b[2]
for {test, time} in *times
print "`#{test}` elapsed: #{time}"
import insert, concat from table
bm_mix {
"concat": (count) ->
buffer = ""
for i=1,count
buffer ..= "hello"
"#len operator": (count) ->
buffer = {}
for i=1,count
buffer[#buffer + 1] = "hello"
concat buffer
"local length": (count) ->
buffer = {}
idx = 0
for i=1,count
idx += 1
buffer[idx] = "hello"
concat buffer
"length in buffer": (count) ->
buffer = { i: 0 }
for i=1,count
idx = buffer.i + 1
buffer.i = idx
buffer[idx] = "hello"
concat buffer
}, 100, 10000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment