Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save basicxman/1130045 to your computer and use it in GitHub Desktop.

Select an option

Save basicxman/1130045 to your computer and use it in GitHub Desktop.
$ ruby weather_split_vs_scan_benchmarks.rb
user system total real
Using split 0.320000 0.010000 0.330000 ( 0.330723)
Using scan 0.230000 0.020000 0.250000 ( 0.239557)
#!/usr/bin/env ruby
require 'benchmark'
Benchmark.bm do |x|
x.report "Using split" do
1000.times do
File.read("weather.dat").each_line do |line|
args = line.split(/\s+/)
next unless args.length == 16
end
end
end
x.report "Using scan" do
1000.times do
File.read("weather.dat").each_line do |line|
day, min_temp = line.scan(/\s+([0-9]+)\s+[0-9]+\s+([0-9]{2}).*/).first
next if day.nil?
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment