Skip to content

Instantly share code, notes, and snippets.

@kaityo256
Created September 15, 2017 12:52
Show Gist options
  • Save kaityo256/f1154b0e4d66a9c4556a34af98a2b8f4 to your computer and use it in GitHub Desktop.
Save kaityo256/f1154b0e4d66a9c4556a34af98a2b8f4 to your computer and use it in GitHub Desktop.
Split spring to array
require 'benchmark'
def use_split(input)
input.each do |s|
a = s.split(//)
end
end
def use_each_char(input)
input.each do |s|
a = s.each_char.to_a
end
end
srand(1)
input = Array.new(100) do
s = ""
10000.times do
s << (rand*10).to_i.to_s
end
s
end
r1 = Benchmark.realtime do
use_split(input)
end
r2 = Benchmark.realtime do
use_each_char(input)
end
puts "split #{r1} [sec]" # => split 0.26577499999984866 [sec]
puts "each_char #{r2} [sec]" # => each_char 0.08966800000052899 [sec]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment