Created
December 29, 2020 14:04
-
-
Save dguo/be4f13549e31276327b0098cf98e9b34 to your computer and use it in GitHub Desktop.
blog - How to Concatenate Strings in Lua
This file contains 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
hyperfine 'lua slow.lua' | |
# Benchmark #1: lua slow.lua | |
# Time (mean ± σ): 1.287 s ± 0.115 s [User: 1.120 s, System: 0.078 s] | |
# Range (min … max): 1.187 s … 1.528 s 10 runs | |
hyperfine 'lua fast.lua' | |
# Benchmark #1: lua fast.lua | |
# Time (mean ± σ): 39.3 ms ± 3.8 ms [User: 34.6 ms, System: 2.8 ms] | |
# Range (min … max): 35.3 ms … 58.3 ms 48 runs |
This file contains 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
message = "Hello, " .. "world!" | |
-- message equals "Hello, World!" |
This file contains 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
for i=1,100000 do | |
io.stdout:write(i) | |
end |
This file contains 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
start = "Hello, " | |
message = start .. "world!" | |
start = "Bye, " | |
-- message still equals "Hello, World!" |
This file contains 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
message = "" | |
for i=1,100000 do | |
message = message .. i | |
end |
This file contains 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
message = "Hello, " | |
message ..= "world!" | |
-- results in a "syntax error near '..'" error |
This file contains 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
count = 42 | |
message = "The count is: " .. count | |
-- message equals "The count is: 42" |
This file contains 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
message = table.concat({1, 2, 3, 4, 5}) | |
-- message equals "12345" | |
message = table.concat({1, 2, 3, 4, 5}, ", ") | |
-- message equals "1, 2, 3, 4, 5" |
This file contains 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
message = table.concat({1, 2, 3, 4, 5}, ", ", 2, 4) | |
-- message equals "2, 3, 4" |
This file contains 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
numbers = {} | |
for i=1,100000 do | |
numbers[i] = i | |
end | |
message = table.concat(numbers) |
This file contains 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
count = nil | |
message = "The count is: " .. count | |
-- results in an "attempt to concatenate a nil value" error |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment