Term for executing code every N frames to optimize?

Hello all,

I’m unclear on how to seek out more information on this subject. I’ve done this before in both XNA and Unity, but wanted to learn more about best practices and pros and cons of this type of optimization.

What I’ve done in the past is tag objects with an enum, then increment a singleton counter each frame. Each frame, objects check whether this global counter matches their enumerated tag, and only execute their complex code on said frames.

I call this “round-robin” when I make notes for myself, but I don’t know the proper term for this technique to facilitate investigation. Can anyone tell me a proper term for this process so I can learn more about it?

Other comments are more than welcome, and thanks for your time.

That’s a neat way of doing it. Round robin is probably a good word for it but try looking it up in a book/site about design patterns.

You can get the frame count from Time.frameCount and then modulo that with the total number of enum values you have.

int maxEnum = Enum.GetValues(typeof(Foo)).Cast<Foo>().Max();
int taskNumber = Time.frameCount % maxEnum;
complexTask[taskNumber].Invoke();

But I would recommend that you look at Coroutines, they can be started/stopped and only run every x frame/second and are really wonderful to work with :slight_smile: when working with coroutines you have these options to yield return:

WaitForEndOfFrame

Waits until the end of the frame after all cameras and GUI is rendered, just before displaying the frame on screen.

WaitForFixedUpdate

Waits until next fixed frame rate update function.

WaitForSeconds

Suspends the coroutine execution for the given amount of seconds.

As far as I know it is possible to create your own yielding return, that would make use of your own counter. Hope this helps in your research :slight_smile:

This design pattern sounds a little like Task Scheduling to me. See this article:

http://en.wikipedia.org/wiki/Task_scheduling

Notice in particular that the word you have chosen for your pattern actually exists, and actually DOES describe pretty much exactly what you're doing:

http://en.wikipedia.org/wiki/Round-robin_scheduling

While these principles do apply theoretically to your scheduling scheme, they're usually used in fields of computer science far more low-level than the stuff you and I deal with, such as spreading computation over several frames like you're accomplishing here. As I understand it, it's concerned mainly with how a CPU manages the assignment of its own computational resources to multiple tasks that are sent to it by the OS. Before multi-core CPUs allowed for true multi-threading where one core handles one thing and a second core another, this was actually how a CPU managed mono-core multithreading; it uses task scheduling to sequentialize atomic instructions of concurrently executing programs, so each get a fair share, and they seem to occur simultaneously to the naked eye. Such a sequence of atomic instructions is known as an "interleaving", and represent one case out of potentially hundreds or thousands of different ways a concurrent process can be scheduled to occur. The circular round-robin scheme you've chosen for sequentializing your tasks can also be thought of as an interleaving, but since you're associating each task with a statically enumerated integer, you get the same interleaving every time the sequence executes.

Round-Robin is a well-chosen name for what you do, stick with it. And do look into Coroutines, as @PaulUsul suggested. They're a design tool provided by the Unity framework that helps accomplish almost exactly what you want, in a clean, framework-supported manner.