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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:47:55+00:00 2026-05-23T22:47:55+00:00

In the documentation for the itertools module I found this comment def dotproduct(vec1, vec2):

  • 0

In the documentation for the itertools module I found this comment

def dotproduct(vec1, vec2):
    return sum(imap(operator.mul, vec1, vec2))

Note, many of the above recipes can be optimized by replacing global
lookups with local variables defined as default values. For example,
the dotproduct recipe can be written as:

def dotproduct(vec1, vec2, sum=sum, imap=imap, mul=operator.mul):
    return sum(imap(mul, vec1, vec2))

How is it?.
Is there a practical noticeable speed-up (that could balance the inconvenience of the larger function signature)?
In which specific conditions the use of local variables in a case as the one shown would be relevant?.

Edit: I tested with timeit and there is any relevant difference.
For two 40-items lists as vec1, vec2:

global lookup -> 3.22720959404
local lookup -> 3.19884065683

that is, only ca. 1% gain.

  • 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-23T22:47:56+00:00Added an answer on May 23, 2026 at 10:47 pm

    Is there a practical noticeable speed-up (that could balance the inconvenience of the larger function signature)?

    I very much doubt it, as the lookups occur once each in the original definition. Note that you’ve changed the meaning of the function.

    In which specific conditions the use of local variables in a case as the one shown would be relevant?

    Only inside a tight loop; in this case, if dot_product where used in say a very large matrix multiplication (which you wouldn’t do in pure Python anyway, let alone with iterators).

    EDIT: I just disassembled both functions and my hunch was wrong, though my point still stands:

    >>> def dotproduct(vec1, vec2):
    ...     return sum(imap(operator.mul, vec1, vec2))
    ...
    >>> dis.dis(dotproduct)
      2           0 LOAD_GLOBAL              0 (sum)
                  3 LOAD_GLOBAL              1 (imap)
                  6 LOAD_GLOBAL              2 (operator)
                  9 LOAD_ATTR                3 (mul)
                 12 LOAD_FAST                0 (vec1)
                 15 LOAD_FAST                1 (vec2)
                 18 CALL_FUNCTION            3
                 21 CALL_FUNCTION            1
                 24 RETURN_VALUE
    >>> def dotproduct(vec1, vec2, sum=sum, imap=imap, mul=operator.mul):
    ...     return sum(imap(mul, vec1, vec2))
    ...
    >>> dis.dis(dotproduct)
      2           0 LOAD_FAST                2 (sum)
                  3 LOAD_FAST                3 (imap)
                  6 LOAD_FAST                4 (mul)
                  9 LOAD_FAST                0 (vec1)
                 12 LOAD_FAST                1 (vec2)
                 15 CALL_FUNCTION            3
                 18 CALL_FUNCTION            1
                 21 RETURN_VALUE
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Documentation can be found here It says in the example: onDrop: Called whenever a
The documentation with the module itself is pretty thin, and just tends to point
The documentation of the Python readline module says Availability: Unix. However, it doesn't appear
The documentation for Access.BackingField() indicates that this: Sets the access-strategy to use the backing-field
Documentation for logging module says that If you are implementing asynchronous signal handlers using
Documentation for GStreamer is confusing. This is still no excuse for resorting to StackOverflow.com
The documentation isn't very clear on this. Can someone illuminate the differences between these
import itertools def _yield_sample(): it = iter(itertools.combinations('ABCD', 2)) it2 = iter(itertools.combinations('EFGH', 3)) itc =
The documentation on RequestCacheLevel.BypassCache doesn't explain much on the internal working of setting this
The documentation of sorl thumbnail still refers to the get_thumbnail function, but this doesn't

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.