How does game “optimization” work? Are people sitting there changing lines of code to more “optimal” ones? What is “optimized”?

2.08K views

The recent The Last of Us for PC made me realize I had no idea what’s meant by “optimizing” a game.

Same with optifine in Minecraft improving performance. How do these things work to just make games use fewer resources?

In: 158

105 Answers

Anonymous 0 Comments

Say you have the computer place 10 circles on the screen.

Then you want to check if any overlap.

One way is to check the location of a circle and compare it to the nine others. That’s nine checks, then you have to do that whole thing a total of ten times, once for each circle. That’s now 90 checks.

The code for that would be mostly simple to write, but takes a longer time to execute.

It would be faster if we write some more code that cuts out any checks that have previously been done already. We can check if circle A overlaps with any of the other nine. But when we check circle B, we don’t check if that one overlaps with A because it’s already been checked before.

The first circle checks nine others, the second checks the remaining 8, the third checks the remaining 7…

It’s slightly more complex code, but reduces the number of checks down to 45 instead of 90. Slightly more optimized.

Now, we can write even more code to cut out checks for circles that are clearly too far away, and prevent checking things twice.

It takes some setup, but we can list the circles in order based on their distance from an orgin point.

We start with the closest circle and start checking the list in order. Eventually we find a circle far enough from the orgin that we know the rest are too far away to overlap. So circle A might only have to check 3 circles for an overlap then stop because the rest of the circles in the list are futher and too far away.

Circle B might only have to check the next 3 or 4 in the list. Same with circle C and so forth…

Something like that might result in only 25 checks, which is slightly faster than doing 45 checks.

This isn’t exactly what programmers are usually doing when they optimize code, but I think it’s okay enough for a ELI5 answer.

In reality, they’re probably doing more with figuring out how to maximize performance of hardware and write code to take advantage of individual operations.

You are viewing 1 out of 105 answers, click here to view all answers.