4 ms·
Strings operations is a tricky part though. It's easy to slow the code down with "obvious" coding - which is a bad thing in a language. Some other operations
by ay 16y ago
Strings operations is a tricky part though.
It's easy to slow the code down with "obvious" coding - which is a bad thing in a language.
Some other operations like metatable lookup might not be always the fastest.
But overall the convenience of having a 700K source tarball that compiles on any ANSI C into a modern language blows all these concerns away for my use. And the performance-critical parts can be easily pushed down to C. If they are not premature optimizations to begin with.
- silentbicycle 16y agoHow are string ops tricky in Lua? Patterns and table.concat can get a lot done, and when you need more power, there's LPEG (which is also quite speedy).
- ay 16y ago"Patterns and table.concat can get a lot done" - sure. but it is may feel somewhat backasswards :-) $ time lua -e 's="";for i=1,100000 do s=s.."-";end' real 0m1.292s user 0m1.029s sys 0m0.108s $ time lua -e 't={};for i=1,100000 do table.insert(t,"-");end; s=table.concat(t,"")' real 0m0.284s user 0m0.062s sys 0m0.077s times are somewhat bogus since it is cygwin under win7, but they give the impression of what I am talking about. I do not argue it is a feature very coherent with the design of the language, and the immutable strings have other good properties. But if you do intensive string ops you need to watch out not to fall into the trap of treating the strings as mutable objects. Don't get me wrong - I like Lua a lot and use it almost on a daily basis. Just that I think the posts like this may attract some new folks to try Lua out - and they deserve to know the obvious ways they can shoot themselves in the foot in order to avoid some disappointment.
- silentbicycle 16y agoContext: Since Lua strings are immutable (and interned, like Erlang atoms, Lisp symbols, etc.), tables can be used as string buffers, and concatenation to large strings can be done in one step via table.concat. Appending to a string in a loop is slow, but that's true of any language with immutable strings. $ time lua -e 't={};for i=1,100000 do table.insert(t,"-");end; s=table.concat(t,"")' 0m0.05s real 0m0.05s user 0m0.01s system 0m0.05s real 0m0.05s user 0m0.00s system 0m0.05s real 0m0.04s user 0m0.02s system (Times on OpenBSD/amd64 4.7, Lua 5.1.4.) table.insert checks the table's length on each iteration, which dominates the string-handling time - len is O(lg n). If you just use the loop index (or otherwise cache it), it cuts the time in half: $ time lua -e 't={};for i=1,100000 do t[i] = "-"; end; s=table.concat(t,"")' 0m0.02s real 0m0.02s user 0m0.00s system 0m0.02s real 0m0.02s user 0m0.01s system 0m0.02s real 0m0.03s user 0m0.00s system Changing to i=1,1000000 gave me an avg. of 0.54s and 0.22s respectively. LuaJIT is probably also considerably better. (You could also just do t = string.rep('-', 100000), but that's not the point of your benchmark.)