I have 2 sets of data as following:
default_products - A set of active record objects retrieved from memcached owned_products - A set of active record objects retrieved from the database
Retrieving the data is fast and i get around 30-40 req/s when just returning either one of the arrays yet as soon as i do the following to return both:
all_products = default_products + owned_products
Performance plummets to around 3-4 req/s. Why is this operation so slow? Is there anoth开发者_开发百科er way to do this to speed things up?
The 'products' use STI to have subclasses such as 'Furniture' and 'Clothing' could this cause any performance problem?
Thanks
I'm guessing that using the '+' operator makes things slow because actually allocates a new object. I suppose this could also be the result of garbage collection being triggered. If you don't mind modifying one or the other array you can use the '<<' operator, which is really fast.
Here's how I measured the speed difference and illustrate the side effect of modifying the array.
require 'benchmark'
a, b = [], []
array_size = 1000
random_limit = 1000
array_size.times { a << rand; b << rand }
p "starting count and object_ids"
p a.count, a.object_id
p b.count, b.object_id
p "resulting object_ids"
p (a<<b).object_id
p (a + b).object_id
n = 50000
Benchmark.bm do |x|
x.report { n.times {a + b }}
x.report { n.times {a << b }}
end
p "notice that the count has changed from 1000 to 51000 for the '<<' method"
p a.count, a.object_id
p b.count, b.object_id
If you have no use for the old variables afterwards, you can use Array.concat
default_products.concat(owned_products)
That should be faster than adding the arrays together and returning the result as it doesn't have to create a new object.
See: http://www.ruby-doc.org/core-1.9.3/Array.html#method-i-concat
精彩评论