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

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

“optimized” (as an example) is loading the same skin texture for separate assets just once (instead of loading it each time for each asset.)

and then making sure that you really need to load 18,000 different textures instead of, maybe, 500 or so.

Anonymous 0 Comments

“Optimisation” is a blanket term that encompasses a wide array of techniques and practises whose aim is to make code as efficient as possible, or in other words, requiring the least amount of resources from a system (processing power, memory etc) while producing the same result. The main reason this is desirable is because it enables developers to either make the most out of a given system (i.e. a gaming console with fixed specs) or because it enables a PC game to reach a wider audience if it has low system requirements.

There’s many methods this is achieved through. The basic form of optimising code is much like simplification in math. Lines of code can be simplified and made shorter which in turn reduces the amount of data. Code can also be made more efficient in the way it utilises the system resources. Data can be compressed, clever ways of loading and unloading can be utilised, unecessary processes can be deleted etc.

Anonymous 0 Comments

algorithmic problems (programming) can be solved in a number of ways, to achieve the same result. some run faster, some run slower. this is direct consequence of how the silicone is built and organized. say, you perform an operation using one complicated instruction, that takes many CPU cycles to complete. BUT – you can achieve the same result by breaking it down into more steps, that in total will run *faster* that the single, complicated instruction. this is more/less what code optimization is.

