Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

superfluous work in #ordered_map_for #154

Closed
audibleblink opened this issue Apr 22, 2015 · 0 comments · Fixed by #484
Closed

superfluous work in #ordered_map_for #154

audibleblink opened this issue Apr 22, 2015 · 0 comments · Fixed by #484

Comments

@audibleblink
Copy link

exploding zipped rows to flatten them is unnecessary since Hash[] takes a 2-d array

def ordered_map_for columns, row
  h = Hash[*columns.zip(row).flatten]
  row.each_with_index { |r, i| h[i] = r }
  h
end

def ordered_map_for2 columns, row
  h = Hash[columns.zip(row)]
  # columns.zip(row) {|k, v| h[k.to_sym] = v}
  row.each_with_index {|r, i| h[i]=r}
  h
end


require 'benchmark'

columns = %w(a b c d e f g h i j k l m n o p q r s)
row     = %w(1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9)

n = 100000
Benchmark.bm do |x|
  x.report("old ") { n.times do ; ordered_map_for columns, row;  end }
  x.report("new ") { n.times do ; ordered_map_for2 columns, row; end }
end
#######################
       user     system      total        real
old   2.730000   0.040000   2.770000 (  2.826577)
new   2.200000   0.030000   2.230000 (  2.062929)
[Finished in 5.3s]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants