Skip to content

Instantly share code, notes, and snippets.

@koseki
Created November 2, 2009 06:17
Show Gist options
  • Save koseki/223995 to your computer and use it in GitHub Desktop.
Save koseki/223995 to your computer and use it in GitHub Desktop.
#! /usr/bin/ruby
require 'benchmark'
require 'yaml'
open("data1.txt", "w") do |io|
5000.times do |i|
io << "abcdefghijklmnopqrstuvwxyz\t#{i}\n"
end
end
open("data1.yml", "w") do |io|
5000.times do |i|
io << "- - abcdefghijklmnopqrstuvwxyz\n - #{i}\n"
end
end
open("data2.yml", "w") do |io|
5000.times do |i|
io << "- key: abcdefghijklmnopqrstuvwxyz\n value: #{i}\n"
end
end
data = []
5000.times do |i|
data << {"key" => "abcdefghijklmnopqrstuvwxyz", "value" => i}
end
open("data1.obj", "w") do |io|
Marshal.dump(data, io)
end
def load_txt
data = []
open("data1.txt") do |io|
io.each_line do |line|
tmp = line.chomp.split(/\t/)
tmp[1] = tmp[1].to_i
data << tmp
end
end
end
# hash
def load_txt2
data = []
open("data1.txt") do |io|
io.each_line do |line|
tmp = line.chomp.split(/\t/)
tmp[1] = tmp[1].to_i
data << {"key" => tmp[0], "value" => tmp[1]}
end
end
end
def load_yml
data = YAML.load_file("data1.yml")
end
def load_yml2
data = YAML.load_file("data2.yml")
end
def load_marshal
open("data1.obj") do |io|
data = Marshal.load(io)
end
end
n = 10
Benchmark.bm do |x|
x.report("txt a") { n.times do; load_txt; end }
x.report("txt h") { n.times do; load_txt2; end }
x.report("yml a") { n.times do; load_yml; end }
x.report("yml h") { n.times do; load_yml2; end }
x.report("msl h") { n.times do; load_marshal; end }
end
user system total real
txt a 0.290000 0.010000 0.300000 ( 0.307217)
txt h 0.390000 0.010000 0.400000 ( 0.419969)
yml a 0.440000 0.010000 0.450000 ( 0.449777)
yml h 0.830000 0.010000 0.840000 ( 0.869857)
msl h 0.220000 0.010000 0.230000 ( 0.226806)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment