Skip to content

Instantly share code, notes, and snippets.

@heycarsten
Created August 19, 2011 00:02
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 heycarsten/1155608 to your computer and use it in GitHub Desktop.
Save heycarsten/1155608 to your computer and use it in GitHub Desktop.
Benchmarking generating fairly unique tokens quickly without external dependencies
require 'benchmark'
Benchmark.bmbm do |x|
x.report('printf') do
for i in 1..1_000_000
3.times.map { '%06x' % rand(0x100000) }.join
end
end
x.report('to_s') do
for i in 1..1_000_000
3.times.map { rand(0x100000).to_s(36) }.join
end
end
end
# Rehearsal ------------------------------------------
# printf 8.580000 0.020000 8.600000 ( 8.597337)
# to_s 3.010000 0.000000 3.010000 ( 3.019717)
# -------------------------------- total: 11.610000sec
#
# user system total real
# printf 8.560000 0.020000 8.580000 ( 8.584776)
# to_s 3.040000 0.000000 3.040000 ( 3.039551)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment