Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Demonstrate the difference of time cost ieterating over ffi array of double versus native table both using luajit
- require "os"
- local ffi = require("ffi")
- local num_ele = 900000
- local num_pass = 50000
- function elap(beg_time, msg)
- local end_time = os.clock()
- print ("elap " .. msg .. " " .. end_time - beg_time .. "sec")
- return os.clock()
- end
- local begt = os.clock()
- local arr = ffi.new("double[?]", num_ele + 1)
- begt = elap(begt, "allocate ffi")
- -- fill the array for the example, here you would fill it from a file
- local begt = os.clock()
- for i = 0, num_ele do
- arr[i] = i
- end
- begt = elap(begt, "fill ffi array")
- for pass = 0, num_pass do
- local sum = 0
- for i = 0, 9999 do
- sum = sum + arr[i]
- end
- end
- begt = elap(begt, "sum the ffi arrray")
- --------------
- --- Test with Native Lua Table
- --------------
- tt = {}
- local begt = os.clock()
- for i = 0, num_ele do
- tt[i] = i
- end
- begt = elap(begt, "fill native table ")
- local sum2 = 0
- for pass = 0, num_pass do
- for i = 0, num_ele do
- sum2 = sum2 + tt[i]
- end
- end
- begt = elap(begt, "sum the lua arrray")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement