Skip to content

Instantly share code, notes, and snippets.

@jennifersmith
Created June 28, 2011 08:54
Show Gist options
  • Save jennifersmith/1050765 to your computer and use it in GitHub Desktop.
Save jennifersmith/1050765 to your computer and use it in GitHub Desktop.
Comparison of long string generation
require 'benchmark'
lengths = [10, 100 , 1000, 10000, 100000, 1000000]
Benchmark.bm do |x|
lengths.each do |length|
x.report("Length = #{length}\t\t\t") {(0...length.to_i).map{ ('a'..'z').to_a[rand(26)] }}
x.report("Same without rand = #{length}\t\t\t") {(0...length.to_i).map{ ('a'..'z').to_a[1] }}
x.report("Fixed characters Length = #{length}\t\t\t") {"x" * length}
end
end
@jennifersmith
Copy link
Author

for 1000000 characters random takes 24 seconds on my machine!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment