ex5_8.lua 663 B

12345678910111213141516171819202122232425262728293031
  1. local function concat(strs)
  2. local res = ""
  3. for i = 1, #strs do
  4. res = res .. strs[i]
  5. end
  6. return res
  7. end
  8. local function random_str()
  9. return tostring(math.random(100000, 999999))
  10. end
  11. local strs = {}
  12. for i = 1, 200000 do
  13. strs[i] = random_str()
  14. end
  15. local time_start = os.clock()
  16. concat(strs)
  17. local own_concat_time = os.clock() - time_start
  18. print("Own concat time: " .. own_concat_time)
  19. time_start = os.clock()
  20. _ = table.concat(strs)
  21. local table_concat_time = os.clock() - time_start
  22. print("table.concat time: " .. table_concat_time)
  23. print(("table.concat was %f%s faster"):format(own_concat_time / table_concat_time * 100, "%"))