[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: Sean Conner <sean@...>
- Date: Sat, 23 Apr 2011 05:54:46 -0400
It was thus said that the Great Emmanuel Oga once stated:
> Hi, I implemented my very first C module for a lua program, be nice :-).
>
> Without further ado:
>
> https://gist.github.com/938457
>
> There is a bench program and a test one in case you feel like meddling
> with the code.
What should
subsequence.subsequenceMatch("banunus","as")
return?
Because as is, the code is returning "true". I'm not sure if this is
intended or not. Other observations about the code:
1. luaL_checklstring() will never return NULL. So you can remove
if (pattern && str)
2. You already bound pidx to 0, so it will always be >= 0, so you can change
if (pidx >= 0 && pidx < lpat)
with
if (pidx < lpat)
3. I tend to like early returns; if you do this, any time you assign 1 to
found you can replace it with:
lua_pushboolean(L,1);
return 1;
Then the end of the function:
if (found) {
lua_pushboolean(L,1)
} else {
lua_pushnumber(L,(lua_Number)pidx + 1);
}
return 1;
can now be:
lua_pushnumber(L,pidx + 1);
return 1;
(I can tell you are using a C++ compiler for this, as the C++ style comments
are not strictly legal in C89; also, C++ compilers force one to cast; the
casts here are not strictly necessary in C89)
> 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.
Even with the changes I've made, they didn't show much of an improvement
in speed. But depending upon your answer to my question above, there may be
a way to improve the speed.
-spc