Optimizing printf for speed makes little sense, since character I / O will be the dominant runtime. Even quick formatting processing will not save you the time needed to send your characters along the way.
The only thing that may cost here is size: the size of the stack, avoiding another level of function invocation and code size, writing as few output functions as possible.
The first thing to do to optimize your code is to turn your tests into a switch. Most likely, the compiler will implement it as a table jump, which is more efficient than a bunch of tests. You can put the “most likely” formats at first, but if you can't read the user mind, it's pretty difficult to predict whether he / she would be better off displaying integers or floating or characters.
Passing a variable number of parameters is the most expensive operation, so delegating the vararg processing to each atomic formatting function will only produce more voluminous and slower code.
Instead, you'd better write your output code as built-in functions and hope that the compiler expands them to save you another level of function calls.
For the various possible sizes of the arguments, the way is to make all the values as large as possible (32 or 64 bits of integers, floats or doubles, depending on what you want to support) and have a limited number of display functions (integer integers / unsigned / double, for example).
Thus, you reduce the size of the source and executable code.
source share