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 | |
| ]] |
printercu
commented
Apr 15, 2020
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