loopCount = 1000000--100萬 table_a = {} for i=1,1000do table_a[i]= i end require"socket"
clock_x = socket.gettime() for i=1, loopCount do for i=1, #table_a do x = table_a[i] end end print(string.format('1. elapsed: %.2f ms', (socket.gettime() - clock_x)*1000 ))
clock_x = socket.gettime() for i=1, loopCount do local leng = #table_a for i=1, leng do x = table_a[i] end end print(string.format('2. elapsed: %.2f ms', (socket.gettime() - clock_x)*1000 ))
clock_x = socket.gettime() for i=1, loopCount do for i,v inpairs(table_a) do x = v end end print(string.format('3. elapsed: %.2f ms', (socket.gettime() - clock_x)*1000 ))
clock_x = socket.gettime() for i=1, loopCount do for i,v inipairs(table_a) do x = v end end print(string.format('4. elapsed: %.2f ms',(socket.gettime() - clock_x)*1000 ))