lua-users home
lua-l archive

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


You may use luasocket’s “socket.sleep(time)” function.

 

From: Ong hean kuan [mailto:mysurface@gmail.com]
Sent: terça-feira, 17 de junho de 2008 02:27
To: Lua list
Subject: Re: cpu usage increase during runing script access c function.

 

Hi List:

I find that its my Lua scripts there access os.time() in for loop cause the CPU usage raised to very high. After adding a milisecond sleep implement from c++, performance greatly improved.

By the way, will Lua implement nanosleep or any sleep function in future? or It does already exist?

Thanks,

On Tue, Jun 17, 2008 at 8:53 AM, Ong hean kuan <mysurface@gmail.com> wrote:

Thanks, my reply inline.

On Mon, Jun 16, 2008 at 8:28 PM, Matthew Paul Del Buono <delbu9c1@erau.edu> wrote:

There are only two things that stand out at me, both from the same function UCSimScript::Execute()

>
>   int UCSimScript::Execute(char * filename)
>   {
>       const struct luaL_reg oci [] = {
>           {"Call", &UCSimScript::L_Call},  //L_Call is
>   a static function , i includes this function as
>   below
>           {"CallAck", &UCSimScript::L_CallAck},
>           {"Alert", &UCSimScript::L_Alert},
>           {"Answer", &UCSimScript::L_Answer},
>           {"Release", &UCSimScript::L_Release},
>           {"ReleaseComplete",
>   &UCSimScript::L_ReleaseComplete},
>           {"Refid", &UCSimScript::L_Refid},
>           {NULL, NULL}  /* sentinel */
>       };

Is there any particular reason that this isn't static? Since it's not, you're recreating this on every call to Execute() which, while not a lot of time, is significant if it's being called 400 times per second.

It also leads into the second issue: Aren't you reloading your library on every call to Execute()? That means that Lua has to GC your old library and open your new library. Again, if this is happening 400 times per second, that could be pretty bad.

I may be misunderstanding your code, but I believe that you are using the same state for this. If that's the case, you should either load the library at construction instead of execution, or check to see if the library already exists before sticking it in.

 

I didn't call Execute() 400 times per seconds. In fact, the Execute is been call only once, to load up the script. The c functions that been called for many times are those static functions like L_Call and L_CallAck etc.





As a side note, all of this can be done with the auxiliary library using luaL_check* or luaL_opt* and would make your code quite a bit cleaner, imho:


>       //first param (direction)
>       if ( lua_type(l, 1) == LUA_TNUMBER )
>       {
>           dir = lua_tointeger(l, 1);
>       }
>       else if ( lua_type(l, 1) != LUA_TNONE )
>           Lua_Error("Param 1 with wrong type or
>   value");

dir = luaL_checknumber(l, 1);


Thanks for the suggestion, but  luaL_checknumber() will raise  an error if the param is not define or wrong type, which what  i don't want it to raise an error, the Lua_Error() is my own function to log down the error msg but the scripts will carry on executing.

 


>
>       //2nd param (refid)
>       if ( lua_type(l, 2) == LUA_TSTRING )
>       {
>          snprintf(tmp,sizeof tmp,"%s" ,
>   lua_tostring(l, 2) );
>          refid=strtoull(tmp,NULL,16);
>       }
>       else if ( lua_type(l, 2) != LUA_TNONE )
>           Lua_Error("Param 2 with wrong type or
>   value");

refid = luaL_optstring(l, 2, NULL);


etc, etc...

Good luck,
-- Matthew P. Del Buono






--
Hean Kuan Ong
http://linux.byexamples.com
http://cc.byexamples.com
http://wordpress.byexamples.com