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

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

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

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