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
-- this version is very slow, as every time we update the buffer, we creating a new string, which | |
-- makes the original buffer unreferenced. and lua's gc will kicks in often to clean up memory | |
-- which is really realy slow. | |
local file = io.open("file.txt", "w") | |
local buffer = '' | |
for i = 1, 500000000 do | |
local n = i % math.random(10000) | |
local str = string.format("This is a number %d\n", n) | |
buffer = buffer .. str -- make the original string buffer unreferenced | |
if i % 10000 == 0 then |
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
// The code below would print overlapped A and B sequences like: | |
// ... | |
// A | |
// A | |
// B | |
// B | |
// ... | |
// | |
// Please add multi-threading protection code to make sure that | |
// As and Bs are printed in the order of: |