ch05 exercises
This commit is contained in:
31
exercises/ch05/ex5_8.lua
Normal file
31
exercises/ch05/ex5_8.lua
Normal 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, "%"))
|
||||
Reference in New Issue
Block a user