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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:10:16+00:00 2026-05-30T14:10:16+00:00

Ok, I understand in languages like C++ why calling virtual method defined in a

  • 0

Ok, I understand in languages like C++ why calling virtual method defined in a class is slower than calling a non-virtual method (you have to go through the dynamic dispatch table to lookup the correct implementation to call).

But in Python, if I have:

list_of_sets = generate_a_list_containg_a_bunch_of_sets()
intersection_of_all = reduce(list_of_sets[0].intersection, list_of_sets)

This is dramatically (in my experiments about 40%) slower than:

list_of_sets = generate_a_list_containg_a_bunch_of_sets()
intersection_of_all = reduce(set.intersection, list_of_sets)

What I don’t get is why that should be so much slower, the method lookup (I would think) would happen on the call to reduce, so the inside of reduce where the intersection method is actually called shouldn’t have to be looked up again (it just just reuse the same method reference).

Could someone illuminate where my understanding is flawed?

  • 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-30T14:10:17+00:00Added an answer on May 30, 2026 at 2:10 pm

    This is completely unrelated to method binding etc. The first version computes the intersection of three sets in each iteration, while the second version only intersects two sets. This is easy to see if we use the explicit loops instead.

    Variant 1:

    intersection = list_of_sets[0]
    for s in list_of_sets[1:]:
        intersection = list_of_sets[0].intersection(intersection, s)
    

    Variant 2:

    intersection = list_of_sets[0]
    for s in list_of_sets[1:]:
        intersection = set.intersection(intersection, s)
    

    (Would you agree now Guido has a point?)

    Note that this will probably be even faster:

    intersection = list_of_sets[0]
    for s in list_of_sets[1:]:
        intersection.intersection_update(s)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that more-dynamic-than-Java languages, like Python and Ruby, often allow you to place
How are files downloaded from servers in programming languages like C? I understand higher
As the title suggests I would like to understand why static classes can have
I use several different programming languages every day, and I'd like to have different
Structured programming languages typically have a few control structures, like while , if ,
This benchmark appears to show that calling a virtual method directly on object reference
I had some experience on programming languages like Java, C#, Scala as well as
I've seen this pretty funny screencast yesterday about some oddities in languages like Ruby
If we've learned anything from HTML/CSS it's that, declarative languages (like XML) do a
I understand the basics of dataflow programming and have encountered it a bit in

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.