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 #find_with_order #4

Closed
khiav223577 opened this issue Feb 23, 2017 · 1 comment
Closed

Benchmark of #find_with_order #4

khiav223577 opened this issue Feb 23, 2017 · 1 comment

Comments

@khiav223577
Copy link
Owner

khiav223577 commented Feb 23, 2017

def test(ids)
  old_logger = ActiveRecord::Base.logger
  ActiveRecord::Base.logger = nil
  Benchmark.bm(32) do |x|
    x.report("Find with order"){ 
      User.find_with_order(ids) 
    }
    x.report("Find then sort by index"){ 
      User.where(id: ids).sort_by{|s| ids.index(s.id)} 
    }
    x.report("Find then sort by hash mapping"){ 
      mapping = ids.each_with_index.to_h  
      User.where(id: ids).sort_by{|s| mapping[s.id]}
    }
  end
  ActiveRecord::Base.logger = old_logger

  nil
end
test((1..10000).to_a.shuffle)
@khiav223577
Copy link
Owner Author

khiav223577 commented Jun 9, 2017

test with order_as_specified
P.S. order_as_specified cannot order with an array whose length is more than 2000 entries: https://stackoverflow.com/questions/42872737/getting-programlimitexceeded-error-from-order-as-specified-gem

def test(ids)
  old_logger = ActiveRecord::Base.logger
  ActiveRecord::Base.logger = nil
  Benchmark.bm(32) do |x|
    x.report("order_as_specified"){ 
      User.where(id: ids).order_as_specified(id: ids).pluck(:id)
    }
    x.report("where_with_order"){ 
      User.where_with_order(:id, ids).pluck(:id)
    }
  end
  ActiveRecord::Base.logger = old_logger

  nil
end
test((1..2000).to_a.shuffle)

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