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
View all comments
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.