7 Billion Humans

7 Billion Humans

Not enough ratings
Optimization tips
By Zyro
Some tips for optimizing your solutions
   
Award
Favorite
Favorited
Unfavorite
General remarks
Except for the more or less trivial cases, your solutions for optimized speed and for optimized instruction count will look different. Sometimes very different. My approach is to first solve the puzzle, then copy the solution over to one of the other program tabs and optimize it for one of the goals. Always keep in mind that when you're optimizing for one of them, the other one doesn't matter at all. Don't try to subconsciously optimize both speed and count.

Failure may be success
In order to count as success, your solution does not need to work for every input. This suprised me and I didn't make use for it, but if you succeed for over 50% of the cases, you're on the winning side.
Optimizing for instruction count
First, don't overlook that the instruction count can be seen left to the actual instructions. No surprises here (unlike with the program speed).

If only...
Every if statement counts as a single instrucion, no matter how long and complicated it is. So do query as many things as useful in your if. Maybe you can spare something else by these, e.g. a second if?

Making sacrifices
There's seven billions of them, and they are not needed anymore anyway...! If the level instruction doesn't say anything else explictly, feel free to let every human except a last one die. Spare the instruction needed to spare a human.

Embrace errors
This is not about recommending to try out what you can think of, even if it will probably fail - which I totally do. I'm thinking about the little errors. Dropping something although you haven't got anything to drop? Results in an ugly speech bubble and (probably) takes lots of time, right? Right, but we're not here to save time. Or bubbles. If it saves you a statement, like an if, just do it.

Do not initialize
Contrary what you are told in real world programming - for a reason -, you might not want to initialize memory sometimes. You don't need to set it to five and count down to zero. It comes preinitialized to zero. So just add on it and compare it to five instead.

Concise statements
Something I might have been the only one to overlook: If you've got a position in memory, you don't need to walk there and afterwards pickup/giveto or whatever you want there. You can pass the memory directly to the pickup/giveto instruction. Gave me some headaches, so I'm writing this to make sure you will not get these.

Do it yourself
Sometimes - but not always - the easiest way to do something is to do it yourself and not cooperate. So some optimizations might involve little to no help of most of the workers.
Optimizing for speed
The time your program needs to run is harder to predict than the number of instructions. If it's not fast enough, just watching it run can help: What is happening too slow? Where are the humans waiting for something?

Math is hard
Contradictory to what I expected, in 7 Billion Humans these humans seem to move fast compared to the calculations. But then... it's humans doing the calculations, so I guess this is to be expected? You probably want to avoid too many calculations.

Inner loops
Now this is just like real world program optimizing: If you want a program to be fast, assure that the inner loops running most often are fast. Mind that you're not optimizing for count of instructions here, so an extra if instruction creating a very close loop (check - single instruction - jump - check again) might help your humans getting their work done faster.

Multi-peopling
Tired of all those game developers that cannot make use of your octa-core CPU? Show them! Problems are solved fastest when everybody, or almost everybody takes part. (Unfortunately, real world programming problems most times just aren't like this...)

Do repeat yourself
If nothing else helps, "loop unrolling" might help. So if you know the number of times something will be done, you can write it down just as many times instead of having some if-jump construction. Tomorrow Corporation assured for Human Resource Machine that loop unrolling was never needed (except one obvious case as far as I remember) to meet the optimization requirements. I guess the same is true for 7 Billion Humans. Still, it might be a last resort. On the other hand... it is tedious! You might want to do it in an external editor with better copy/paste capabilities if you want to use this technique.

What am I doing here?!?
The most important speedup possibility in real programs is not about the small things, it's about changing the algorithm. Is the one you're using the fastest one? Is it at least close to the optimization goal?
14 Comments
Zyro  [author] 31 Oct, 2023 @ 3:03am 
@BitBlit Didn't know that one! :D
BitBlit 31 Oct, 2023 @ 2:28am 
One incredibly useful speed optimization is to use dividing by zero (when available) to kill humans when necessary. I've saved time and instructions by abusing this easter egg to kill stuff. You don't always need to fall down a hole.
Futae Yamagawa 16 Nov, 2018 @ 6:20pm 
NOPs are fun. I once sped up a program by ~6% by inserting what amounted to a NOP >>between<< methods. (Lesson: Byte alignment matters).
Zyro  [author] 12 Nov, 2018 @ 11:27pm 
Oh, I didn't question speed optimization. I am software developer and I literally fought for nanoseconds in an inner loop once. And colleagues recently discussed if the proper placement of NOPs could speed up a program. :-D
Futae Yamagawa 12 Nov, 2018 @ 9:49pm 
You can store what is in the northwest space, and query that later. Even if what was in that space is moved, or changed. But memory will still only hold what was in that space at the time you queried it. It's a minor distinction, but one that can bite you if you arn't careful, or exploit to save time.

Real world programming? You are at some point going to run into performance bottlenecks. I deal daily with servers, where a pause in processing as short as 2 seconds can mean the server drops requests, and a pause over 5 seconds can crash the server outright. For me, a server taking more than 10 milliseconds to serve a request is too slow, because users will be able to quickly generate a backlog of requests greater than a mere one or two seconds.
Zyro  [author] 31 Oct, 2018 @ 2:33am 
@ramonpontes Will add that, thanks!
Zyro  [author] 31 Oct, 2018 @ 2:32am 
@Futae Yamagawa I didn't ask earlier because I still was early in the game when publishing the first version of the guide. Now I'm through with all speed and all but two count optimizations - and didn't think of (and didn't need) the math replacement trick. It's counterintuitive from real world programming. :) Will add it in the guide, thanks!
I didn't really understand the direction storage trick. Can you store say upper left and query your upper left from different positions? Probably not, so... You store a position and query it instead of "looking" there? I wonder why this makes a speed difference. X)
Futae Yamagawa 26 Oct, 2018 @ 5:09pm 
Looking at the world can slow you down, if youneed to check a direction for something, Store that direction to memory and perform your tests against what you have in memory.
--
Futae Yamagawa 26 Oct, 2018 @ 5:06pm 
if mem1 = 2 then set mem1 = 3

But if you need 2+1, you may need 1+1, 3+1, 4+1, 5+1, etc ...
if mem1 < 4 then
if mem1 < 2 then
if mem1 = 0 then mem1 = 1 else mem1 = 2
else
if mem1 = 2 then mem1 = 3 else mem1 = 4
endif
else
if mem1 < 6 then
if mem1 = 4 then mem1 = 5 else mem1 = 6
else
if mem1 = 6 then mem1 = 7 else mem1 = 8
endif
endif

Math works slow, but if trees like that proc quickly.
ramonpontes 26 Oct, 2018 @ 10:39am 
Very good tips. The "Do it yourself" is very true, sometimes I focus only in one of the workers to complete a task with less commands, instead of making them woring together.

You may also add a hint about the ELSE command. When I was trying to optimize the size of my programs, I could easily skip the Else command on many opportunities, or change the order of the IF structure to completely avoid using the ELSE.