Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7813503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:53:26+00:00 2026-06-02T04:53:26+00:00

Do you have a suggestion on how to test performances over two different mongoid/mongodb

  • 0

Do you have a suggestion on how to test performances over two different mongoid/mongodb query implementations?

The implementation to compare, are related to a previous “Q & A” but I’ll report those here again, for brevity :

first code :

Competitor  = Struct.new(:html_url, :description, :user)
competitors = []

User.all.map do |user|
  user.watchlists.all.map do |wl|
    if wl.tags_array == ["ruby", "web", "framework"]
        competitors << Competitor.new(wl.html_url, wl.description, wl.user.nickname)
    end
  end
end

vs. second code :

Competitor = Struct.new(:html_url, :description, :user)
competitors = []

User.where('watchlists.tags_array' => %w[ruby web framework]).only(:nickname, :watchlists).each do |u|
  u.watchlists.where(:tags_array => %w[ruby web framework]).each do |wl|
    competitors << Competitor.new(wl.html_url, wl.description, u.nickname)
  end
end

and for completness,

the underline datamodel is :

class User
  include Mongoid::Document

  field :nickname
  embeds_many :watchlists
end

class Watchlist
 include Mongoid::Document

 field :html_url
 field :description
 field :tags_array, type: Array
 embedded_in :user
end

