Created
June 21, 2011 18:02
-
-
Save agmcleod/1038463 to your computer and use it in GitHub Desktop.
parse_zips.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# original version, took approximatley 27 minutes. | |
lines = [] | |
File.open('zips-in.txt', 'r+') do |f| | |
f.each_line { |l| lines << l unless lines.include? l } | |
end | |
File.open('zips-out.csv', 'w+') do |f| | |
lines.each do |l| | |
f.write "#{l.split(',').join(' - ')},#{l}" | |
end | |
end | |
# Updated code, also using 1.9.2 | |
require 'benchmark' | |
Benchmark.bm(1) do |b| | |
b.report('test') do | |
lines = [] | |
File.open('zips-in.txt', 'r+') do |f| | |
f.each_line { |l| lines << l } | |
end | |
File.open('zips-out.csv', 'w+') do |f| | |
lines.uniq.each do |l| | |
f.write "#{l.gsub(',',' - ').gsub(/\n/, '')},#{l}" | |
end | |
end | |
end | |
end | |
# user system total real | |
# test 1.960000 0.140000 2.100000 ( 2.122407) | |
# jruby 1.6.2 | |
# user system total real | |
# test 2.653000 0.000000 2.653000 ( 2.395000) | |
# rubinius 2.0.0pre | |
# user system total real | |
# test 7.756975 0.138513 7.895488 ( 8.820250) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
After putting this up, I know I can easily replace the split and join with gsub. Do'h