Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

8
  • 18
    How did you actually do the timing? Commented Jun 28, 2012 at 9:21
  • 59
    Just an intuition, not sure if it's true: I would guess it's because of scopes. In the function case, a new scope is created (i.e. kind of a hash with variable names bound to their value). Without a function, variables are in the global scope, when you can find lot of stuff, hence slowing down the loop. Commented Jun 28, 2012 at 9:22
  • 5
    @Scharron That doesn't seem to be it. Defined 200k dummy variables into the scope without that visibly affecting the running time. Commented Jun 28, 2012 at 9:27
  • 64
    @Scharron you're half correct. It is about scopes, but the reason it's faster in locals is that local scopes are actually implemented as arrays instead of dictionaries (since their size is known at compile-time). Commented Jun 28, 2012 at 10:31
  • 4
    @AndrewJaffe The output would suggest linux' time command. Commented Jun 28, 2012 at 15:35