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

Benchmark of #pluck_all #18

Closed
khiav223577 opened this issue Aug 28, 2017 · 1 comment
Closed

Benchmark of #pluck_all #18

khiav223577 opened this issue Aug 28, 2017 · 1 comment

Comments

@khiav223577
Copy link
Owner

khiav223577 commented Aug 28, 2017

Benchmark

def test()
  old_logger = ActiveRecord::Base.logger
  ActiveRecord::Base.logger = nil
  Benchmark.bm(32) do |x|
    x.report('map'){ 
      User.where('').map do |user|
        {
          'account'  => user.account,
          'email'    => user.email,
          'realname' => user.realname,
        }
      end
    }
    x.report('select + map'){ 
      User.select(:account, :email, :realname).map do |user|
        {
          'account'  => user.account,
          'email'    => user.email,
          'realname' => user.realname,
        }
      end
    }
    x.report('select + as_json'){ 
      User.select(:account, :email, :realname).as_json(only: [:account, :email, :realname])
    }
    x.report('pluck_all'){ 
      User.pluck_all(:account, :email, :realname)
    }
  end
  ActiveRecord::Base.logger = old_logger

  nil
end
test()

Benchmark-ips

require 'benchmark/ips'
def test()
  old_logger = ActiveRecord::Base.logger
  ActiveRecord::Base.logger = nil

  Benchmark.ips do |x|
    x.report('map'){ 
      User.limit(100).map do |user|
        {
          'account'  => user.account,
          'email'    => user.email,
          'realname' => user.realname,
        }
      end
    }
    x.report('select + map'){ 
      User.limit(100).select(:account, :email, :realname).map do |user|
        {
          'account'  => user.account,
          'email'    => user.email,
          'realname' => user.realname,
        }
      end
    }
    x.report('select + as_json'){ 
      User.limit(100).select(:account, :email, :realname).as_json(only: [:account, :email, :realname])
    }
    x.report('pluck_all'){ 
      User.limit(100).pluck_all(:account, :email, :realname)
    }
    x.compare!
  end
  ActiveRecord::Base.logger = old_logger

  nil
end
test()
@khiav223577
Copy link
Owner Author

khiav223577 commented Aug 28, 2017

compare with pluck_to_hash 1.0.2

Benchmark

def test()
  old_logger = ActiveRecord::Base.logger
  ActiveRecord::Base.logger = nil
  Benchmark.bm(32) do |x|
    x.report('pluck_to_hash'){ 
      User.pluck_to_hash(:account, :email, :realname)
    }
    x.report('pluck_all'){ 
      User.pluck_all(:account, :email, :realname)
    }
  end
  ActiveRecord::Base.logger = old_logger

  nil
end
test()

Benchmark-ips

require 'benchmark/ips'
def test()
  old_logger = ActiveRecord::Base.logger
  ActiveRecord::Base.logger = nil

  Benchmark.ips do |x|
    x.report('pluck_to_hash'){ User.limit(1000).pluck_to_hash(:account, :email, :realname) }
    x.report('pluck_all'){ User.limit(1000).pluck_all(:account, :email, :realname) }
    x.compare!
  end
  ActiveRecord::Base.logger = old_logger

  nil
end
test()

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

No branches or pull requests

1 participant