Skip to content

Instantly share code, notes, and snippets.

@ochaton
Last active April 15, 2020 11:53
Show Gist options
  • Select an option

  • Save ochaton/19c69c5b4849c96e8a1c6f160f65c477 to your computer and use it in GitHub Desktop.

Select an option

Save ochaton/19c69c5b4849c96e8a1c6f160f65c477 to your computer and use it in GitHub Desktop.
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
]]
@printercu
Copy link
Copy Markdown

here are some larger strings:

local clock = require('clock')
local fun = require('fun')
local MAX = 100000

local function bm(fn)
    return clock.bench(function()
        for _ = 1, MAX do
            fn()
        end
    end)
end

local function run(n)
    local strs = fun.range(n):map(function(x) return ('a'):rep(300) end):totable()
    print('' .. n .. ': for+..', bm(function()
        local s = ""
        for _, str in pairs(strs) do
            s = s .. str .. str
        end
    end)[1])
    print('' .. n .. ': for+table.insert', bm(function()
        local s = {}
        for _, str in pairs(strs) do
            table.insert(s, str .. str)
        end
        table.concat(s)
    end)[1])
    print('' .. n .. ': for+table', bm(function()
        local s = {}
        for i, str in pairs(strs) do
            s[i] = str .. str
        end
        table.concat(s)
    end)[1])
    print('' .. n .. ': for+table+new', bm(function()
        local s = table.new(n, 0)
        for i, str in pairs(strs) do
            s[i] = str .. str
        end
        table.concat(s)
    end)[1])
end

run(50)
run(100)
50: for+..	10.69397
50: for+table.insert	1.271426
50: for+table	1.061971
50: for+table+new	1.018673
100: for+..	43.339186
100: for+table.insert	2.587557
100: for+table	2.150612
100: for+table+new	2.061154

with :rep(30)

50: for+..	1.185899
50: for+table.insert	0.422421
50: for+table	0.342766
50: for+table+new	0.296029
100: for+..	4.399439
100: for+table.insert	0.886914
100: for+table	0.649508
100: for+table+new	0.572523

@printercu
Copy link
Copy Markdown

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

@ochaton
Copy link
Copy Markdown
Author

ochaton commented Apr 15, 2020

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