4 ms·
"Since function calls usually take a long time, getchar is often implemented as a macro." And then I stopped reading. Does anyone know of a meaning for this se
by saucetenuto 17y ago
"Since function calls usually take a long time, getchar is often implemented as a macro."
And then I stopped reading. Does anyone know of a meaning for this sentence that's more sophisticated than "function call overhead is punishing, don't call functions if you can help it", advice that was out of date when C was /invented/, for God's sake? Something special about getchar(), maybe?
- spc476 17y agogetc() and putc() can be implemented as macros in standard C (although a non-macro version needs to be supplied as well), so I think the author was trying to explain why getc() may appear slower in some circumstances than others. Of course, nowadays, it would be difficult to measure the difference between a macro getc() and a function getc().
- visitor4rmindia 17y agoUm...function calls do have an overhead. Yes it is negligible in most cases but it certainly made a difference when C was invented. In fact, C++ still carries the "inline" declaration around to avoid function call overhead. The compiler can ignore when knows better but the fact that inlining exits implies that function call overhead is not imaginary.
- silentbicycle 17y agoGenerally, function call overhead is absolutely worth the added ability to abstract, but adding that overhead per character in all IO operations adds up quickly. Getchar is often the innermost loop of IO-bound processing, so inlining it (in this case, by making it a #define macro) is worth doing as a special case.