Created
February 6, 2015 00:30
-
-
Save nefftd/633b5313454ed0b8b8db 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
-- Implementations | |
local impl = {} | |
impl[1] = function(size) | |
local c,r = string.char,math.random | |
local str = {} | |
for i = 1,size do | |
str[i] = c(r(0,255)) | |
end | |
return table.concat(str) | |
end | |
local MAX_UNPACK_SIZE = 50000 | |
impl[2] = function(size) | |
local c,r,u,m = string.char,math.random,(table.unpack or unpack),math.min | |
local str,temp,n = {},{},1 | |
while size > 0 do | |
local L = m(size,MAX_UNPACK_SIZE) | |
for i = 1,L do | |
temp[i] = r(0,255) | |
end | |
str[n] = c(u(temp,1,L)) | |
n = n + 1 | |
size = size - L | |
end | |
return table.concat(str) | |
end | |
local MAX_UNPACK_SIZE = 500000 | |
impl[3] = function(size) | |
local c,r,u,m = string.char,math.random,(table.unpack or unpack),math.min | |
local str,temp,n = {},{},1 | |
while size > 0 do | |
local L = m(size,MAX_UNPACK_SIZE) | |
for i = 1,L do | |
temp[i] = r(0,255) | |
end | |
str[n] = c(u(temp,1,L)) | |
n = n + 1 | |
size = size - L | |
end | |
return table.concat(str) | |
end | |
impl[4] = function(size) | |
local c,r = string.char,math.random | |
return ('.'):rep(size):gsub('.',function() | |
return c(r(0,255)) | |
end) | |
end | |
-- Profile | |
local suf = {'KiB', 'MiB', 'GiB', 'TiB'} | |
function fmtsize(amt) | |
if amt == 0 or amt ~= amt then return 0 end | |
local m = math.min(math.floor((math.log(math.abs(amt)) / math.log(10)) / 3),#suf) | |
local n = amt / 1024 ^ m | |
return ('%.2f %s'):format(n,(suf[m] or 'B')) | |
end | |
collectgarbage('stop') | |
local tests = {10^3, 10^4, 10^5, 10^6, 10^7, 10^8, 10^9} | |
for i,size in ipairs(tests) do | |
for i,func in ipairs(impl) do | |
collectgarbage('collect') | |
collectgarbage('collect') | |
local t = os.clock() | |
func(size) | |
t = os.clock() - t | |
print(('method %d (size %d): %.4f (usage %s)'):format( | |
i, size, t, fmtsize(collectgarbage('count')*1024) | |
)) | |
end | |
end | |
--[[ RESULTS on i5-2520M w/ 6 GiB 1333 MHz RAM on Windows 7 SP1 64-bit, via | |
64-bit Lua 5.2.3 from http://joedf.users.sourceforge.net/luabuilds/: | |
D:\Desktop | |
λ lua test.lua | |
method 1 (size 1000): 0.0000 (usage 56.21 KiB) | |
method 2 (size 1000): 0.0010 (usage 80.90 KiB) | |
method 3 (size 1000): 0.0000 (usage 80.90 KiB) | |
method 4 (size 1000): 0.0010 (usage 42.52 KiB) | |
method 1 (size 10000): 0.0030 (usage 335.26 KiB) | |
method 2 (size 10000): 0.0010 (usage 637.26 KiB) | |
method 3 (size 10000): 0.0020 (usage 637.26 KiB) | |
method 4 (size 10000): 0.0030 (usage 99.07 KiB) | |
method 1 (size 100000): 0.0260 (usage 2.38 MiB) | |
method 2 (size 100000): 0.0160 (usage 2.98 MiB) | |
method 3 (size 100000): 0.0170 (usage 5.46 MiB) | |
method 4 (size 100000): 0.0290 (usage 586.86 KiB) | |
method 1 (size 1000000): 0.2660 (usage 18.99 MiB) | |
method 2 (size 1000000): 0.1270 (usage 8.42 MiB) | |
method 3 (size 1000000): 0.1540 (usage 27.58 MiB) | |
method 4 (size 1000000): 0.2780 (usage 4.90 MiB) | |
method 1 (size 10000000): 2.6880 (usage 297.57 MiB) | |
method 2 (size 10000000): 1.2800 (usage 55.55 MiB) | |
method 3 (size 10000000): 1.4300 (usage 81.94 MiB) | |
method 4 (size 10000000): 2.7770 (usage 60.65 MiB) | |
method 1 (size 100000000): 26.5250 (usage 2.34 GiB) | |
method 2 (size 100000000): 13.2950 (usage 484.08 MiB) | |
method 3 (size 100000000): 13.9930 (usage 553.07 MiB) | |
method 4 (size 100000000): 27.9750 (usage 542.14 MiB) | |
lua: not enough memory | |
NOTES: The test was also repeated the the garbage collector turned on, and the | |
time was (surprisingly) the same across the board. | |
--]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment