This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.
| Index Nav: | [Date Index] [Subject Index] [Author Index] [Thread Index] | |
|---|---|---|
| Message Nav: | [Date Prev] [Date Next] | [Thread Prev] [Thread Next] |
| Other format: | [Raw text] | |
Am Dienstag, 29. November 2005 11:13 schrieb Hayim Shaul:
> Hi all,
>
> I am trying to write a smart logger that can be activated/deactivated at
> run-time. I have a print function for the logger that gets va_list:
>
> void tLogger::print(const char *format, ...) {
> if (!is_activated())
> return;
> va_list marker;
> va_start(marker, format);
> etc. etc.
>
>
> Trouble is with performance. When I call the print function while the
> logger is not activated arguments are still passed to the function. This
> is very time-consuming.
>
> Is there a way to avoid passing unnecessarily extra arguments?
>
> Obviously I can write my code as:
>
> if (logger->is_activated())
> logger->print("%d\n", ackerman(5,5));
>
> But this is very tiring.
> I created a MACRO
>
> #define LOG if (logger->is_activated()) logger->print
>
> and use it:
> LOG("%d\n", ackerman(5,5));
>
> but this seems to me a very fragile solution.
>
> I tried an inline function (hoping for the optimizer would kick in) but
> it was no good.
>
> Is there an more elegant solution for this?
>
> Hayim.
Though your question doesn't belong to this list. Your need is cannot be
fullfilled logically:
- You want to pass arguments to a function only if a certain function gives
true.
- but you want to evaluate the switching function after you called the
function
- this means you want to have a function interface that is not only loading a
variable number of arguments but also loads it's argument at some variable
time (possibly delayed) and from a context that is above the current stack
context, when the function has been called.
- what you can do is: You can build your own output lists (not va_arg, which
is no more than a simple array).
I assume your problem actual performance problem is the generation of
arguments (ackerman(5,5)), which should only be executed if the arguments are
really needed.
You can do this by not passing a pointer to the function (or class) object
(&ackerman) and the arguments to the interface (5,5) and call the evaluation
from inside the logger.
I think there exist such template models in C++, but you shouldn't break your
head and long for a simpler solution since it is better to spend the time you
will need to solve the odds and ends of a fully autmoatic solution of delayed
evaluation, into a good course of "ten finger super high speed secretary
typing" and simply push in those if (...) lines.
The great advantage of these simple if decisions is that you will understand
your code one year later and other might want to read it too and will be
able to understand it.
Attachment:
pgp00000.pgp
Description: PGP signature
| Index Nav: | [Date Index] [Subject Index] [Author Index] [Thread Index] | |
|---|---|---|
| Message Nav: | [Date Prev] [Date Next] | [Thread Prev] [Thread Next] |