p.s. this is also the reason why CPU realm is divided into two contradictory approaches. ever heard of [RISC](https://en.wikipedia.org/wiki/Reduced_instruction_set_computer) vs. [CISC](https://en.wikipedia.org/wiki/Complex_instruction_set_computer)? this is exactly the same problem, but in context of silicon design and code execution approach. either one complex instruction to do something, or set of simple instructions to do exactly the same thing, but differently.

Anonymous 0 Comments

“Optimisation” is a blanket term that encompasses a wide array of techniques and practises whose aim is to make code as efficient as possible, or in other words, requiring the least amount of resources from a system (processing power, memory etc) while producing the same result. The main reason this is desirable is because it enables developers to either make the most out of a given system (i.e. a gaming console with fixed specs) or because it enables a PC game to reach a wider audience if it has low system requirements.

There’s many methods this is achieved through. The basic form of optimising code is much like simplification in math. Lines of code can be simplified and made shorter which in turn reduces the amount of data. Code can also be made more efficient in the way it utilises the system resources. Data can be compressed, clever ways of loading and unloading can be utilised, unecessary processes can be deleted etc.

Anonymous 0 Comments

algorithmic problems (programming) can be solved in a number of ways, to achieve the same result. some run faster, some run slower. this is direct consequence of how the silicone is built and organized. say, you perform an operation using one complicated instruction, that takes many CPU cycles to complete. BUT – you can achieve the same result by breaking it down into more steps, that in total will run *faster* that the single, complicated instruction. this is more/less what code optimization is.

p.s. this is also the reason why CPU realm is divided into two contradictory approaches. ever heard of [RISC](https://en.wikipedia.org/wiki/Reduced_instruction_set_computer) vs. [CISC](https://en.wikipedia.org/wiki/Complex_instruction_set_computer)? this is exactly the same problem, but in context of silicon design and code execution approach. either one complex instruction to do something, or set of simple instructions to do exactly the same thing, but differently.

Anonymous 0 Comments

For a simple example, lets say you need to add the numbers from 1 to 1,000,000. The obvious way to do this is to go through the list and add each one to the total. This is quite slow. Your processor has some instructions that can work on multiple pieces of data at once, so you can do 1+2, 3+4, 5+6, 7+8 simultaneously, then go back and add those results together.

Your processor also has multiple cores, if you have eight cores you can have core zero working on the numbers up to 125,000, core 1 working on 125,001 to 250,000, etc. So we’re now ~32x faster than the original code, ignoring overhead.

But we can do better. We don’t REALLY care about the process of adding the numbers, what we actually want is the answer at the end. And with a little math we can turn that into something that’s much faster to compute. If you notice that the first number plus the last number is equal to the second number plus the second to last number, and that that pattern holds as you work to the middle, you can turn all those additions into one multiplication. 1,000,001 * 500,000. The code is now hundreds of thousands of times faster, even if you’re only using one core.

If there isn’t a clever trick to make the calculation faster, what you can sometimes do instead is do the calculation once, then keep the result, so you only have to look the answer up in a table.

Anonymous 0 Comments

there are MANY ways to optimize things, but I’ll demonstrate a really simple example to make it clear what it means:

let’s say you are a PC technician. your company has a stack of 1000 laptops. they gave you a stack of 1000 pieces of paper that each have a different serial number on them. those are warranty cards. all of the laptops have a recall, and in order to send them in for free repair, you have to send each one in with their own warranty card with matching serial number in the box.

unoptimized code:

you power a laptop one. you manually look through all 1,000 pieces of paper to find the matching serial number. even after you find the serial number that matches it, you keep looking through the stack of warranty papers until you reach the end. even if the 1st page you check was the matching one. you still check the other 999 pages. you then put the warranty paper on the keyboard and close the lid to the laptop. put it in the completed pile, and grab the next laptop. repeat this for all 1000 laptops. since you have 1000 laptops and 1000 pieces of paper to check, it will be almost 1 MILLION times that you read the serial number on those cards. slow and inefficient

1st optimization:

you find out that only 100 of those laptops are new enough to get the fix. the others won’t be eligible. so you look on the bottom of each laptop and find the 100 that have the model number that IS eligible for the warranty fix. now we just cut our time down to 10% of what it was earlier, since we’re only checking 10% of the laptops.

2nd optimization:

humans aren’t this dumb, but computers programs by default will check ALL items in a list in a loop, unless you tell it to stop after it finds a match. so when you look through the warranty papers, as soon as you find a match, you put the paper on the keyboard, close the laptop, and put it in the done pile. if the 3rd paper you check is the right one, there’s no point in checking the other 997 pages, right? people often overlook this. inside a loop, programming languages have a command called “break” or “continue”, and you can use those to tell the program hey, I found what I need, move on to the next thing

3rd optimization:

multi-threading. you get a friend to help you. they grab a laptop, and power it on. your job is to find the paper with the matching serial number. it will definitely speed the process up, but you’re still limited by how fast you can turn the laptops on, or by how fast your friend can find the matching serial number. even if one person got the laptop, another opened it, another powered it on, another person read off the serial number from the screen, and another searched for the matching paper, and another person put the completed laptop in the completed pile, you still won’t be any faster than the slowest person. in this case, the person checking the warranty papers will probably be the slowest part. or maybe the person powering the laptop on, if the laptop takes a long time to boot up

4th optimization:

multi-processing. the above process is serial. like an assembly line. you can only go as fast as the slowest step in this assembly line. with multi-processing, however, you have parallelism. meaning you can have more than one of those lines. let’s say we have 8 people total now. 4 grabbing laptops,, turning them on, and reading the serial number to you like earlier. another 4 are checking for the matching serial number/warranty paper. even if the serial number part is the slowest, you now have 4 lines of work, so 4 people are checking at the same time. you’ll get 4x the speed in most cases by using multi-processing. when you hear about computers having “cores”, each core is like one of these 4 lines we have in this example.

that’s just the tip of the iceberg. there are tons of places to find optimizations in code, but that’s a simplified one that I think might make sense. holler if you have questions

Anonymous 0 Comments

For a simple example, lets say you need to add the numbers from 1 to 1,000,000. The obvious way to do this is to go through the list and add each one to the total. This is quite slow. Your processor has some instructions that can work on multiple pieces of data at once, so you can do 1+2, 3+4, 5+6, 7+8 simultaneously, then go back and add those results together.

Your processor also has multiple cores, if you have eight cores you can have core zero working on the numbers up to 125,000, core 1 working on 125,001 to 250,000, etc. So we’re now ~32x faster than the original code, ignoring overhead.

But we can do better. We don’t REALLY care about the process of adding the numbers, what we actually want is the answer at the end. And with a little math we can turn that into something that’s much faster to compute. If you notice that the first number plus the last number is equal to the second number plus the second to last number, and that that pattern holds as you work to the middle, you can turn all those additions into one multiplication. 1,000,001 * 500,000. The code is now hundreds of thousands of times faster, even if you’re only using one core.

If there isn’t a clever trick to make the calculation faster, what you can sometimes do instead is do the calculation once, then keep the result, so you only have to look the answer up in a table.

Anonymous 0 Comments

For a simple example, lets say you need to add the numbers from 1 to 1,000,000. The obvious way to do this is to go through the list and add each one to the total. This is quite slow. Your processor has some instructions that can work on multiple pieces of data at once, so you can do 1+2, 3+4, 5+6, 7+8 simultaneously, then go back and add those results together.

Your processor also has multiple cores, if you have eight cores you can have core zero working on the numbers up to 125,000, core 1 working on 125,001 to 250,000, etc. So we’re now ~32x faster than the original code, ignoring overhead.

But we can do better. We don’t REALLY care about the process of adding the numbers, what we actually want is the answer at the end. And with a little math we can turn that into something that’s much faster to compute. If you notice that the first number plus the last number is equal to the second number plus the second to last number, and that that pattern holds as you work to the middle, you can turn all those additions into one multiplication. 1,000,001 * 500,000. The code is now hundreds of thousands of times faster, even if you’re only using one core.

If there isn’t a clever trick to make the calculation faster, what you can sometimes do instead is do the calculation once, then keep the result, so you only have to look the answer up in a table.

Anonymous 0 Comments

there are MANY ways to optimize things, but I’ll demonstrate a really simple example to make it clear what it means:

let’s say you are a PC technician. your company has a stack of 1000 laptops. they gave you a stack of 1000 pieces of paper that each have a different serial number on them. those are warranty cards. all of the laptops have a recall, and in order to send them in for free repair, you have to send each one in with their own warranty card with matching serial number in the box.

unoptimized code:

you power a laptop one. you manually look through all 1,000 pieces of paper to find the matching serial number. even after you find the serial number that matches it, you keep looking through the stack of warranty papers until you reach the end. even if the 1st page you check was the matching one. you still check the other 999 pages. you then put the warranty paper on the keyboard and close the lid to the laptop. put it in the completed pile, and grab the next laptop. repeat this for all 1000 laptops. since you have 1000 laptops and 1000 pieces of paper to check, it will be almost 1 MILLION times that you read the serial number on those cards. slow and inefficient

1st optimization:

you find out that only 100 of those laptops are new enough to get the fix. the others won’t be eligible. so you look on the bottom of each laptop and find the 100 that have the model number that IS eligible for the warranty fix. now we just cut our time down to 10% of what it was earlier, since we’re only checking 10% of the laptops.

2nd optimization:

humans aren’t this dumb, but computers programs by default will check ALL items in a list in a loop, unless you tell it to stop after it finds a match. so when you look through the warranty papers, as soon as you find a match, you put the paper on the keyboard, close the laptop, and put it in the done pile. if the 3rd paper you check is the right one, there’s no point in checking the other 997 pages, right? people often overlook this. inside a loop, programming languages have a command called “break” or “continue”, and you can use those to tell the program hey, I found what I need, move on to the next thing

3rd optimization:

multi-threading. you get a friend to help you. they grab a laptop, and power it on. your job is to find the paper with the matching serial number. it will definitely speed the process up, but you’re still limited by how fast you can turn the laptops on, or by how fast your friend can find the matching serial number. even if one person got the laptop, another opened it, another powered it on, another person read off the serial number from the screen, and another searched for the matching paper, and another person put the completed laptop in the completed pile, you still won’t be any faster than the slowest person. in this case, the person checking the warranty papers will probably be the slowest part. or maybe the person powering the laptop on, if the laptop takes a long time to boot up

4th optimization:

multi-processing. the above process is serial. like an assembly line. you can only go as fast as the slowest step in this assembly line. with multi-processing, however, you have parallelism. meaning you can have more than one of those lines. let’s say we have 8 people total now. 4 grabbing laptops,, turning them on, and reading the serial number to you like earlier. another 4 are checking for the matching serial number/warranty paper. even if the serial number part is the slowest, you now have 4 lines of work, so 4 people are checking at the same time. you’ll get 4x the speed in most cases by using multi-processing. when you hear about computers having “cores”, each core is like one of these 4 lines we have in this example.

that’s just the tip of the iceberg. there are tons of places to find optimizations in code, but that’s a simplified one that I think might make sense. holler if you have questions