TIL [python] code runs faster in functions

For all coding issues - MODers and programmers, HTML and more.

Moderators: Jeff250, fliptw

Post Reply
User avatar
Isaac
DBB Artist
DBB Artist
Posts: 7737
Joined: Mon Aug 01, 2005 8:47 am
Location: 🍕

TIL [python] code runs faster in functions

Post 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
❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉
-⎽__⎽-⎻⎺⎺⎻-⎽__⎽--⎻⎺⎺⎻-★ ·:*¨༺꧁༺ :E ༻꧂༻¨*:·.★-⎽__⎽-⎻⎺⎺⎻-⎽__⎽--⎻⎺⎺⎻-
❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉
User avatar
Jeff250
DBB Master
DBB Master
Posts: 6539
Joined: Sun Sep 05, 1999 2:01 am
Location: ❄️❄️❄️

Re: TIL python code runs faster in functions

Post 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. ;)
User avatar
Isaac
DBB Artist
DBB Artist
Posts: 7737
Joined: Mon Aug 01, 2005 8:47 am
Location: 🍕

Re: TIL python code runs faster in functions

Post 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.
❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉
-⎽__⎽-⎻⎺⎺⎻-⎽__⎽--⎻⎺⎺⎻-★ ·:*¨༺꧁༺ :E ༻꧂༻¨*:·.★-⎽__⎽-⎻⎺⎺⎻-⎽__⎽--⎻⎺⎺⎻-
❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉
User avatar
snoopy
DBB Benefactor
DBB Benefactor
Posts: 4435
Joined: Thu Sep 02, 1999 2:01 am

Re: TIL python code runs faster in functions

Post 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.
Arch Linux x86-64, Openbox
"We'll just set a new course for that empty region over there, near that blackish, holeish thing. " Zapp Brannigan
User avatar
Isaac
DBB Artist
DBB Artist
Posts: 7737
Joined: Mon Aug 01, 2005 8:47 am
Location: 🍕

Re: TIL python code runs faster in functions

Post by Isaac »

?!?!!?!? :? !?!?!?!?!?
❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉
-⎽__⎽-⎻⎺⎺⎻-⎽__⎽--⎻⎺⎺⎻-★ ·:*¨༺꧁༺ :E ༻꧂༻¨*:·.★-⎽__⎽-⎻⎺⎺⎻-⎽__⎽--⎻⎺⎺⎻-
❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉
User avatar
Jeff250
DBB Master
DBB Master
Posts: 6539
Joined: Sun Sep 05, 1999 2:01 am
Location: ❄️❄️❄️

Re: TIL python code runs faster in functions

Post 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.
Post Reply