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

1.94K 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

You’re doing two loads of laundry with a washer and a dryer.

First approach is you put the first load in the washer and wait for it to finish washing, put it in the dryer and wait for it to finish drying. Then you repeat the process for your second load. The total time is two wash cycles and two drying cycles.

Optimized approach is to start second load’s wash cycle while the first load’s dry cycle is going. Now the total time is one wash cycle, one dry cycle, and some time in between a wash and dry cycle time (when both machines are in use). Much faster!

Optimizing a game is like that but finding opportunities like that in code. Technically this is only a specific type of optimization, but the general idea is to find ways to make things run more efficiently.

Anonymous 0 Comments

You’re doing two loads of laundry with a washer and a dryer.

First approach is you put the first load in the washer and wait for it to finish washing, put it in the dryer and wait for it to finish drying. Then you repeat the process for your second load. The total time is two wash cycles and two drying cycles.

Optimized approach is to start second load’s wash cycle while the first load’s dry cycle is going. Now the total time is one wash cycle, one dry cycle, and some time in between a wash and dry cycle time (when both machines are in use). Much faster!

Optimizing a game is like that but finding opportunities like that in code. Technically this is only a specific type of optimization, but the general idea is to find ways to make things run more efficiently.

Anonymous 0 Comments

One implementation of certain optimizations, including a feature in Optifine, it calls “fast math”. In this case they take expensive math operations, like calculating sine, cosine, or square roots and replace them with faster less accurate functions. These functions are used a lot in graphics processing, so there can be some significant gains by reducing the amount of time those operations take.

Anonymous 0 Comments

You’re doing two loads of laundry with a washer and a dryer.

First approach is you put the first load in the washer and wait for it to finish washing, put it in the dryer and wait for it to finish drying. Then you repeat the process for your second load. The total time is two wash cycles and two drying cycles.

Optimized approach is to start second load’s wash cycle while the first load’s dry cycle is going. Now the total time is one wash cycle, one dry cycle, and some time in between a wash and dry cycle time (when both machines are in use). Much faster!

Optimizing a game is like that but finding opportunities like that in code. Technically this is only a specific type of optimization, but the general idea is to find ways to make things run more efficiently.

Anonymous 0 Comments

I think it is also important to mention that there are tools that can analyze code & running programs to tell you where it is spending most of its time. That’s helps identify places where can optimize the code. As an example, I had program that dealt with database records that had a date. And every time to display a record we needed a formatter to make the date look as desired. Using a profiling tool it showed doing that was actually the bulk of processing time used by the app. For each record it created a formatter object which turned out to be a very expensive operation. I fixed it by creating one at the beginning of the app and reusing it for each record. Made the program 90% faster. Optimized!

Anonymous 0 Comments

One implementation of certain optimizations, including a feature in Optifine, it calls “fast math”. In this case they take expensive math operations, like calculating sine, cosine, or square roots and replace them with faster less accurate functions. These functions are used a lot in graphics processing, so there can be some significant gains by reducing the amount of time those operations take.

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.

Anonymous 0 Comments

I think it is also important to mention that there are tools that can analyze code & running programs to tell you where it is spending most of its time. That’s helps identify places where can optimize the code. As an example, I had program that dealt with database records that had a date. And every time to display a record we needed a formatter to make the date look as desired. Using a profiling tool it showed doing that was actually the bulk of processing time used by the app. For each record it created a formatter object which turned out to be a very expensive operation. I fixed it by creating one at the beginning of the app and reusing it for each record. Made the program 90% faster. Optimized!

Anonymous 0 Comments

A lot of overcomplicated answers here…

Example:

I give you a box and ask you to deliver it to the house across the street. You can either A) just walk across the street with the box, or B) you can spend a bunch of time to find car keys, load the box into the car, drive the car across the street, and finally unload the box.

Option A is faster and easier to accomplish, because it’s just one box. Now imagine the same scenario but with ten boxes. Since you can load all ten boxes into the car at one time, option B becomes faster than walking across the street 10 times, even though option B is more complicated.

>Are people sitting there changing lines of code to more “optimal” ones?

In short: Yes

Programmers may start with poorly optimized code which is easy to write, and then later go back and make highly optimized code which is harder to write.

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.