Skip to content

Instantly share code, notes, and snippets.

@presidentbeef
Created May 24, 2013 06:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save presidentbeef/b9a7e754a5b5e7a9018f to your computer and use it in GitHub Desktop.
Save presidentbeef/b9a7e754a5b5e7a9018f to your computer and use it in GitHub Desktop.
Brat code (with LuaJIT) beats Ruby code (implemented in C) for first time evar?
In place shuffle of 1,000,000 item array over 10 runs. Brat code "precompiled" to Lua.
Brat | Ruby
------|------
Best .033s | .048s
Median .043s | .049s
Worst .046s | .052s
#* http://rosettacode.org/wiki/Knuth_shuffle
Shuffle an array (in-place)
*#
shuffle = { a |
(a.length - 1).to 1 { i |
random_index = random(0, i)
temp = a[i]
a[i] = a[random_index]
a[random_index] = temp
}
a
}
shuffle 1000000.of 1
# Fastest way to make a big array?
Array.new(1000000, 1).shuffle!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment