| 12345678910111213141516171819202122232425262728293031 |
- local function concat(strs)
- local res = ""
- for i = 1, #strs do
- res = res .. strs[i]
- end
- return res
- end
- local function random_str()
- return tostring(math.random(100000, 999999))
- end
- local strs = {}
- for i = 1, 200000 do
- strs[i] = random_str()
- end
- local time_start = os.clock()
- concat(strs)
- local own_concat_time = os.clock() - time_start
- print("Own concat time: " .. own_concat_time)
- time_start = os.clock()
- _ = table.concat(strs)
- local table_concat_time = os.clock() - time_start
- print("table.concat time: " .. table_concat_time)
- print(("table.concat was %f%s faster"):format(own_concat_time / table_concat_time * 100, "%"))
|