Is there any speed difference in the execution of the
os.system() command? Suppose that the command itself is exactly same.
I know there are many posts out there talking about the benefits of subprocess and why
os.system() should be ditched…but no one mentioned anything about the speed of them. From what I understand, subprocess is nowhere near a wrapper for
os.system(), which means there could be speed differences.
Would prefer an answer with experimental data instead of theory stuff, as I’m actually needing it to evaluate the performance of a script that has hundreds of such calls.
os.system is significantly more expensive than
subprocess.call on Windows. Indeed,
os.system starts a batch interpreter that will parse the command, then typically locate an executable to run in the path and then start a new process running the target executable. Meanwhile,
subprocess.call mainly starts the target executable. Running a new process is one of the most expensive operation so running two processes makes the operation much slower.
A simple benchmark on Windows proves this (calling
ls.exe on an empty directory):
os.system: 62.3 ms/call subprocess.call: 34.8 ms/call
On Linux/Mac/WSL, the cost of starting a new process is much smaller. Thus, we should expect the gap to be smaller which is the case. However, the results are a bit surprising as
subprocess.call is a bit slower. Here is the result on WSL2:
os.system: 3.0 ms/call subprocess.call: 3.8 ms/call