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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:39:16+00:00 2026-06-11T06:39:16+00:00

This is surely no python-specific question, but I am looking for a python-specific answer

  • 0

This is surely no python-specific question, but I am looking for a python-specific answer – if any. It is about putting code blocks with a large number of variables into functions (or alike?). Let me assume this code

##!/usr/bin/env python
# many variables: built in types, custom made objects, you name it.
# Let n be a 'substantial' number, say 47.
x1 = v1
x2 = v2
...
xn = vn

# several layers of flow control, for brevity only 2 loops
for i1 in range(ri1):
    for i2 in range(ri2):
        y1 = f1(i1,i2)
        y2 = f2(i1,i2)
        # Now, several lines of work

        do_some_work

        # involving HEAVY usage and FREQUENT (say several 10**3 times)
        # access to all of x1,...xn, (and maybe y1,y2)
        # One of the main points is that slowing down access to x1,...,xn
        # will turn into a severe bottleneck for the performance of the code.


# now other things happen. These may or may not involve modification
# of x1,...xn

# some place later in the code, again, several layers of flow control,
# not necessarily identical to the first occur
for j1 in range(rj1):
    y1 = g1(j1)
    y2 = g2(j1)
    # Now, again

    do_some_work  # <---- this is EXACTLY THE SAME code block as above

# a.s.o.

Obviously I would like to put ‘do_some_work’ into something like a function (or maybe something better?).

What would be the most performant way to do this in python

  • without function calls with a confusingly large numbers of arguments

  • without performance lossy indirection to access x1,…,xn (Say, by wrapping them into another list, class, or alike)

  • without using x1,…,xn as globals in a function do_some_work(…)

I have to admit, that I always find myself returning to globals.

  • 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-11T06:39:18+00:00Added an answer on June 11, 2026 at 6:39 am

    A simple and dirty(probably not optimal) banchmark:

    import timeit
    def test_no_func():
        (x0,x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,x16,x17,x18,x19) = range(20)
        for i1 in xrange(100):
                for i2 in xrange(100):
                        for i3 in xrange(100):
                                results = [x0+x1+x2+x3+x4+x5+x6 for _ in xrange(100)]
                                results.extend(x7+x8+x9+x10+x11+x12+x13+x14+x15 for _ in xrange(100))
                                results.extend(x16+x17+x18+x19+x0 for _ in xrange(500))
        for j1 in xrange(100):
                for j2 in xrange(100):
                        for i3 in xrange(100):
                                results = [x0+x1+x2+x3+x4+x5+x6 for _ in xrange(100)]
                                results.extend(x7+x8+x9+x10+x11+x12+x13+x14+x15 for _ in xrange(100))
                                results.extend(x16+x17+x18+x19+x0 for _ in xrange(500))
    
    
    def your_func(x_vars):
        # of the number is not too big you can simply unpack.
        # 150 is a bit too much for unpacking...
        (x0,x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,x16,x17,x18,x19) = x_vars
    
        results = [x0+x1+x2+x3+x4+x5+x6 for _ in xrange(100)]
        results.extend(x7+x8+x9+x10+x11+x12+x13+x14+x15 for _ in xrange(100))
        results.extend(x16+x17+x18+x19+x0 for _ in xrange(500))
        return results
    
    
    def test_func():
        (x0,x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,x16,x17,x18,x19) = range(20)
        for i1 in xrange(100):
                for i2 in xrange(100):
                        for i3 in xrange(100):
                                results = your_func(val for key,val in locals().copy().iteritems() if key.startswith('x'))
        for j1 in xrange(100):
                for j2 in xrange(100):
                        for i3 in xrange(100):
                                results = your_func(val for key,val in locals().copy().iteritems() if key.startswith('x'))
    
    
    print timeit.timeit('test_no_func()', 'from __main__ import test_no_func', number=1)
    print timeit.timeit('test_func()', 'from __main__ import test_func, your_func', number=1)
    

    Result:

    214.810357094
    227.490054131
    

    which is about 5% slower passing the arguments. But probably you can’t do much better than this introducing 1 million function calls…

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

Sidebar

Related Questions

This surely is a noob question, but I can't find an answer in Doxygen
Apologies for this seemingly minor question, but I can't seem to find the answer
This question is directly related to this SO question I posed about 15 minutes
I know this is really basic but it is frustrating. I'm using Python 2
Surely this is possible? I have been hunting through PyQt tutorials and documentation but
I occasionally see the list slice syntax used in Python code like this: newList
This will surely be an easy one but it is really bugging me. I
Surprisingly I've been unable to find anyone else really doing this, but surely someone
The following code works just fine. But it seems so verbose, surely there is
this is my first question in here, and I would like to ask if

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.