I am trying to benchmark the below code and see that the non-threaded version runs faster than the treaded version.
puts Benchmark.measure{
100.times do |i|
carrier.services.map do |service_obj|
Thread.new {
rate << Rate.rate_by_service(service_obj,@package,@from,@to,@credentials)
}
end.each(&:join)
end
}
below are the timings
#threading
4.940000 0.730000 5.670000 ( 5.795008)
4.740000 0.740000 5.480000 ( 5.554500)
4.740000 0.730000 5.470000 ( 5.436129)
4.840000 0.710000 5.550000 ( 5.524418)
4.710000 0.720000 5.430000 ( 5.431673)
#no threading
3.640000 0.190000 3.830000 ( 3.962347)
3.670000 0.220000 3.890000 ( 4.402259)
3.430000 0.200000 3.630000 ( 3.780768)
3.480000 0.190000 3.670000 ( 3.830547)
3.650000 0.210000 3.860000 ( 4.065442)
one thing i have observed in the log is when non-threaded version runs, the queries results are returned from cache like below and in threaded version it does not fetch from cache but accessed the DB.
CACHE (0.0ms) SELECT
CACHE (0.0ms) SELECT
CACHE (0.0ms) SELECT
CACHE (0.0ms) SELECT
CACHE (0.0ms) SELECT
CACHE (0.0ms) SELECT
CACHE (0.0ms) SELECT
CACHE (0.0ms) SELECT
CACHE (0.0ms) SELECT
CACHE (0.0ms) SELECT
CACHE (0.0ms) SELECT
Note: I have removed the actual queries for readability purpose.
Can anyone please explain as to what could be causing the threaded version to run a bit slow than non-threaded