The greatest timekeeping hack is the Silent Sweep Wall Clock Timing. It allows you to time your program through the wall using a fixed number of pulses, rather than just timing a simple loop. It’s something you’ll find useful if you’re writing games or anything that requires timing other activities. It’s not a hack that’s particularly groundbreaking, and the concept is well known. It does however require you to make a few changes to your code, so I figure writing up a simple example is better than leaving it as an almost-secret.
I’m going to use my existing clock program from previous posts, but we’ll tweak it by using some features of the Silent Sweep Wall Clock Timing. This is important to note, as you can’t just slice and dice old code for this task.
While I’m at it, I’ll cover the method behind the method. The code is actually a pretty simple way of doing it, and once you get a hang of it should take only five minutes to add. The basic idea is that we’re going to count clock pulses using a delay loop instead of using the timing of a simple loop. A delay loop uses an “if-break” style structure where if the desired time has been reached, we break out and end our monitoring. This isn’t the most efficient way of doing things, but this is a timing-centric article.
Making the Delay Loop
Before we get into the code itself, let’s look at how I’m going to drive my program using the delay loop. I’ll use a simple timing loop which takes an item from an array and puts it in a variable which keeps track of how many ticks have passed. Inside this loop, it will take that variable (which starts at 0) and add on 60, subtracting 60 if needed until it reaches 0 again.
What is silent sweep wall clock?
We’re going to use this loop again, but instead of using the timing loop to time the item from the array, we’ll use it simply to delay. Rather than using a simple “if-break” structure inside the timing loop, we’ll count up until we reach a number of pulses that we want and then exit. I’ve labelled these as our “pulse” points. It will take 60 pulses for me to get my timing value from 0 to 1 second (aka 60 ticks). For figuring out how many pulses are involved in my custom loop, I’m going to use what’s called an “anti-time counter”. These are simple loops that count up to the value of a specific number, in this case 60.
By using the anti-time counter, we can obtain the number of pulses that would take to do a custom loop. So in our example above, it would take 60 pulses to get my count from 0 to 1 second. We’re going to use these numbers as our timing points as well. These points will tell us exactly when our task is complete with exact precision. We’ll cover how we’ll do this at the end below, but first let’s talk about how we’ll drive all of these events together.
Is there a wall clock that doesn’t tick?
While the idea of using a wall clock to time things may seem like a crazy one, it actually works pretty well. If you’ve ever played around with timing games or timed the movement of any other computer software, you’ve likely noticed that having a fixed number of ticks or pulses isn’t too much different than not having the timer. How often your computer checks and updates your pixels on the screen is just as important as how long your program takes to do its thing.
In order for this hack to work, we don’t have to use a wall clock or even be in real-time. The only requirement is that we’re using an array which we’ll regularly iterate over (and check) at specific intervals. We can even use a simple delay to generate these intervals. The key to making this work is that we have a way of knowing when the array has been completed. We want to know when we’ve reached the number of pulses we want.
I’m going to assume here that you’re using a simple for-loop to do your monitoring of the pulses. Inside your loop, you’re simply going from 0 (zero) until you reach 59 (or whatever number is in the array). When we reach our final pulse point, then we’ll know that our task has been completed and we can exit out.
Are sweep clocks silent?
While we may be using a simple for loop here, some sweep clocks still do tick (or at least they did back in the day when I first started writing this stuff). This is a bit of an exaggeration, but the concept is the same. They’re just not so good at it.
Rather than use a simple “if-continue” loop as we did with our custom timing loop, we’re going to take a different approach and make our own pseudo-tick counter. This is similar to how an anti-time counter can provide the number of ticks you need for your timing task. Instead, we’ll use the “get_value” method which is available on our array object (the one that contains our timing tasks). The get_value method returns a number of true/false flags that represent the ticks we’re dealing with. If you remember from the basic loop above, it takes 60 pulses to get to a second (so if the number of true/false flags is 0, then we know we’ve only been able to get up to 59 pulses).
Since we know that our task should take 60 pulses in order for it to complete successfully, we simply want to do this test on our array every time through our loop. By checking this value against what we know is 60 (in this case 1), we can tell whether or not our task has taken place within those 60 ticks.
So there you have it. Just a bit of code which does exactly what we need. By combining the delay loop and the anti-time counter, we’re able to time our own custom loops and tasks so that they run at a specific speed. It should only take about five minutes to implement this in your own programs, and it’s really not that hard to remember the method behind it even after you’ve finished (as long as you don’t delete this post).
In the end, we’re just talking about how long it takes to do a task. This concept is pretty straightforward and it’s the same whether you’re working on a simple digital clock or writing your own games. We’re going to talk about creating our own types of counters in the next post, but that’s just adding another layer of complexity. In this case we’ve kept things really simple and still got the job done.