More efficient for loop

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I wish the for loop below me can be more efficient.

  2. What is the issue? Include screenshots / videos if possible!
    The loop below me ran too slow for my game.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I searched keywords such as “more efficient loops”, “alternative for loops” and such. However, I can’t find any solutions.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

local Loop1 = function()
	for i = 0, 1000
	do
		for i = 0, 1000
		do
			--[[
				Code
			--]]
		end
		
		wait();
	end
end

Loop1();

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

You have a wait() statement in it. A wait thing will wait 1/30 seconds. You run it 1000 times, leading to a wait of over 30 seconds! Remove the wait statement and try again!

old
local Loop1 = function()
	for i = 0, 1000
	do
		for i = 0, 1000
		do
          coroutine.resume(coroutine.create(function()
            --[[
				Code
			--]]
          end))
			
		end
		
		wait();
	end
end

Loop1();

coroutine basically makes it run on a seperate thread, speeding up the script

If you speed up this loop it will kill your game.

Editing my comment to something better, a flaw in OP’s code ;D

1 Like

Ty, idk why it somehow works this time without overloading.

If it does end up overloading, try this:

local Loop1 = function()
	for i = 0, 1000
	do
		for i = 0, 200 do
			--[[
				Code
			--]]
		end
for i = 0, 200 do
			--[[
				Code
			--]]
		end
for i = 0, 200 do
			--[[
				Code
			--]]
		end
for i = 0, 200 do
			--[[
				Code
			--]]
		end
for i = 0, 200 do
			--[[
				Code
			--]]
		end
		
		task.wait();
	end
end

Loop1();
1 Like

Could you provide more information on what exactly you’re trying to accomplish? Without much context, it’s hard to deduce the exact approach to your problem. However, I can still provide the following suggestions:

  1. You have a loop inside of another loop (nested loop), but they both use the same variable i. This means the code would do the same thing exactly 1,000,000 times. I can’t really think of a reason why you would want to run something 1000 times per frame in 1000 frames, which brings me to point 2;
  2. Rethink your approach. Are you trying to modify instance properties? Perhaps you could use Tweens instead?
  3. Does your Code portion also use wait? As @SeargentAUS suggested, you can run the code in separate threads;
  4. The solution that @SeargentAUS provided is a band-aid solution (and a really bad one, especially the last one) that should be improved upon using my suggestions and especially point 5;
  5. Use the task library instead of wait and coroutine:
    Instead of wait(), use task.wait();
    Instead of coroutine.resume(coroutine.create(function(), use task.spawn(function() to immediately run the function, or task.defer(function() to run the function on the next frame.

Optional (and somewhat biased) code quality suggestions that do not affect performance:

  • Instead of writing local Loop1 = function() you can write local function Loop1();
  • Semicolons are optional;
  • You can write for i = 0, 1000 do on the same line to make it better to read.
2 Likes

Just A Small Fix, You Should Do This If You Want It To Be Faster And Not Crash At The Same Time

local Loop1 = function()
	for i = 0, 1000
	do
          for i = 1,2 do
		for i = 0, 100 do
			--[[
				Code
			--]]
		end
for i = 0, 100 do
			--[[
				Code
			--]]
		end
for i = 0, 100 do
			--[[
				Code
			--]]
		end
for i = 0, 100 do
			--[[
				Code
			--]]
		end
for i = 0, 100 do
			--[[
				Code
			--]]
		end
                local t = tick()-tick()+tick()-tick()+tick()-tick() --To Introduce Some Delay Between Frame Iterations (Like A Wait But Faster)
		end
		game:GetService("RunService").RenderStepped:Wait()
	end
end

Loop1();
2 Likes

Thank, You Very Much. For This. :grin:

2 Likes