Last active
April 15, 2020 11:53
-
-
Save ochaton/19c69c5b4849c96e8a1c6f160f65c477 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| collectgarbage('stop') | |
| local MAX = 1000000 | |
| print("50: for+..", clock.bench(function() for j = 1, MAX do local s = "" for i = 1, 50 do s = s .. i end end end)[1]) | |
| print("50: for+table.insert", clock.bench(function() for j = 1, MAX do local s = {} for i = 1, 50 do table.insert(s, i) end table.concat(s) end end)[1]) | |
| print("50: for+table", clock.bench(function() for j = 1, MAX do local s = {} for i = 1, 50 do s[i] = i end table.concat(s) end end)[1]) | |
| print("50: for+table+new", clock.bench(function() for j = 1, MAX do local s = table.new(50, 0) for i = 1, 50 do s[i] = i end table.concat(s) end end)[1]) | |
| print("100: for+..", clock.bench(function() for j = 1, MAX do local s = "" for i = 1, 100 do s = s .. i end end end)[1]) | |
| print("100: for+table.insert", clock.bench(function() for j = 1, MAX do local s = {} for i = 1, 100 do table.insert(s, i) end table.concat(s) end end)[1]) | |
| print("100: for+table", clock.bench(function() for j = 1, MAX do local s = {} for i = 1, 100 do s[i] = i end table.concat(s) end end)[1]) | |
| print("100: for+table.insert+new", clock.bench(function() for j = 1, MAX do local s = table.new(100, 0) for i = 1, 100 do table.insert(s, i) end table.concat(s) end end)[1]) | |
| print("100: for+table+new", clock.bench(function() for j = 1, MAX do local s = table.new(100, 0) for i = 1, 100 do s[i] = i end table.concat(s) end end)[1]) | |
| --[[ | |
| result: | |
| 50: for+.. 5.137557 | |
| 50: for+table.insert 7.283428 | |
| 50: for+table 6.728287 | |
| 50: for+table+new 4.62105 | |
| 100: for+.. 15.128278 | |
| 100: for+table.insert 14.525568 | |
| 100: for+table 12.227589 | |
| 100: for+table.insert+new 13.138442 | |
| 100: for+table+new 9.673224 | |
| ]] |
local clock = require('clock')
local fun = require('fun')
local MAX = 100000
local function bm(name, n, fn)
print('' .. n .. ': ' .. name, clock.bench(function()
for _ = 1, MAX do
fn()
end
end)[1])
end
local function run(n)
local strs = fun.range(n):map(function(x) return ('a'):rep(300) end):totable()
bm('for+..', n, function()
local s = ""
for _, str in ipairs(strs) do
s = s .. str .. str
end
end)
bm('for+table.insert', n, function()
local s = {}
for _, str in ipairs(strs) do
table.insert(s, str .. str)
end
table.concat(s)
end)
bm('for+table', n, function()
local s = {}
for i, str in ipairs(strs) do
s[i] = str .. str
end
table.concat(s)
end)
bm('for+table+new', n, function()
local s = table.new(n, 0)
for i, str in ipairs(strs) do
s[i] = str .. str
end
table.concat(s)
end)
end
run(50)
run(100)300
50: for+.. 10.411794
50: for+table.insert 0.893488
50: for+table 0.807473
50: for+table+new 0.630997
100: for+.. 39.144871
100: for+table.insert 1.65558
100: for+table 1.395289
100: for+table+new 1.20343
30
50: for+.. 1.049417
50: for+table.insert 0.275828
50: for+table 0.249313
50: for+table+new 0.179999
100: for+.. 3.888137
100: for+table.insert 0.571232
100: for+table 0.470101
100: for+table+new 0.370496
Author
True, on my desktop:
tarantool -l clock bench2.lua
50: for+.. 32.340517
50: for+table.insert 0.54341700000001
50: for+table 0.417396
50: for+table+new 0.22173099999999
100: for+.. 124.873568
100: for+table.insert 1.038552
100: for+table 0.72050400000001
100: for+table+new 0.47638400000002
Ok, it seems that for + .. faster only for short strings and when we concatenate less than hundred of them. In other cases table.concat is much faster.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
here are some larger strings:
with :rep(30)