Sure. I cold imagine a printf macro that just expands to static initialization of the format followed by use of that format. There's some good things to be said for brevity, and in seriousness I do not propose this. It is perfectly acceptable to waste CPU cycles and memory, because proportionally speaking they aren't in such a short supply.
Posted Jun 2, 2012 21:27 UTC (Sat) by vonbrand (subscriber, #4458)
[Link]
Oh, come on. The compiler surely could figure out that this is printf, scan the format string and replace the whole printf("Look at this: %d %x\n", a, b) call by something along the lines of puts("Look at this: "); do_fmt_d(a); do_fmt_x(b); putchar('\n');. Probably pointless (if that is your performance bottleneck, you can do something along those lines yourself).
GCC Explorer - an interactive take on compilation
Posted Jun 3, 2012 5:37 UTC (Sun) by quotemstr (subscriber, #45331)
[Link]
Your transformation isn't valid: consider multithreading. One printf will write to a stream as an atomic operation (enforced by internal stdio locks), but the output from discrete output functions may overlap.
GCC Explorer - an interactive take on compilation
Posted Jun 3, 2012 9:23 UTC (Sun) by dgm (subscriber, #49227)
[Link]
That's why a naive coder with a optimizing compiler cannot beat a good coder. The compiler is bound by the precise semantics of the code, even if there are aspects of it that you don't care. The compiler has to care.