i have a better suggestion than my last one. the last one was kind of basic
basically to make this piece of code run faster, the problem is that what this code is doing is
note: randomNumbersTable being folder:GetChildren() in this case
1: take a random number from the randomNumbersTable
2: compare the random number to every number in the randomNumbersTable
3: index into tableOfSizes once complete
4: repeat
do you not see the problem?
the question is that, do you REALLY have to compare one number to every number to achieve this?
is it really that necessary?
and the answer is that there is a better way to go about it compared to how you went about it then to make this code run faster, you’ll have to change the approach and get rid of the thought of comparing one number to every other number.
note: orderedTable is tableOfSizes
i suggest you try this method i came up with:
take a random number from the randomNumbersTable.
blindly index into orderedTable.
ask the computer “is the random number indexed smaller than the random number indexed before it”.
if the random number indexed IS smaller than the random number indexed before it then keep swapping the index of the most recent random number indexed with the index before it until the index before it has a number smaller than it.
repeat.
--also i scripted it for you
local callsMade = 0
local random = Random.new()
local randomNumbers = {}
for i = 1, 250 do
randomNumbers[i] = random:NextInteger(1, 41019238712983712)
end
local numbersMinToMax = {}
numbersMinToMax[0] = -math.huge
local function sort(i, randomNumber)
callsMade += 1
numbersMinToMax[i] = randomNumber
if randomNumber < numbersMinToMax[i - 1] then
numbersMinToMax[i] = numbersMinToMax[i - 1]
sort(i - 1, randomNumber)
end
end
local theTimeWhenThisVarWasMade = os.clock()
for i = 1, #randomNumbers do
callsMade += 1
sort(i, randomNumbers[i])
end
local speedPerformance = os.clock() - theTimeWhenThisVarWasMade
print(numbersMinToMax)
print(callsMade)
print(speedPerformance)
note: this method can be made theoretically faster if it has a guessing mechanical that makes a better guess out of a bad one and keep repeating it until it reaches the correct index but you should just use table.sort. The point of this was to prove that there is a better way to go about “compare the random number to all numbers in the table”
summary of suggestion: just dont repeat “compare the random number to all numbers in the table” because this is the MAIN reason why your code is slow