読者です 読者をやめる 読者になる 読者になる

TI-Nspire & Lua / スクリプティングのヒント / テーブルの最適化 / 不要な再ハッシュは避ける

Reference: http://adriweb.free.fr/t3/2014/Advanced_TI-Nspire_Lua_Programming.pptx, p.6
“ Again, help Lua know more about the tables you're going to use! Avoid useless rehashes when possible! ”
 

-- 1
timeStart = timer.getMilliSecCounter()
for i = 1, 500000 do
   local polyline = {   -- ★★
      {x = 10, y = 20},
      {x = 15, y = 20},
      {x = 30, y = 20}
   }
end
print("method 1 takes "..timer.getMilliSecCounter() - timeStart.." msec")

-- 2
timeStart = timer.getMilliSecCounter()
for i = 1, 500000 do
   local polyline = {   -- ★★
      {10, 20},
      {15, 20},
      {30, 20}
   }
end
print("method 2 takes "..timer.getMilliSecCounter() - timeStart.." msec")

-- 3
timeStart = timer.getMilliSecCounter()
for i = 1, 500000 do
   local polyline = {   -- ★★
      x = {10, 15, 30},
      y = {20, 20, 20},
   }
end
print("method 3 takes "..timer.getMilliSecCounter() - timeStart.." msec")

-- 4
timeStart = timer.getMilliSecCounter()
for i = 1, 500000 do
   local polyline = {10, 15, 30, 20, 20, 20}   -- ★★
end
print("method 4 takes "..timer.getMilliSecCounter() - timeStart.." msec")

f:id:ti-nspire:20170131153649p:plain

広告を非表示にする