4 ms·
> and it takes microseconds to launch a subprocess On my laptop (ASUS GA503QM, 5800HS), running /usr/bin/true to completion takes around 400μs, demonstrated by
by chrismorgan 3y ago
> and it takes microseconds to launch a subprocess
On my laptop (ASUS GA503QM, 5800HS), running /usr/bin/true to completion takes around 400μs, demonstrated by these three different approaches to spawning a thousand processes:
$ python -m timeit -n 1000 --setup 'import subprocess' 'subprocess.run(["/usr/bin/true"])'
1000 loops, best of 5: 441 usec per loop
$ rustc -o x - <<<'fn main() {
for _ in 0..1000 {
let _ = std::process::Command::new("/usr/bin/true").status();
}
}' && time ./x
./x 0.33s user 0.07s system 99% cpu 0.402 total
$ time (i=0; while [ $i -lt 1000 ]; do i=$(($i + 1)); /usr/bin/true; done)
[elided] 0.66s user 0.43s system 100% cpu 1.088 total
Historically spawning processes was vastly slower on Windows; I’m not certain of the current state though I think it improved a few years ago, but I believe it’s still considerably more than one millisecond; 10–20ms is the vague number that’s lodged in my memory, but not supported, and I can’t test it now. (I invite anyone running Windows to provide figures. You may well already have a true.exe somewhere, or there’s https://github.com/mlocati/true-for-windows https://github.com/mlocati/true-for-windows, or just write your own stub executable.)
If you’re going to be using such calls often, spawning just one persistent process and using IPC for I/O is likely to be much faster.
- dataflow 3y ago> Historically spawning processes was vastly slower on Windows Indeed. > I’m not certain of the current state though I think it improved a few years ago It did? I feel like it gets slower and slower every time. > I believe it’s still considerably more than one millisecond; 10–20ms is the vague number that’s lodged in my memory, but not supported, and I can’t test it now. (I invite anyone running Windows to provide figures.) Yep. On the Windows 10 machine I have it's around 4ms, though that's antivirus and other similar sources of slowness disabled. You could probably get it closer to 2-3ms on a faster machine, but that's the ballpark.