Echo Help

User avatar
Heiko
Site Admin
Posts: 1548
Joined: Wed Mar 11, 2009 6:26 pm

Re: Echo Help

Post by Heiko »

tsuujin wrote:
demonnic wrote:In this case, "better" is a matter of opinion. Either one works just fine. Some folks prefer concat, some folks prefer string.format . I imagine a lot of it has to do with your coding roots.
No, I spent a good deal of time looking this up a while back. string.format ends up being a lot faster than concatenations, because the lua engine creates an entirely new memory address for each concat. So if you concat three times, you end up with four separate concatenation functions and the memory sits until the GC clears it up. String.format, however, is done outside of the lua engine on the C side and doesn't suffer this problem.

There are problems with the GC and concat when used rapidly (like a combat system tends to do).
Exactly! Lua is very slow on string concats of any kind. string.format is definitely a lot faster, but if performance is an issue you wouldn't want to use cecho() anyways but use setFgColor() and echo() directly.

Though for most users performance is never actually an issue they have to care about simply because their systems don't have very high performance requirements.

User avatar
tsuujin
Posts: 695
Joined: Fri Feb 26, 2010 12:59 am
Location: California
Contact:

Re: Echo Help

Post by tsuujin »

Heiko wrote: Exactly! Lua is very slow on string concats of any kind. string.format is definitely a lot faster, but if performance is an issue you wouldn't want to use cecho() anyways but use setFgColor() and echo() directly.

Though for most users performance is never actually an issue they have to care about simply because their systems don't have very high performance requirements.
I kind of view it as a trade-off. Cecho is very convenient and i use string.format to mitigate some of the lost cycles.

Besides, I personally find string.format to be -much- easier to read and debug.

User avatar
demonnic
Posts: 886
Joined: Sat Dec 05, 2009 3:19 pm

Re: Echo Help

Post by demonnic »

I quite like reading formats like string.format, and find it to be fairly easy... but I've noticed when working with people new to scripting or programming in general that the idea of .. = addition for strings seems a little bit easier to explain than string.format("put the string here %s then it will %s everything", "and", "parse") ... I think I got the syntax right, it's been awhile. Either way, I think I'll be using that one more now, especially in code that gets repeated often.

Denarii
Posts: 111
Joined: Thu Dec 03, 2009 10:54 pm

Re: Echo Help

Post by Denarii »

Heiko wrote:if performance is an issue you wouldn't want to use cecho() anyways but use setFgColor() and echo() directly.
While this is technically true, if you're having performance issues such that you need to use setFgColor() and echo() over cecho(), you probably have bigger problems than cecho()'s performance footprint. showColors(), for example, calls cecho 224 times, calculates the luminosity of a color 224 times, uses .. string concatenation 1344 times as well as some other stuff and it executes in around 100-200ms for me. I would venture to guess that most of the time people won't be calling cecho or using concatenation nearly that many times in a single chunk.

User avatar
tsuujin
Posts: 695
Joined: Fri Feb 26, 2010 12:59 am
Location: California
Contact:

Re: Echo Help

Post by tsuujin »

Denarii wrote:
Heiko wrote:if performance is an issue you wouldn't want to use cecho() anyways but use setFgColor() and echo() directly.
While this is technically true, if you're having performance issues such that you need to use setFgColor() and echo() over cecho(), you probably have bigger problems than cecho()'s performance footprint. showColors(), for example, calls cecho 224 times, calculates the luminosity of a color 224 times, uses .. string concatenation 1344 times as well as some other stuff and it executes in around 100-200ms for me. I would venture to guess that most of the time people won't be calling cecho or using concatenation nearly that many times in a single chunk.
I'd just like to point out that this amount of string concatenation in such a short period is very likely more than enough to cause the GC problems I was talking about earlier, leading to horrible memory leaks.

Daiger
Posts: 7
Joined: Sat Mar 13, 2010 8:16 pm

Re: Echo Help

Post by Daiger »

Hmm... so, would it be better to make aliases like this:
send (string.format("raze %s", target))
instead of
send ("raze " .. target) ?

Also not entirely related but, I read somewhere that "exact match" triggers are faster than perl regexp, so should I only use regexp when there's no other option? If I have a trigger like "^The bones in your (left|right) (arm|leg) mend.", should I instead make it four triggers, or is it faster this way?

User avatar
tsuujin
Posts: 695
Joined: Fri Feb 26, 2010 12:59 am
Location: California
Contact:

Re: Echo Help

Post by tsuujin »

Daiger wrote:Hmm... so, would it be better to make aliases like this:
send (string.format("raze %s", target))
instead of
send ("raze " .. target) ?

Also not entirely related but, I read somewhere that "exact match" triggers are faster than perl regexp, so should I only use regexp when there's no other option? If I have a trigger like "^The bones in your (left|right) (arm|leg) mend.", should I instead make it four triggers, or is it faster this way?
For the first point: It'd be a good idea to make a habit of using string.format, but for aliases it's not that big a deal because you won't be calling them (in general) all that often.

Second: regex is a very slow option in comparison with an exact match. However, don't be afraid to use regex. Realistically you can have a ton of regex and still parse them faster than you would be able to notice. If speed is an issue for you, use regex shielding. Basically, you create one trigger that uses an exact match or beginning of line substring (which I believe is the fastest option) and have that trigger do nothing except to chain to another trigger which is your regex. This way, the regex only ever gets called when there's a very good chance it'll match otherwise it's never compiled.

User avatar
Vadi
Posts: 5042
Joined: Sat Mar 14, 2009 3:13 pm

Re: Echo Help

Post by Vadi »

Regexes, like the Lua scripts are pre-compiled (compiled when you save them, hence why if you also introduce a syntax error in script or regex you get a warning right away).

Post Reply