Count your 9s before blaming your CPU

Recently I was working on a Windows Forms application and I tried to create an async model, to see how long does it take to perform the loops. Although it doesn’t take much time for our CPU to calculate the results for our general loops; thousands, or hundred thousand maximum. I created the model for performing the loop as,

// loops, seconds are defined to be long variables.
private async void runAsync() 
{ 
     long res = await result();  
      resultLabel.Text = String.Format("Result of {0} loop addition is {1}.", loops, res);  
  
      // Done.. Stop the timer..  
      timer.Stop();  
      timerLabel.Text = String.Format("{0} seconds took to calculate result of {1} loops", seconds, loops);  
      seconds = 0;  
}  
  
private async Task<long> result()  
{  
     long res = 0;  
     return await Task.Run(() =>  
     {  
          for (var i = 0; i < loops; i++)  
          {  
               res += i;  
          }  
          return res;  
     });  
}

Well, it probably does what it is asked to do so. The only case of interest is, when the value is given to the loop. Let me show you the results for each of these cases…

Having 0-8 9s.

In case of 9s from 0 to 8, the execution takes only a fraction of one second.

Continuing to 8th result now,

Quite similar behavior being shown in these cases. Which shows that CPU is pretty much fast to calculate this much loops easily and to provide the result; if the result is not correct, please avoid mentioning it.

Adding a 9th 9

With a new 9th 9 being added to the loop; the results now amplify, instead of 1 2 or 3 seconds, it takes up to 8  seconds to execute the loop and provide the results.

That shows the significance of a 9 in the row of loops…

Adding a 10th 9

By using another 9 in the loop, I was finally able to manage to make the CPU work for a longer time… Much much longer time. My CPU doesn’t provide any result for this calculation, and took more than 7 or 8 hundred seconds.

Not bad, eh?

I will not say, CPU was not efficient as it was able to calculate the results for 0-8 9s and then started to topple the time by an increment of each 9 (each 9 made the first 9 in the left side tens, from ones, and hundreds from ones and so on making the loop counter to be billions). The number for the loops was, nine billion nine hundred ninety-nine million nine hundred ninety-nine thousand nine hundred ninety-nine. Which is quite reasonable for the CPU to take this much time.

This reminds me of the “Transistor as an amplifier“, current moves and at a critical level, voltage rises and just keeps rising… I am really glad I found this and took time to write a blog about it. 🙂 Hopefully, next time you will count your nines before saying your CPU is fast!

Advertisements

Hey, leave a comment! I love to read and reply to them.

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s