###### tags: `tarantool`, `performance` # Tarantool `table` performance --- ## `table.insert` ```lua= local batch = 500 local iters = 1e8 / batch for i = 1, iters, 1 do local t = {} for j = 1, batch do table.insert(t, math.random(0, 100)) end end ``` ``` real 0m4.172s user 0m4.123s sys 0m0.015s ``` --- ## `table[index] = value` ```lua= local batch = 500 local iters = 1e8 / batch for i = 1, iters, 1 do local t = {} for j = 1, batch do t[j] = math.random(0, 100) end end ``` ``` real 0m1.014s user 0m0.973s sys 0m0.010s ``` --- ## C array ```lua= local ffi = require("ffi") local batch = 500 local iters = 1e8 / batch for i = 1, iters, 1 do local t = ffi.new("double[?]", 500) for j = 0, batch-1 do t[j] = math.random(0, 100) end end ``` ``` real 0m0.751s user 0m0.708s sys 0m0.010s ``` --- ## `table.insert` && `table.sort` ```lua= local batch = 500 local iters = 1e8 / batch for i = 1, iters, 1 do local t = {} for j = 1, batch do table.insert(t, math.random(0, 100)) end table.sort(t) end ``` ``` real 0m26.302s user 0m26.171s sys 0m0.054s ``` --- ## `table[index] = value` && `table.sort` ```lua= local batch = 500 local iters = 1e8 / batch for i = 1, iters, 1 do local t = {} for j = 1, batch do t[j] = math.random(0, 100) end table.sort(t) end ``` ``` real 0m22.357s user 0m22.266s sys 0m0.035s ``` --- ## `table[index] = value` && `qsort` ```lua= local function quicksort(array, low, high) if high - low < 1 then return array end local pivot = low for i = low + 1, high do if array[i] <= array[pivot] then if i == pivot + 1 then array[pivot], array[pivot + 1] = array[pivot + 1], array[pivot] else array[pivot], array[pivot + 1], array[i] = array[i], array[pivot], array[pivot + 1] end pivot = pivot + 1 end end array = quicksort(array, low, pivot - 1) return quicksort(array, pivot + 1, high) end local batch = 500 local iters = 1e8 / batch for i = 1, iters do local t = {} for j = 1, 500 do t[j] = math.random(1, 100) end quicksort(t, 1, 500) end ``` ``` real 0m11.459s user 0m11.341s sys 0m0.036s ``` --- ## C-array && `qsort` ```lua= local function quicksort(array, low, high) if high - low < 1 then return array end local pivot = low for i = low + 1, high do if array[i] <= array[pivot] then if i == pivot + 1 then array[pivot], array[pivot + 1] = array[pivot + 1], array[pivot] else array[pivot], array[pivot + 1], array[i] = array[i], array[pivot], array[pivot + 1] end pivot = pivot + 1 end end array = quicksort(array, low, pivot - 1) return quicksort(array, pivot + 1, high) end local ffi = require("ffi") local batch = 500 local iters = 1e8 / batch for i = 1, iters do local arr = ffi.new("double[?]", 500) for j = 0, batch - 1 do arr[j] = math.random(1, 100) end quicksort(arr, 0, 499) end ``` ``` real 0m0.962s user 0m0.919s sys 0m0.011s ```