Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Rubyコードの最適化

687 views

Published on

第77回 Ruby関西 勉強会 でプレゼンした資料です。

Published in: Engineering
  • Login to see the comments

  • Be the first to like this

Rubyコードの最適化

  1. 1. FNAME = "KEN_ALL.CSV" count = [] File.open(FNAME, "r:SJIS:UTF-8") do |f| while line = f.gets next unless line =~ /[ ]/ c = count.find{|a| a.first == Regexp.last_match[0] } unless c c = [Regexp.last_match[0], []] count.push c end c[1] << line end end p count .sort{|a, b| a.last.length <=> b.last.length } .map{|k, v| [k, v.length] } #=> [[" ", 8079], [" ", 8392], [" ", 11271], [" ", 15017]]
  2. 2. FNAME = "KEN_ALL.CSV" count = [] File.open(FNAME, "r:SJIS:UTF-8") do |f| while line = f.gets next unless line =~ /[ ]/ c = count.find{|a| a.first == Regexp.last_match[0] } unless c c = [Regexp.last_match[0], []] count.push c end c[1] << line end end p count .sort{|a, b| a.last.length <=> b.last.length } .map{|k, v| [k, v.length] } #=>[[" ", 8079], [" ", 8392], [" ", 11271], [" ", 15017]]
  3. 3. FNAME = "KEN_ALL.CSV" count = [] body = nil File.read(FNAME, “r:SJIS:UTF-8"){|f| body = f.read end body.each_line do |line| next unless line =~ /[ ]/ c = count.find{|a| a.first == Regexp.last_match[0] } unless c c = [Regexp.last_match[0], []] count.push c end c[1] << line end p count .sort{|a, b| a.last.length <=> b.last.length } .map{|k, v| [k, v.length] } #=>[[" ", 8079], [" ", 8392], [" ", 11271], [" ",
  4. 4. FNAME = "KEN_ALL.CSV" count = [] body = nil File.read(FNAME, “r:SJIS:UTF-8"){|f| body = f.read end body.each_line do |line| next unless line =~ /[ ]/ c = count.find{|a| a.first == Regexp.last_match[0] } unless c c = [Regexp.last_match[0], []] count.push c end c[1] << line end p count .sort{|a, b| a.last.length <=> b.last.length } .map{|k, v| [k, v.length] } #=>[[" ", 8079], [" ", 8392], [" ", 11271], [" ", 15017]]
  5. 5. FNAME = "KEN_ALL.CSV" count = {} body = nil File.open(FNAME, "r:SJIS:UTF-8") do |f| body = f.read end body.each_line do |line| next unless line =~ /[ ]/ count[Regexp.last_match[0]] ||= [] count[Regexp.last_match[0]] << line end p count .sort{|a, b| a.last.length <=> b.last.length } .map{|k, v| [k, v.length] } #=>[[" ", 8079], [" ", 8392], [" ", 11271], [" ", 15017]]
  6. 6. FNAME = "KEN_ALL.CSV" count = {} body = nil File.open(FNAME, "r:SJIS:UTF-8") do |f| body = f.read end body.each_line do |line| next unless line =~ /[ ]/ count[Regexp.last_match[0]] ||= [] count[Regexp.last_match[0]] << line end p count .sort{|a, b| a.last.length <=> b.last.length } .map{|k, v| [k, v.length] } #=>[[" ", 8079], [" ", 8392], [" ", 11271], [" ", 15017]]
  7. 7. FNAME = "KEN_ALL.CSV" count = {} body = nil File.open(FNAME, "r:SJIS:UTF-8") do |f| body = f.read end body.each_line do |line| next unless line =~ /[ ]/ count[Regexp.last_match[0]] ||= 0 count[Regexp.last_match[0]] += 1 end p count .sort{|a, b| a.last <=> b.last }
  8. 8. FNAME = "KEN_ALL.CSV" count = {} body = nil File.open(FNAME, "r:SJIS:UTF-8") do |f| body = f.read end body.each_line do |line| next unless line =~ /[ ]/ count[Regexp.last_match[0]] ||= 0 count[Regexp.last_match[0]] += 1 end p count .sort{|a, b| a.last <=> b.last }
  9. 9. FNAME = "KEN_ALL.CSV" count = {} body = nil File.open(FNAME, "r:SJIS:UTF-8") do |f| body = f.read end reg = /[ ]/ body.each_line do |line| next unless line =~ reg count[Regexp.last_match[0]] ||= 0 count[Regexp.last_match[0]] += 1 end p count .sort{|a, b| a.last <=> b.last }
  10. 10. FNAME = "KEN_ALL.CSV" count = {} body = nil File.open(FNAME, "r:SJIS:UTF-8") do |f| body = f.read end reg = /[ ]/ body.each_line do |line| next unless line =~ reg count[Regexp.last_match[0]] ||= 0 count[Regexp.last_match[0]] += 1 end p count .sort{|a, b| a.last <=> b.last }
  11. 11. FNAME = "KEN_ALL.CSV" count = {} body = nil File.open(FNAME, "r:SJIS:UTF-8") do |f| body = f.read end reg = /[ ]/ body.each_line do |line| next unless line =~ reg k = Regexp.last_match[0] count[k] ||= 0 count[k] += 1 end p count .sort{|a, b| a.last <=> b.last }
  12. 12. FNAME = "KEN_ALL.CSV" count = {} body = nil File.open(FNAME, "r:SJIS:UTF-8") do |f| body = f.read end reg = /[ ]/ body.each_line do |line| next unless line =~ reg k = Regexp.last_match[0] count[k] ||= 0 count[k] += 1 end p count .sort{|a, b| a.last <=> b.last }
  13. 13. FNAME = "KEN_ALL.CSV" count = {” ” => 0} count = Hash.new { 0 } body = nil File.open(FNAME, "r:SJIS:UTF-8") do |f| body = f.read end reg = /[ ]/ body.each_line do |line| next unless line =~ reg k = Regexp.last_match[0] count[k] ||= 0 count[k] += 1 end p count.sort_by{|k, v| v }

×