2 ms·
Why do you insist on making this a flame war? Sit down and write a program that makes 10M trivial system calls (getpid() is a good choice) and one that calls
by ajross 18y ago
Why do you insist on making this a flame war? Sit down and write a program that makes 10M trivial system calls (getpid() is a good choice) and one that calls a function to return a constant integer 10M times.
I'm sorry, but you seem to have a wildly inflated idea of the speed of system calls on modern OSs. The real world just doesn't work like that. This is my last post on this thread.
- tptacek 18y agoHere you go: http://pastie.org/230109 http://pastie.org/230109 Want the punch line? OSX getpid averages under 100 cycles; snprintf (no IO) averages over 1000.
- ajross 18y agoAre you sure getpid() is doing a system call and not reading TLS?
- tptacek 18y agoWhat a great question! I just checked. Nope: Breakpoint 1, 0x96210c44 in getpid () (gdb) disp/i $eip 2: x/i $eip 0x96210c44 <getpid>: call 0x96210c49 <getpid+5> (gdb) stepi 0x96210c49 in getpid () 2: x/i $eip 0x96210c49 <getpid+5>: pop %ecx 0x96210c4a in getpid () 2: x/i $eip 0x96210c4a <getpid+6>: lea 0xa6427a3(%ecx),%ecx 0x96210c50 in getpid () 2: x/i $eip 0x96210c50 <getpid+12>: mov (%ecx),%eax 0x96210c52 in getpid () 2: x/i $eip 0x96210c52 <getpid+14>: test %eax,%eax 0x96210c54 in getpid () 2: x/i $eip 0x96210c54 <getpid+16>: jle 0x96210c57 <getpid+19> 0x96210c57 in getpid () 2: x/i $eip 0x96210c57 <getpid+19>: mov $0x14,%eax 0x96210c5c in getpid () 2: x/i $eip 0x96210c5c <getpid+24>: call 0x961e8cf4 <_sysenter_trap>
- tptacek 18y agoBy the way, you asked me to time getpid against "a function that returns a constant integer". Obviously that's a silly comparison (getpid isn't really returning a constant; it's a vectored call and it has to pull the PID out of a structure offset from %%fs). But here's a fairer fight: getpid vs. a trivial LFSR: s = (s >> 1) ^ (-(s & 1) & 0xd0000001); I also timed it the way you asked, with wall clock time over 100M iterations (10M runs too fast to time). Surprising even me, getpid wins by almost a full second. Got another challenge for me to write code for?
- spc476 18y agoI did that a few months ago (http://boston.conman.org/2007/11/30.1 http://boston.conman.org/2007/11/30.1) and found some surprising results (http://boston.conman.org/2007/12/02.1 http://boston.conman.org/2007/12/02.1)which showed that using getpid() wasn't a good test of system call overhead.
- tptacek 18y agoThat is a really nice catch.