Page 1 of 1

TIL [python] code runs faster in functions

Posted: Thu Jun 28, 2012 6:29 pm
by Isaac
Today I learned that python code runs faster in functions because their variables are enclosed and not accessible to all the outer "top-level" python stuff.

Not sure if I'm explaining it clearly.

Code: Select all

slowvariable = 4

def some_function (): 
     fastvariable = 4
Loops within some_function that use the fastvariable will go faster than loops outside of some_function that use slowvariable.

source: http://stackoverflow.com/questions/1124 ... a-function

Re: TIL python code runs faster in functions

Posted: Thu Jun 28, 2012 6:52 pm
by Jeff250
I wouldn't have expected that. I would have expected each global lookup to look in global() and each local lookup to look in local(), but it looks like they optimize local lookups for the general case.

If you need to reference slowvariable multiple times in a function, I suppose you can do fastvariable = slowvariable once and then just reference fastvariable from then on in. However, this would damage readability, and if that function is that performance critical, then you're probably using the wrong language for it. ;)

Re: TIL python code runs faster in functions

Posted: Thu Jun 28, 2012 9:14 pm
by Isaac
Oh yeah. If I need something written in C, normally there's a python library written in C I can compile, out there on the net. Also, I can use terminal applications with os.system("blah blah"), so I can do heavy lifting that way too.

Re: TIL python code runs faster in functions

Posted: Fri Jun 29, 2012 2:19 pm
by snoopy
Isaac wrote:Oh yeah. If I need something written in C, normally there's a python library written in C I can compile, out there on the net. Also, I can use terminal applications with os.system("blah blah"), so I can do heavy lifting that way too.
I believe that subprocess is the preferred way to call external programs... it's more secure than os.system, IIRC.

Re: TIL python code runs faster in functions

Posted: Fri Jun 29, 2012 2:40 pm
by Isaac
?!?!!?!? :? !?!?!?!?!?

Re: TIL python code runs faster in functions

Posted: Fri Jun 29, 2012 3:50 pm
by Jeff250
For constant strings, it should be fine. The problem occurs when you want to say something like os.system('cp ' + file1 + ' ' +file2), since if file1 and file2 are untrusted input, like something some dickhole inputted through a Web form, then file2 could be e.g. the string '&& rm -rf /'. So now you could escape file1 and file2 to force the shell to treat each file as one "word" each, but now you have to know how to do that correctly, and different shells have different escaping rules.

The subprocess module is nice because you just give it a list of arguments, although for untrusted filename input, you might still want to first filter slashes or preceding dots.