Try this code. Making 20 of loops with your function takes the game to 5 FPS. Running 20 loops with task.wait doesn’t even change the milliseconds per frame by 0.01.
local function stupidWait(TIME: number?)
local t = tick or os.clock
local i = t()
TIME = TIME or 0.01
local w = task.wait
local r = math.random or Random.new
while t() - i < TIME do
if r(1,100000) == 1 then w() end
end
return true
end
local runningLoops = 0
game:GetService("UserInputService").InputBegan:Connect(function(inputObject)
if inputObject.KeyCode == Enum.KeyCode.E and inputObject.UserInputState == Enum.UserInputState.Begin then
runningLoops += 1
print("Stupid loops running: "..runningLoops)
while true do
stupidWait(1)
end
end
end)
local runningLoops = 0
game:GetService("UserInputService").InputBegan:Connect(function(inputObject)
if inputObject.KeyCode == Enum.KeyCode.Q and inputObject.UserInputState == Enum.UserInputState.Begin then
runningLoops += 1
print("Less stupid loops running: "..runningLoops)
while true do
task.wait(1)
end
end
end)
Your code is over ten thousand times less efficient (not even an exaggeration). No one should ever use this code, and it shouldn’t be on the forums to trick and confuse people.
I’ll never grasp why people try to use the same engine features, to make those engine features faster, or finding hacky methods at that, to try to mimick something being “faster”.
On that topic, I feel like even if this thing did what people would expect it too, it would have no purpose at the end of the day. I don’t see any real use case for this in a loop, especially any normal loop. Using task.wait() as is is already pretty fast and superseded the outdated wait() (much needed tbh)
I would just stick with the regular task.wait() method. It’s less code, easier to remember, and saves you time. Also with engine changes, you never know if this will forever be reliable.
Remaking engine features that are already good enough is probably not going to end well, as we can see here. I see that you did it for fun and all, but if that were the case, you probably wouldn’t want to post it here.
Hello! I am just going to say now that this is not as fast as you claim it is. (Keep in mind the speed is also affected by your processor, and my processor is the i9-13900K, one of the fastest Intel CPUs on the market.)
After conducting some benchmarking I have found that the regular speed of this wait function on my processor is around ~.5249… between ~.6877… (Aproximations, not exact,) on a 0.1 wait. Even standard wait which has been proven to be the slowest built-in wait function currently, is ultimately faster than your custom function.
Now I decided to make an attempt to fix/rewrite your script to be simpler, and at first I tried removing the math.random function in turn for just the w() function. This occasionally made it faster than task.wait, but this was in like every 1 and 10 benchmarks.
After that my friend suggested that I where to clean up the script and make it more simple and refined.
Refined Script:
local function twait(TIME: number?)
local i = tick()
local w = task.wait
while tick() - i < (TIME or 0.01) do
w()
end
return tick() - i
end