ch05 exercises

This commit is contained in:
2026-02-25 23:58:37 +02:00
parent 53d5f2878c
commit d91930f1f9
4 changed files with 135 additions and 0 deletions

31
exercises/ch05/ex5_8.lua Normal file
View File

@@ -0,0 +1,31 @@
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, "%"))