The wonders of optimisation

Hi! I’m very much still learning programming, and a funny thing happened to me right now. Since I have nobody else to share it with, I figured I’d post here, though it’s probably the kind of story that happened to every one of you.

I’m working on a potentially rather complex game right now. The kind of complexity you probably shouldn’t have in your first project if you have zero programming experience. Well screw that! I’ve spent tons of time in the 90s fiddling with QBasic and later spent days making custom maps for Warcraft 3 (though I always stubbornly avoided writing custom code and did everything through the horribly inefficient GUI). That should be enough, right? To my surprise, I took to LUA really fast and it’s been a blast figuring everything out so far. It also helps to have realistic goals:

I’ve already surpassed my first goal - “Do not lose interest after two weeks”, seeing as I’ve been working on this since February. Right now I’m on “Stick with it long enough to see the code turn into a horrid inefficient mess I don’t understand at all”. In case you are wondering, “Add actual gameplay” is step 7, though I can see myself pushing it farther down the line. The real end goal is not “have a playable and fun game” but “learn programming and have fun doing that” and I’m doing pretty well in that regard.

The idea is to set up something like a generic construction and management simulator - think Tropico or Startopia or any number of similar games. However the player won’t be the one doing the managing. I want to create a world where the peeps would live their lives, go to work, steal, go to jail and murder each other and once I have that set the player loose an have them fight crime, commit crime, try to live or do whatever they want and once they inevitably get killed (I’m aiming for a roguelike style), the world gets reset and the simulation gets run again until the time of the player’s death (Edit: without the player’s actions, of course) and then it will tell you how much of an impact you had (edit: compared to a world where you never existed).

I’m honestly interested in seeing which approaches lead to what results - what would reduce crime the most? Playing as Batman with a no-killing rule, or just mowing every criminal down as the Punisher? Hell, maybe it will turn out that whatever you do, the overal results tend to be random and you can’t chose to change anything. Not a problem! I’ll just turn the nihilism up to eleven and call it a feature.

That’s all in the future, though, and I may not get even close to that. Right now I’m still working on basic economy , which is a bit of a drag, so I decided to do some rewriting and optimizing for a while instead.

I’ve got 100 peeps right now, though I’d like to be able to scale that up to about 1000 later. My original code simply iterated through every peep every minute of game time (one step), checked their status, added hunger etc, made decisions if needed and had their current action move forward to completion. It worked fine for shorter times, but moving time ahead by one month unsurprisingly took a while to calculate, so I added a loading bar for convenience (Shout out to @Pkeod for helping me figure that out!) and continued writing horridly inefficient code.

When I started with the optimisation it took about 30 second to advance time by 6 months. After about three days I managed to cut that into half, especially by removing peeps from the iteration table whenever they are doing something and adding an “alarm clock” system that lets the program know which peeps are done doing their stuff at appropriate times. I was really proud of that one. I’ve done a lot of other stuff which may not have affected the loading time that much, but which I believe will help me out in the long run. To put it short, I was feeling really good, almost like I’m a real programmer.

Well, this morning I decided to make one last tweak - since the loading bar is only really needed if I’m advancing time by a significant number of steps, I decided to only update it after a certain number of loops. I set the number at 100 just to see if it works correctly and… well, advancing time by half a year now takes less than 2 seconds.

Turns out the loading bar was the worst offender by far.

tl;dr Added a loading bar after code started to be too slow, later spent 3 days optimising to get double the speed, then realized removing the loading bar makes the program run almost instantly.

14 Likes

This reminded me of this old Gamasutra post. Check out “Don’t Sync and Load” at the end of the article.

3 Likes

Nice, that’s quite similar. I used to hate loading bars which tend to update in nasty chunks, then seemingly stop halfway through, then jump erratically only to get stuck on 90% for a long time. I always though “why don’t they make it more smooth an useful?”

I’m beginning to see how messing around with loading code might not be a good idea, especially if you have a deadline.

2 Likes

I think it’s even more generic. When using update function, you have to pay as much attention, as with loops without delays. Also, be aware when checking input constantly - for me, it was a bottleneck once :stuck_out_tongue: timers and “interrupts” are saving :smiley:

1 Like

thanks for sharing your story! We’ll tweet it from https://twitter.com/defold, as it may relate to many people actually.

1 Like

Hmmm… I’ve been considering creating and occasionally updating a Dev Diaries topic for my project. My biggest fear is that knowing myself I’d spend more time writing about it than writing the code itself, though a combination of being ashamed of both how the code looks and how slowly I’m progressing is certainly a factor too. Judging from the reception of this thread, though, it seems like it might actually be worth it.

Then again, there’s not that much to show off right now and I’d end up rambling about the theory and that would eat up way too much time. But I’ll consider starting it after I hit another nice milestone (such as when I’m ready to introduce crime into the world).

1 Like

explaining is the best way to understand yourself what you’re actually talking about. So while you’re learning to code, these blogpost may actually be useful to understand what you’ve learned. And also be useful to those walking the same path as you are.

3 Likes