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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:35:22+00:00 2026-05-22T17:35:22+00:00

I need a very fast way to determine if an array consits only of

  • 0

I need a very fast way to determine if an array consits only of integers with the value of 9. Here is my current solution:

input = [9,9,9,9,9,9,9,9,9,9,9,9]
input.uniq == [9]

Can you do it faster?

  • 1 1 Answer
  • 2 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-22T17:35:22+00:00Added an answer on May 22, 2026 at 5:35 pm
    require 'benchmark'
    
    n = 50000
    Benchmark.bm do |x|
      x.report "uniq  " do
        n.times do 
          input = [9,9,9,9,9,9,9,9,9,9,9,9]
          input.uniq == [9]
        end
      end
      x.report "delete" do
        n.times do 
          input = [9,9,9,9,9,9,9,9,9,9,9,9]
          input.delete 9
          input == []
        end  
      end
      x.report "count " do
        n.times do
          input = [9,9,9,9,9,9,9,9,9,9,9,9]
          input.count(9)==input.size
        end
      end
      x.report "select" do
        n.times do
          input = [9,9,9,9,9,9,9,9,9,9,9,9]
          input.select{|x| x != 9}.empty?
        end
      end  
      x.report "detect" do
        n.times do
          input = [9,9,9,9,9,9,9,9,9,9,9,9]
          input.detect { |i| i != 9 }.nil?
        end
      end 
    
      x.report "all?  " do
        n.times do
          input = [9,9,9,9,9,9,9,9,9,9,9,9]
          input.all?{|x| x == 9} 
        end
      end 
    
    end
    

    it a benchmark for the answers above and some mine

            user       system      total        real
    uniq    0.313000   0.000000   0.313000 (  0.312500)
    delete  0.140000   0.000000   0.140000 (  0.140625)
    count   0.079000   0.000000   0.079000 (  0.078125)
    select  0.234000   0.000000   0.234000 (  0.234375)
    detect  0.234000   0.000000   0.234000 (  0.234375)
    all?    0.219000   0.000000   0.219000 (  0.218750)
    

    if input = [1]+[9]*9:

            user     system      total        real
    uniq    0.328000   0.000000   0.328000 (  0.328125)
    delete  0.188000   0.000000   0.188000 (  0.203125)
    count   0.187000   0.000000   0.187000 (  0.218750)
    select  0.281000   0.016000   0.297000 (  0.296875)
    detect  0.203000   0.000000   0.203000 (  0.203125)
    all?    0.204000   0.000000   0.204000 (  0.203125)
    

    if input = [9]*9 + [1]:

            user     system      total        real
    uniq    0.313000   0.000000   0.313000 (  0.328125)
    delete  0.187000   0.000000   0.187000 (  0.187500)
    count   0.172000   0.000000   0.172000 (  0.187500)
    select  0.297000   0.000000   0.297000 (  0.312500)
    detect  0.313000   0.000000   0.313000 (  0.312500)
    all?    0.281000   0.000000   0.281000 (  0.281250)
    

    if input = [1,2,3,4,5,6,7,8,9]:

            user     system      total        real
    uniq    0.407000   0.000000   0.407000 (  0.406250)
    delete  0.125000   0.000000   0.125000 (  0.125000)
    count   0.125000   0.000000   0.125000 (  0.125000)
    select  0.218000   0.000000   0.218000 (  0.234375)
    detect  0.110000   0.000000   0.110000 (  0.109375)
    all?    0.109000   0.000000   0.109000 (  0.109375)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a very fast SQLite database acces. Setting parameters this way: PRAGMA synchronize
I need a very fast way of displaying a data buffer to screen. I
I need a very simple menu which probably contains only one or two items:
I need a very fast method to detect if an image is empty. Im
I need to insert many sql rows into oracle database very fast. IndexData is
I need a simple and fast way to compare two images for similarity. I.e.
I'm looking for the fastest way to determine if a long value is a
For my internship on Brain-Computer Interfacing I need to generate some very fast flickering
I need very basic template engine, which is incapsulated in single class. So I
I need a very simple jQuery to constantly scroll a list of text items

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.