What’s the current way for benchmarking & comparing performances on that two codes ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T04:53:28+00:00Added an answer on June 2, 2026 at 4:53 am

    Until now, I found this way but if you know something better please answer too …

    first code :

    1.9.2p290 :038 >   Competitor  = Struct.new(:html_url, :description, :user)
    1.9.2p290 :039 >   time = Benchmark.measure do
    1.9.2p290 :040 >     competitors = []
    1.9.2p290 :041?>   
    1.9.2p290 :042 >     User.all.map do |user|
    1.9.2p290 :043 >         user.watchlists.all.map do |wl|
    1.9.2p290 :044 >             if wl.tags_array == ["ruby", "web", "framework"]
    1.9.2p290 :045?>                 competitors << Competitor.new(wl.html_url, wl.description, wl.user.nickname)
    1.9.2p290 :046?>             end
    1.9.2p290 :047?>         end
    1.9.2p290 :048?>     end
    1.9.2p290 :049?>   end
    MONGODB (34ms) heroku_app1707530['users'].find({})
    MONGODB [DEBUG] cursor.refresh() for cursor 2492208785546818168
    MONGODB [DEBUG] cursor.refresh() for cursor 2492208785546818168
     =>   1.390000   0.010000   1.400000 (  1.400842)
    
    1.9.2p290 :050 > 
    

    vs. second code :

    1.9.2p290 :049 > Competitor  = Struct.new(:html_url, :description, :user)
    1.9.2p290 :050 > time = Benchmark.measure do
    1.9.2p290 :051 >     competitors = []
    1.9.2p290 :052?>   
    1.9.2p290 :053 >     User.where('watchlists.tags_array' => %w[ruby web framework]).only(:nickname, :watchlists).each do |u|
    1.9.2p290 :054 >         u.watchlists.where(:tags_array => %w[ruby web framework]).each do |wl|
    1.9.2p290 :055 >             competitors << Competitor.new(wl.html_url, wl.description, u.nickname)
    1.9.2p290 :056?>         end
    1.9.2p290 :057?>     end
    1.9.2p290 :058?>   end
    MONGODB (185ms) heroku_app1707530['users'].find({"watchlists.tags_array"=>["ruby", "web", "framework"]}, {:_type=>1, :nickname=>1, :watchlists=>1})
    MONGODB [DEBUG] cursor.refresh() for cursor 2195378613558492020
     =>   0.440000   0.000000   0.440000 (  0.456714)
    
    1.9.2p290 :059 > 
    

    but even better :

    first code :

    1.9.2p290 :157 > Competitor  = Struct.new(:html_url, :description, :user)
    1.9.2p290 :158 > competitors = []
     => [] 
    1.9.2p290 :159 > Benchmark.bm(10) do |x|
    1.9.2p290 :160 >     x.report("first:") do
    1.9.2p290 :161 >       User.all.map do |user|
    1.9.2p290 :162 >           user.watchlists.all.map do |wl|
    1.9.2p290 :163 >               if wl.tags_array == ["ruby", "web", "framework"]
    1.9.2p290 :164?>                   competitors << Competitor.new(wl.html_url, wl.description, wl.user.nickname)
    1.9.2p290 :165?>               end
    1.9.2p290 :166?>           end
    1.9.2p290 :167?>       end
    1.9.2p290 :168?>     end
    1.9.2p290 :169?>   x.report("second:") do
    1.9.2p290 :170 >       User.all.map do |user|
    1.9.2p290 :171 >           user.watchlists.all.map do |wl|
    1.9.2p290 :172 >               if wl.tags_array == ["ruby", "web", "framework"]
    1.9.2p290 :173?>                   competitors << Competitor.new(wl.html_url, wl.description, wl.user.nickname)
    1.9.2p290 :174?>               end
    1.9.2p290 :175?>           end
    1.9.2p290 :176?>       end
    1.9.2p290 :177?>     end
    1.9.2p290 :178?>   x.report("third:") do
    1.9.2p290 :179 >       User.all.map do |user|
    1.9.2p290 :180 >           user.watchlists.all.map do |wl|
    1.9.2p290 :181 >               if wl.tags_array == ["ruby", "web", "framework"]
    1.9.2p290 :182?>                   competitors << Competitor.new(wl.html_url, wl.description, wl.user.nickname)
    1.9.2p290 :183?>               end
    1.9.2p290 :184?>           end
    1.9.2p290 :185?>       end
    1.9.2p290 :186?>     end
    1.9.2p290 :187?>   end
                    user     system      total        real
    first:    MONGODB (30ms) heroku_app1707530['users'].find({})
    MONGODB [DEBUG] cursor.refresh() for cursor 6320857008182747446
    MONGODB [DEBUG] cursor.refresh() for cursor 6320857008182747446
      1.460000   0.010000   1.470000 (  1.475545)
    second:   MONGODB (24ms) heroku_app1707530['users'].find({})
    MONGODB [DEBUG] cursor.refresh() for cursor 8580701579081246457
    MONGODB [DEBUG] cursor.refresh() for cursor 8580701579081246457
      1.470000   0.010000   1.480000 (  1.494812)
    third:    MONGODB (24ms) heroku_app1707530['users'].find({})
    MONGODB [DEBUG] cursor.refresh() for cursor 6472818135140756688
    MONGODB [DEBUG] cursor.refresh() for cursor 6472818135140756688
      1.490000   0.010000   1.500000 (  1.505000)
     => true 
    1.9.2p290 :188 > 
    

    vs. second code :

    1.9.2p290 :065 > Competitor  = Struct.new(:html_url, :description, :user)
    1.9.2p290 :066 > competitors = []
     => [] 
    1.9.2p290 :067 > Benchmark.bm(10) do |x|
    1.9.2p290 :068 >     x.report("first:") {User.where('watchlists.tags_array' => %w[ruby web framework]).only(:nickname, :watchlists).each{|u|u.watchlists.where(:tags_array => %w[ruby web framework]).each{|wl|competitors << Competitor.new(wl.html_url, wl.description, u.nickname)}}}
    1.9.2p290 :069?>   x.report("second:") {User.where('watchlists.tags_array' => %w[ruby web framework]).only(:nickname, :watchlists).each{|u|u.watchlists.where(:tags_array => %w[ruby web framework]).each{|wl|competitors << Competitor.new(wl.html_url, wl.description, u.nickname)}}}
    1.9.2p290 :070?>   x.report("third:") {User.where('watchlists.tags_array' => %w[ruby web framework]).only(:nickname, :watchlists).each{|u|u.watchlists.where(:tags_array => %w[ruby web framework]).each{|wl|competitors << Competitor.new(wl.html_url, wl.description, u.nickname)}}}
    1.9.2p290 :071?>   end
                    user     system      total        real
    first:    MONGODB (163ms) heroku_app1707530['users'].find({"watchlists.tags_array"=>["ruby", "web", "framework"]}, {:_type=>1, :nickname=>1, :watchlists=>1})
    MONGODB [DEBUG] cursor.refresh() for cursor 7239021952645827000
      0.330000   0.000000   0.330000 (  0.380199)
    second:   MONGODB (164ms) heroku_app1707530['users'].find({"watchlists.tags_array"=>["ruby", "web", "framework"]}, {:_type=>1, :nickname=>1, :watchlists=>1})
    MONGODB [DEBUG] cursor.refresh() for cursor 4816095738351422260
      0.320000   0.010000   0.330000 (  0.381196)
    third:    MONGODB (125ms) heroku_app1707530['users'].find({"watchlists.tags_array"=>["ruby", "web", "framework"]}, {:_type=>1, :nickname=>1, :watchlists=>1})
    MONGODB [DEBUG] cursor.refresh() for cursor 5014359782446173361
      0.390000   0.010000   0.400000 (  0.397241)
     => true 
    1.9.2p290 :072 > 
    

    then second code results really faster and less cpu consuming …,
    thanks to rubish for the “second code”

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Anyone have a suggestion for a good utility class that maps values from one
Does anyone have a suggestion for where to find archives or collections of everyday
Does anyone have a suggestion for creating paragraph-type line spaces within a <li> tag
I need a excel-like grid control in MFC, do anyone have good suggestion to
I have followed the suggestion in this question... [ How to handle checkboxes in
I have followed the suggestion in this question as I am using Django, I
I have the latest ReSharper 5.0 build (1655), where I have encountered the suggestion
I have a working Lucene index supporting a suggestion service. When a user types
I have a little experience with WCF and would like to get your opinion/suggestion
I need suggestion about YAMI library . I have a system which receives Json

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.