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 7053347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:29:38+00:00 2026-05-28T03:29:38+00:00

In a Ruby project that I have been spending some time on lately, I

  • 0

In a Ruby project that I have been spending some time on lately, I have been counting the intersection of two large sets of strings.

From what I thought I understood, I decided that it would make a lot of sense to compare integers instead of strings (all of these strings are being held in a database, and i could easily just swap them out for ids)

When i actually did the benchmarking, i ended up finding the complete opposite.

First i generated sets of 850 strings, and sets of ~850 large integers:

r = Random.new
w1 = (1..850).collect{|i| w="";(0..3).collect{|j| (rand*26 + 10).to_i.to_s(35)}.each{|l| w+=(l.to_s)};w}.to_set
w2 = (1..850).collect{|i| w="";(0..3).collect{|j| (rand*26 + 10).to_i.to_s(35)}.each{|l| w+=(l.to_s)};w}.to_set

i1 = (1..2000).collect{|i| (r.rand*1000).to_i**2}.to_set;
i2 = (1..2000).collect{|i| (r.rand*1000).to_i**2}.to_set;

And then i timed the comparisons:

t=Time.now;(0..1000).each {|i| w1 & w2};Time.now-t
=> 0.301727
t=Time.now;(0..1000).each {|i| i1 & i2};Time.now-t
=> 0.70151

Which i thought was crazy! I always thought integer comparison was much faster..

So i was wondering if anybody in the world of stacks knew anything about why the string comparison is so much faster in ruby, i would really appreciate hearing your thoughts.

  • 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-05-28T03:29:39+00:00Added an answer on May 28, 2026 at 3:29 am

    The speed of the set intersection operation appears to be affected by the number of intersecting elements.

    Your integer creation code is creating a substantially larger number of intersecting elements, probably because it’s selecting 2000 entries from a smaller set (1000).

    In one test, for example, 755 of the 857 entries in i1 were duplicated in i2, but only 2 of the 849 entries in w1 were duplicated in w2.

    When I ran a simple alteration:

    755.times {|x| w2 << w1.to_a[x]}
    

    (dumping 755 items into w2 that are known to be in w1), the results on my system showed the string set operation to be much closer to the equivalent integer operation.

    My original results were:

    1.9.2p180 :006 > t=Time.now;(0..1000).each {|i| w1 & w2};Time.now-t
     => 1.020355
    1.9.2p180 :007 > t=Time.now;(0..1000).each {|i| i1 & i2};Time.now-t
     => 2.057535
    

    My results after making the two sets of sets more alike in terms of intersecting elements, via:

    1.9.2p180 :051 > 755.times {|x| w2 << w1.to_a[x]}
    1.9.2p180 :052 > w2 = w2.to_a[-849..-1].to_set
    

    were:

    1.9.2p180 :053 > t=Time.now;(0..1000).each {|i| w1 & w2};Time.now-t
     => 2.014967 
    1.9.2p180 :054 > t=Time.now;(0..1000).each {|i| i1 & i2};Time.now-t
     => 2.037542
    1.9.2p180 :055 > [i1.length, i2.length, w1.length, w2.length, (i1 & i2).length, (w1 & w2).length]
     => [857, 884, 849, 849, 755, 754]
    

    I hope that helps some; the two timings are within what I would consider a margin of error that other things on the system could be causing the difference. They are, essentially, equal for strings of this length.

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

Sidebar

Related Questions

I have been using ActiveResource in my Ruby on Rails applications for some time,
So I have been playing with a Ruby gem for a work project that
I've started working on a little ruby project that will have sample implementations of
Hello Ruby/Rails/Merb developers! Im currently working on a web project that will have a
I'm working with a Ruby project for school, and have sadly not been able
I have a project of ruby on rails downloaded from net...how to execute that
I have been contributing heavily to this open source project that aims to help
I have just been informed that a Rails 3 project I've been working on
On a rails project that I know to have been working as recently as
Having been landed with a Ruby project, I have been sceptical about the decision

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.