in Education by
def main(): for i in xrange(10**8): pass main() This piece of code in Python runs in (Note: The timing is done with the time function in BASH in Linux.) real 0m1.841s user 0m1.828s sys 0m0.012s However, if the for loop isn't placed within a function, for i in xrange(10**8): pass then it runs for a much longer time: real 0m4.543s user 0m4.524s sys 0m0.012s Why is this? Select the correct answer from above options

1 Answer

0 votes
by
 
Best answer
The reason for a Python code run faster in a function is because, when a function is compiled, the local variables are stored in a fixed-size array (not a dictionary) and variable names are assigned to indexes. This is possible because we can't dynamically add local variables to a function. While retrieving a local variable is literally a pointer lookup into the list and a refcount increase on the PyObject which is trivial.

Related questions

0 votes
    Why does a sorted array get processed faster than an unsorted array, even though the size of the arrays are ... processed faster. Why? Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    I was discussing neural networks (NN) with a friend over lunch the other day and he claimed the the ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 27, 2022 in Education by JackTerrance
0 votes
    I was discussing neural networks (NN) with a friend over lunch the other day and he claimed the the ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 27, 2022 in Education by JackTerrance
0 votes
    During trying to achieve the performance with Hyperledger Fabric which IBM team reported in their article Hyperledger ... performance? Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    Just wondering if any of you guys use Count(1) over Count(*) and if there is a noticeable difference in ... SQL Server 2005.) Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    Can anyone tell me what tool does a programmer use to produce python source code? Select the correct answer from above options...
asked Jan 8, 2022 in Education by JackTerrance
0 votes
    If I use a while loop for my below code, it is not giving the desired output, but when i use for loop i am anle ... x += 1 print(Comm) Select the correct answer from above options...
asked Jan 9, 2022 in Education by JackTerrance
0 votes
    I understand nearly nothing to the functioning of EC2. I created an Amazon Web Service (AWS) account. Then I ... user's guide. Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    I can use vi{ and va{ to select C++ code blocks. It helps me a lot when I need to yank ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Aug 1, 2022 in Education by JackTerrance
0 votes
    This is my code it's just starting the scan but it is not completing ,where is the error in it. ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 26, 2022 in Education by JackTerrance
0 votes
    I'm new to Python and I want to install some packages with pip. But pip install unroll gives me: Command " ... How to solve this? Select the correct answer from above options...
asked Jan 25, 2022 in Education by JackTerrance
0 votes
    I had some Python 2 code as follows (pardon the indentation): def getZacksRating(symbol): c = httplib.HTTPSConnection("www. ... data.split(' ')[1] result = ratingPart.partition("...
asked Jan 9, 2022 in Education by JackTerrance
0 votes
    _______________ has made PL/SQL code run faster without requiring any additional work on the part of the ... topic in chapter Query Processing Techniques of Database Management...
asked Oct 10, 2021 in Education by JackTerrance
0 votes
    I am getting errors for the code when running it on a 'tips' dataset but I can run it on a tulips dataset ... . Am I missing something? Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    I'm trying to figure out Python lambdas. Is lambda one of those "interesting" language items that in ... maintenance coder nightmare. Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
...