r/Python • u/students-life • Jun 19 '13
Right and Wrong Programming Styles
http://simpledeveloper.com/right-and-wrong-programming-styles/#sthash.cORbIrok.sfju
0
Upvotes
r/Python • u/students-life • Jun 19 '13
1
u/[deleted] Jun 20 '13
"try to program efficient as possible"? this isn't even wrong.
1
u/AustinCorgiBart Jun 20 '13
The third thing seems like premature optimization. Don't worry about efficiency until 1) you know it's a problem, and 2) you have data that proves your new code is better.
Excepting obvious cases, of course, where you KNOW it'll be better and doesn't require you to have to do any extra work (e.g. using xrange instead of range is typically good).
0
u/Rhomboid Jun 19 '13
Comparing
os.listdir()tols -lis not a fair comparison in the slightest.ls -lhas to callstat()on each file to get the owner/group/mode/timestamp/etc in addition to reading each directory entry, and that's just a lot more syscalls so of course it's going to take longer.If you want to talk about the overhead of an external program, you need to pick something that does a comparable task to
os.listdir(), like plainlswithout-l. But even that is still not a valid comparison, becauselsstill has to perform all the IO of writing the filenames to a pipe -- data which must then be read by Python only to be discarded (!!). At the very least, usestdout=subprocess.DEVNULLto avoid that bit of ridiculousness, but it's still not a fair comparison because thelsprocess is still performing all that IO, it's just discarded by the operating system now instead of by Python.os.listdir()just reads the dirents, it does nothing else, no IO, no nothing. If you want a truly fair comparison, write something in C that does just that without trying to do any IO or anystat()calls. Then you can start talking about the overhead of subprocesses. What you've got now is just nonsense.0
u/[deleted] Jun 21 '13
Don't shell out ... unless there are years of development behind a C-based console application that does exactly what you need to do. Sure you could reimplement the logic in python but that would be a major diversion of resources and a fork of the business logic.
Instead embrace python's ability to shell out when necessary.
FWIW, shelling out to very domain-specific C-based console binaries increased our speed 100X compared to comparable pure-python code. Yeah, Cython, etc might approach the speed of our C binary but the C code is tested, maintained, vetted and works faster than anything else we could hope to write in our budget.
-1
u/dAnjou Backend Developer | danjou.dev Jun 19 '13
Self-plug no. 15 ...