lua-users home
lua-l archive

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


On 20/12/2012, at 7:52 AM, Luiz Henrique de Figueiredo <lhf@tecgraf.puc-rio.br> wrote:

>> As far as I can tell, if you're using PUC Lua, then yes, a numeric for is faster than ipairs, but a while loop is even better. 
> 
> On Mac OS X 10.6.8 running in an old mini, numeric for wins:
> 
> % lua -v test_ipairs.lua 
> Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
> while: 0.749045s, 3.3343334e+14
> ipairs: 1.332448s, 3.3343334e+14
> fixed-length numeric_for: 0.519153s, 3.3343334e+14
> numeric_for and #: 0.518728s, 3.3343334e+14
> 
> % lua -v test_ipairs.lua 
> Lua 5.2.1  Copyright (C) 1994-2012 Lua.org, PUC-Rio
> while: 0.736432s, 3.3343334e+14
> ipairs: 1.189561s, 3.3343334e+14
> fixed-length numeric_for: 0.58731s, 3.3343334e+14
> numeric_for and #: 0.58876s, 3.3343334e+14

Interesting!  On a year-or-so old MBP with OS X 10.8.2, while is best for 5.1, numeric for is best for 5.2 (sorry, I hadn't tried that before), and LuaJIT doesn't care.

$ uname -v
Darwin Kernel Version 12.2.0: Sat Aug 25 00:48:52 PDT 2012; root:xnu-2050.18.24~1/RELEASE_X86_64

$ lua -v test_ipairs.lua 
Lua 5.1.5  Copyright (C) 1994-2012 Lua.org, PUC-Rio
while: 0.472889s, 3.3343334e+14
ipairs: 0.964868s, 3.3343334e+14
fixed-length numeric_for: 0.541613s, 3.3343334e+14
numeric_for and #: 0.540567s, 3.3343334e+14


$ ~/software/lua-5.2.1/src/lua -v test_ipairs.lua 
Lua 5.2.1  Copyright (C) 1994-2012 Lua.org, PUC-Rio
while: 0.453054s, 3.3343334e+14
ipairs: 0.704332s, 3.3343334e+14
fixed-length numeric_for: 0.361604s, 3.3343334e+14
numeric_for and #: 0.362802s, 3.3343334e+14


$ luajit -v test_ipairs.lua 
LuaJIT 2.0.0 -- Copyright (C) 2005-2012 Mike Pall. http://luajit.org/
while: 0.235889s, 3.3343334e+15
ipairs: 0.231344s, 3.3343334e+15
fixed-length numeric_for: 0.234711s, 3.3343334e+15
numeric_for and #: 0.233434s, 3.3343334e+15