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

  • Home
  • SEARCH
  • 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 8406347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:05:11+00:00 2026-06-09T23:05:11+00:00

In Ruby, I am building a method which constructs and returns a (probably large)

  • 0

In Ruby, I am building a method which constructs and returns a (probably large) array which should contain no duplicate elements. Would I get better performance by using a set and then converting that to an array? Or would it be better to just call .uniq on the array I am using before I return it? Or what about using & to append items to the array instead of +=? And if I do use a set, would not having a <=> method on the object I am putting into the set have an effect on performance? (If you’re not sure, do you know of a way to test this?)

  • 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-09T23:05:13+00:00Added an answer on June 9, 2026 at 11:05 pm

    The real answer is: write the most readable and maintainable code, and optimize it only after you’ve shown it is a bottleneck. If you can find an algorithm in that is in linear time, you won’t have to optimize it. Here it’s easy to find…

    Not quite sure which methods you are suggesting, but using my fruity gem:

    require 'fruity'
    require 'set'
    
    enum = 1000.times
    
    compare do
      uniq { enum.each_with_object([]){|x, array| array << x}.uniq }
      set  { enum.each_with_object(Set[]){|x, set| set << x}.to_a }
      join { enum.inject([]){|array, x| array | [x]} }
    end
    
    # set is faster than uniq by 10.0% ± 1.0%
    # uniq is faster than join by 394x ± 10.0
    

    Clearly, it makes no sense building intermediate arrays like in the third method. Otherwise, it’s not going to make a big difference since you will be in O(n); that’s the main thing.

    BTW, both sets, uniq and Array#| use eql? and hash on your objects, not <=>. These need to be defined in a sane manner, because the default is that objects are never eql? unless they have the same object_id (see this question)

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

Sidebar

Related Questions

Ruby has a select method that takes an array and returns a subarray consisting
Im building a ruby on rails aplication, and trying to get my TDD on.
Possible Duplicate: How to turn a string into a method call? Using Ruby 1.9,
I'm currently building a web app (atop Ruby on Rails), which will let users
I building a website with Ruby on Rails framework. The site will contain a
I'm building a Ruby web service which accepts POST or PUT requests that tell
I'm building Ruby on Rails 2.3.5 app. By default, Ruby on Rails doesn't provide
I am building a ruby on rails project. it is a portal. I have
I am building a Ruby on Rails application and I want to be able
I'm currently building a semi-small ruby app for a project. The problem I'm currently

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.