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

Optimization and Fixes #15

Merged
merged 1 commit into from
Jun 10, 2016
Merged

Optimization and Fixes #15

merged 1 commit into from
Jun 10, 2016

Conversation

akodkod
Copy link
Contributor

@akodkod akodkod commented May 27, 2016

Hi Girish,

I made some changes for you gem, if you do not against.

  1. Little bit optimized the speed of methods pluck_h and pluck_s
    1.1 Moved keys.size == 1 from loop
    1.2 Keys was converted to string because HashWithIndifferentAccess always convert keys into string inside self
  2. Fixed some RSpec warnings.
  3. Changed behaviour of returning value if block given. For example:
result = Todo.pluck_h(:id) do |todo|
  "TODO ID: #{todo[:id]}"
end

# result = ["TODO ID: 1", "TODO ID: 1", "TODO ID: 1"]
# Instead of [{ id: 1 }, { id: 2 }, { id: 3 }]

On my mind, it's more flexible behaviour.

Benchmarks

Main benchmark code (o - means optimized).

require 'benchmark/ips'

require_relative 'config/environment'


Benchmark.ips do |x|  
  COUNT = 100000
  x.config(time: 10, warmup: 5)


 

  x.report('pluck_to_hash') { Todo.limit(COUNT).pluck_to_hash(:id) }
  x.report('o_pluck_to_hash') { Todo.limit(COUNT).o_pluck_to_hash(:id) }



  x.compare!
end
Warming up --------------------------------------
       pluck_to_hash     1.000  i/100ms
     o_pluck_to_hash     1.000  i/100ms
Calculating -------------------------------------
       pluck_to_hash      1.773  (± 0.0%) i/s -     18.000  in  10.162897s
     o_pluck_to_hash      2.013  (± 0.0%) i/s -     21.000  in  10.439589s

Comparison:
     o_pluck_to_hash:        2.0 i/s
       pluck_to_hash:        1.8 i/s - 1.14x slower

Pluck with lot of keys

x.report('pluck_to_hash') { Todo.limit(COUNT).pluck_to_hash(:id, :user_id, :text, :done, :created_at, :updated_at) }
x.report('o_pluck_to_hash') { Todo.limit(COUNT).o_pluck_to_hash(:id, :user_id, :text, :done, :created_at, :updated_at) }
Warming up --------------------------------------
       pluck_to_hash     1.000  i/100ms
     o_pluck_to_hash     1.000  i/100ms
Calculating -------------------------------------
       pluck_to_hash      0.117  (± 0.0%) i/s -      2.000  in  17.073606s
     o_pluck_to_hash      0.124  (± 0.0%) i/s -      2.000  in  16.182610s

Comparison:
     o_pluck_to_hash:        0.1 i/s
       pluck_to_hash:        0.1 i/s - 1.06x slower

Pluck to Struct

x.report('pluck_to_struct') { Todo.limit(COUNT).pluck_to_struct }
x.report('o_pluck_to_struct') { Todo.limit(COUNT).o_pluck_to_struct }
Warming up --------------------------------------
     pluck_to_struct     1.000  i/100ms
   o_pluck_to_struct     1.000  i/100ms
Calculating -------------------------------------
     pluck_to_struct      0.124  (± 0.0%) i/s -      2.000  in  16.192719s
   o_pluck_to_struct      0.129  (± 0.0%) i/s -      2.000  in  15.466436s

Comparison:
   o_pluck_to_struct:        0.1 i/s
     pluck_to_struct:        0.1 i/s - 1.05x slower

@girishso girishso merged commit 67116b3 into girishso:master Jun 10, 2016
girishso added a commit that referenced this pull request Jun 10, 2016
@girishso
Copy link
Owner

Merged. Thanks @MrEmelianenko for good work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants