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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:32:26+00:00 2026-05-24T11:32:26+00:00

Here is what I did, I created 2 procedures, one in a function and

  • 0

Here is what I did, I created 2 procedures, one in a function and one in the python file itself. The one on the python file itself run almost 2 times slower even if it’s exactly the same. WHY ?

Bellow is an example with 2 procedures that are just loops on P element

I have the following python file :

from time import *
P=1000000 #range of the 2 loops

def loop(N):
        for k in range(N):
                continue

start=time()
loop(P)
stop1=time()
for k in range(P):
            continue
stop2=time()
print "time with function ",stop1-start
print "time without function ",stop2-stop1

Here is what I get (I tried it with one thousand samples and the result is the following) :

time with function  0.0950000286102
time without function  0.15700006485

with xrange instead of range I get :

time with function  0.0460000038147
time without function  0.107999843597

So it’s like 0.05 second in used to build the list

I know it’s may be a useless question, but If someone know why is this going so much faster I would be happy to know

  • 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-24T11:32:27+00:00Added an answer on May 24, 2026 at 11:32 am

    The only significant difference is that the version in the function is only updating the local variable for that function, whereas the version not in a function is updating a global variable k.

    As mentioned here:

    The final speedup available to us for the non-map version of the for loop is to use local variables wherever possible. If the above loop is cast as a function, append and upper become local variables. Python accesses local variables much more efficiently than global variables.

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

Sidebar

Related Questions

Here is the application on codeplex all i did is created a new text
Here's my problem: I created three separate repos under one project. The repos are
Here's what I did, I have installed svnserve as a service and I started
Here's what I did: Right-clicked on an item in the 'Objects and Timeline' panel.
Did some searches here & on the 'net and haven't found a good answer
I did it using the commands as described here and it works but I
What did I do wrong? Here is an excerpt from my code: public void
I always believe they did, but seeing some answers here make me doubt... Can
Does anyone here know SVG? If so, how did you learn it? Any books/tutorial
Please refer my previous post here . I did changes accordingly but getting error.

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.