diff --git a/Rakefile b/Rakefile index 2f0748e..afccabf 100644 --- a/Rakefile +++ b/Rakefile @@ -1,7 +1,8 @@ require 'bundler/gem_tasks' require 'yaml' +require 'benchmark' -task :convert do +file 'lib/data/US.yml' do codes = {} File.open('lib/data/US.txt').each_line do |line| array = line.split('\t') @@ -16,3 +17,21 @@ task :convert do file.write codes.to_yaml end end + +file 'lib/data/US.marshal' => 'lib/data/US.yml' do + File.write('lib/data/US.marshal', Marshal.dump(YAML.load(File.read('lib/data/US.yml')))) +end + +task :convert => ['lib/data/US.marshal', 'lib/data/US.yml'] + +task :benchmark => :convert do + Benchmark.bm(15) do |x| + x.report("YAML.load") do + YAML.load(File.read('lib/data/US.yml')) + end + + x.report("Marshal.load") do + Marshal.load(File.read('lib/data/US.marshal')) + end + end +end diff --git a/lib/data/US.marshal b/lib/data/US.marshal new file mode 100644 index 0000000..4d87638 Binary files /dev/null and b/lib/data/US.marshal differ diff --git a/lib/zip-codes.rb b/lib/zip-codes.rb index 1791d4f..20643aa 100644 --- a/lib/zip-codes.rb +++ b/lib/zip-codes.rb @@ -1,5 +1,3 @@ -require 'yaml' - module ZipCodes VERSION = '0.2.1' @@ -11,8 +9,8 @@ def identify(code) def db @db ||= begin this_file = File.expand_path(File.dirname(__FILE__)) - us_data = File.join(this_file, 'data', 'US.yml') - YAML.load(File.open(us_data)) + us_data = File.join(this_file, 'data', 'US.marshal') + Marshal.load(File.open(us_data)) end end