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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:06:10+00:00 2026-05-27T22:06:10+00:00

I am working with serialized array fields in one of my models, specifically in

  • 0

I am working with serialized array fields in one of my models, specifically in counting how many members of each array are shared.

Now, by the nature of my project, I am having to a HUGE number of these overlap countings.. so I was wondering if there was a super quick, cleaver way to do this.

At the moment, I am using the ‘&’ method, so my code looks like this

(user1.follower_names & user2.follower_names).count

which works fine… but I was hoping there might be a faster way to do it.

  • 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-27T22:06:11+00:00Added an answer on May 27, 2026 at 10:06 pm

    Sets are faster for this.

    require 'benchmark'
    require 'set'
    alphabet = ('a'..'z').to_a
    user1_followers = 100.times.map{ alphabet.sample(3) }
    user2_followers = 100.times.map{ alphabet.sample(3) }
    user1_followers_set = user1_followers.to_set
    user2_followers_set = user2_followers.to_set
    
    n = 1000
    Benchmark.bm(7) do |x|
      x.report('arrays'){ n.times{ (user1_followers & user2_followers).size } }
      x.report('set'){ n.times{ (user1_followers_set & user2_followers_set).size } }
    end
    

    Output:

                  user     system      total        real
    arrays    0.910000   0.000000   0.910000 (  0.926098)
    set       0.350000   0.000000   0.350000 (  0.359571)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While working with ActiveRecord I have a table which stores a serialized array of
I'm working on a problem where I need to store serialized hierarchies of Ruby
Working on a project at the moment and we have to implement soft deletion
Working on a project that parses a log of events, and then updates a
I'm working with some serialized data from a MySQL database and I need to
I have an array of Shift objects that I'm working with in PHP. I
I'm working on a PHP/MySQL rating system right now. For the user to be
I'm working on database designs for a project management system as personal project and
I'm working on an application for a lab project and I'm making it in
In a Flex project, I have an array with objects in it. I want

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.