View previous topic :: View next topic |
Author |
Message |
MateeJr GT Advanced Cheater Reputation: 0
Joined: 24 Dec 2017 Posts: 66
|
Posted: Sun Jun 03, 2018 9:15 pm Post subject: Random Executing() |
|
|
Executing / Generating random Text
_________________
Hi Lynxz Gaming |
|
Back to top |
|
|
FreeER Grandmaster Cheater Supreme Reputation: 53
Joined: 09 Aug 2013 Posts: 1091
|
Posted: Mon Jun 04, 2018 1:46 pm Post subject: |
|
|
Code: | local function randomAlpha() return string.char(math.random(('a'):byte(), ('z'):byte())) end
local function randomText(len)
if not len or type(len) ~= 'number' or len < 1 then error('Invalid len to randomText', 2) end
local res = {}
for i = 0, len do res[#res+1] = randomAlpha() end
return table.concat(res)
end
print(randomText(241)) -- print 241 random letters
|
_________________
|
|
Back to top |
|
|
Corroder Grandmaster Cheater Supreme Reputation: 75
Joined: 10 Apr 2015 Posts: 1667
|
Posted: Mon Jun 04, 2018 5:54 pm Post subject: |
|
|
or
Code: | local function RandomText( len )
str = ""
for i = 0, len do
str = str .. string.char( math.random(97,122) )
end
return str
end
print(RandomText(241)) |
_________________
Stealing Code From Stolen Code...
And Admit It.. Hmmm....Typically LOL |
|
Back to top |
|
|
FreeER Grandmaster Cheater Supreme Reputation: 53
Joined: 09 Aug 2013 Posts: 1091
|
Posted: Mon Jun 04, 2018 6:00 pm Post subject: |
|
|
yeah, but iirc lua strings are immutable and concatenating like that is kind of bad memory wise
concatenating 1 character to a 200 character string has to allocate a 201 character string, copy 200 characters then add the new one, adding another allocates a new 201 characters repeat
The following measuring code results in
FreeER 5.2978515625
Corroder 37.6201171875
at 1000
FreeER 18.0390625
Corroder 523.8544921875
increase the length to 10000 and you get
FreeER 282.8828125
Corroder 49175.725585938
at 100000
FreeER 2386.890625
Corroder 4886280.2177734
with a very noticeable delay between the prints...
note the values are in kilobytes so that makes the final results for a 100000 character random string 2.33 MB vs 4.66 GB.
Code: | local function measure(func)
collectgarbage("collect")
collectgarbage("stop")
thestart = collectgarbage("count")
pcall(func)
theend = collectgarbage("count")
collectgarbage("restart")
return theend - thestart
end
local function RandomString( len )
str = ""
for i = 0, len do
str = str .. string.char( math.random(97,122) )
end
return str
end
local function randomAlpha() return string.char(math.random(('a'):byte(), ('z'):byte())) end
local function randomText(len)
if not len or type(len) ~= 'number' or len < 1 then error('Invalid len to randomText', 2) end
local res = {}
for i = 0, len do res[#res+1] = randomAlpha() end
return table.concat(res)
end
local len = 10000
print('FreeER ', measure(function() randomText(len) end))
print('Corroder', measure(function() RandomString(len) end))
|
_________________
Last edited by FreeER on Mon Jun 04, 2018 6:21 pm; edited 4 times in total |
|
Back to top |
|
|
Corroder Grandmaster Cheater Supreme Reputation: 75
Joined: 10 Apr 2015 Posts: 1667
|
|
Back to top |
|
|
FreeER Grandmaster Cheater Supreme Reputation: 53
Joined: 09 Aug 2013 Posts: 1091
|
Posted: Mon Jun 04, 2018 6:24 pm Post subject: |
|
|
That one is a recursive concatenation (and no tail-call because concatenation) so I can only imagine it'd be worse both in memory and cpu usage even with the lookup table
Oh, not to mention re-seeding n times
_________________
|
|
Back to top |
|
|
|