lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


>> In the debug mode of my project, I did a lot log using print().
>> There're many string concats and other calculation in the
>> arguments of print func.
>>
>> In release mode, I assign the print to:
>>   function dumb_print() end
>> to hide the logs.
>>
>> But the arguments for print are still evaluated, is it possible
>>  to avoid this overhead?

Rebel Neurofog wrote:
> I guess, with something like
> 
> function do_something (f)
>    print (f ())
> end
> 
> -- Uncomment for release mode:
> -- function do_something () end
> 
> do_something (function () return "Error: "..obj.get_error () end)
> 
> This technique will enlarge VM's code but execution-time will be minimal

It would also create a closure each time the (inactive) function is
called. In a loop that might hurt.

I guess the only real way to stop argument evaluation in Lua are the
boolean operators.

    logprint(logflag and <expression>)

In release mode you could then set logprint to your dummy function and
logflag to nil. Of course that would mean you would have to change all
your existing code and multiple arguments might cause trouble. (logflag
and arg1, logflag and arg2, ...) Multiple return values from a single
expression are impossible this way. string.format might help, but it
wouldn't look pretty.


The only other choice known to me is to do a replace on all your source
files to remove or comment any call to the print() function for the
release version.

  src = src:gsub("print%s*%b()", "")

or

  src = src:gsub("print%s*%b()", "--[===[%0]===]")

(Choose a comment level that isn't used otherwise).

This is probably the fastest way for already existing code.

-- David