Asked • 06/14/19

Why does Python code run faster in a function?

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.012sHowever, if the for loop isn't placed within a function, for i in xrange(10**8):  passthen it runs for a much longer time: real 0m4.543s user 0m4.524s sys  0m0.012sWhy is this?

1 Expert Answer

By:

Yacine F. answered • 03/28/20

Tutor
New to Wyzant

I Will Make Easy it For You

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.