Created
September 13, 2013 09:55
-
-
Save Yonaba/6548708 to your computer and use it in GitHub Desktop.
Next Iterator versus Ipairs for iteration in Lua arrays.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Evaluates times | |
local function time(nm,times, f,...) | |
local st = os.clock() | |
for i = 1, times do | |
f(...) | |
end | |
local et = os.clock() | |
print(('(%s): Times (%d): %.2f ms') | |
:format(nm, times, (et-st)*1000)) | |
end | |
-- next iterator | |
local function test_ntk(t) | |
for i,v in next,t do | |
end | |
end | |
-- next iterator, localized | |
local _next = next | |
local function test_lntk(t) | |
for i,v in _next,t do | |
end | |
end | |
-- Ipairs iterator | |
local function test_ipairs(t) | |
for i,v in ipairs(t) do | |
end | |
end | |
-- Ipairs iteratork, localized | |
local _ipairs = ipairs | |
local function test_lipairs(t) | |
for i,v in _ipairs(t) do | |
end | |
end | |
-- Makes a table of 'len' entries | |
local function make_table(len) | |
local t = {} | |
for i = 1,len do | |
t[i] =i | |
end | |
return t | |
end | |
local t = make_table(1e5) | |
local reps = 100 | |
time(' ltknil', reps, test_lntk, t) | |
time(' tknil', reps, test_ntk, t) | |
time('lipairs', reps, test_ipairs, t) | |
time(' ipairs', reps, test_lipairs, t) |
Author
Yonaba
commented
Sep 13, 2013
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment