[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: help optimizing a small lua function implemented in a C module.
- From: Emmanuel Oga <emmanueloga@...>
- Date: Tue, 26 Apr 2011 22:16:37 -0300
On Tue, Apr 26, 2011 at 6:38 PM, Mike Pall <mikelu-1104@mike.de> wrote:
> Emmanuel Oga wrote:
>> I'm pretty happy with the 24x speed up for lua and 8x speed up for
>> luajit but since I'm running the function over thousands of strings
>> I'm wondering if a little more speed could be squeezed on the C side.
>
> Not unexpectedly, I'm wondering why not squeeze out a little more
> speed on the Lua side:
>
> local byte = string.byte
> local function ssm(pattern, str, start)
> local pidx = start or 1
> for i=1,#str do
> if byte(str, i) == byte(pattern, pidx) then pidx = pidx + 1 end
> if pidx > #pattern then return true end
> end
> return pidx
> end
>
> With LuaJIT that's 8x faster than the original Lua code.
> I.e. pretty close to the performance of C.
Nice, I was pretty sure creating those tables out of the byte calls
was not very efficient, but it was better than doing string.sub in
each string and comparing the strings (my previous 'naive'
implementation :p).
So now with luajit:
Old Version
lua 2.87
C ext 0.26
C Speed Up: 11.038461538462
With your implementation:
lua 0.36
C ext 0.25
C Speed Up: 1.44
-----------------------------------------------------------------
But in plain lua I get the reverse effect:
Old Version
lua 12.3
C ext 0.44
C Speed Up: 27.954545454545
Your version:
lua 16.6
C ext 0.43
C Speed Up: 38.604651162791
I'm guessing that's because calling string.byte is a lot more
expensive in plain lua than in the jit interpreter.
> --Mike
>
>