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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:56:31+00:00 2026-05-24T02:56:31+00:00

Which one of these is faster? Is one better? Basically I’ll have two sets

  • 0

Which one of these is faster? Is one “better”? Basically I’ll have two sets and I want to eventually get one match from between the two lists. So really I suppose the for loop is more like:

for object in set:
    if object in other_set:
        return object

Like I said – I only need one match, but I’m not sure how intersection() is handled, so I don’t know if its any better. Also, if it helps, the other_set is a list near 100,000 components and the set is maybe a few hundred, max few thousand.

  • 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-24T02:56:32+00:00Added an answer on May 24, 2026 at 2:56 am
    from timeit import timeit
    
    setup = """
    from random import sample, shuffle
    a = range(100000)
    b = sample(a, 1000)
    a.reverse()
    """
    
    forin = setup + """
    def forin():
        # a = set(a)
        for obj in b:
            if obj in a:
                return obj
    """
    
    setin = setup + """
    def setin():
        # original method:
        # return tuple(set(a) & set(b))[0]
        # suggested in comment, doesn't change conclusion:
        return next(iter(set(a) & set(b)))
    """
    
    print timeit("forin()", forin, number = 100)
    print timeit("setin()", setin, number = 100)
    

    Times:

    >>>
    0.0929054012768
    0.637904308732
    >>>
    0.160845057616
    1.08630760484
    >>>
    0.322059185123
    1.10931801261
    >>>
    0.0758695262169
    1.08920981403
    >>>
    0.247866360526
    1.07724461708
    >>>
    0.301856152688
    1.07903130641
    

    Making them into sets in the setup and running 10000 runs instead of 100 yields

    >>>
    0.000413064976328
    0.152831597075
    >>>
    0.00402408388788
    1.49093627898
    >>>
    0.00394538156695
    1.51841512101
    >>>
    0.00397715579584
    1.52581949403
    >>>
    0.00421472926155
    1.53156769646
    

    So your version is much faster whether or not it makes sense to convert them to sets.

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

Sidebar

Related Questions

Which one of these is the faster/better one? This one: List<User> list = new
Which one of these two method invocations is considered most pythonic? some_method(that_has, very_many, aurguments=None,
I have a tab view which has a listview in one of these tabs.
Iam making an app which has one index.php file. I also have these other
Hi, i would like to know which one of these assignments is faster, safer,
Which one of these would be more suitable for RLE on bytes? I need
How do you connect these to your computer? Which one would be easier to
I have one frame in which one TestArea is there. When I append some
Is there any API by which one can get all the categories of a
I have a PHP based web application which is currently only using one webserver

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.