Counting up to one trillion

How long does it take a computer to count up to one trillion?

This is a question that came after a discussion with a colleague of mine at the university. He was telling me about some company worth over 20 trillion dollars – and he just couldn’t imagine how that much money would look like in cash. Just to get an idea of it we calculated how many hundred dollar bills it would take to circle the Earth once – the answer was I believe around 240.000.000 meaning around 24 billion US dollars. That’s a lot of money. How much would it take a person to count that much money? Well, I can’t say for sure.

What we can do to get an idea is see how much it would take a computer to count up to that value. To simply iterate, no other action in between. For that I’ve wrote a simple piece of code which measures how much it takes to count up to one million and then does some simple math to estimate how much it would take to count up to different values and then displays the results in a friendly way using a method courtesy of StackOverflow.

The results are interesting. And the answer is: it depends on your machine. Even on the same machine you will get different results depending on the load. But let’s look at mine for a bit:

  • one billion (9 zeros) is being reached fast – 15 seconds
  • but to get to one trillion (12 zeros) – the difference is amazing – 4 hours and 10 minutes. Basically 1000 times more.
  • the differences get even more impressive as we go up to quadrillions (15 zeros)¬† which would take 173 days and then quintillions (18 zeros) which would take 475 years
  • the last one for which I did the math is one sextillion (21 zeros) and get ready – it would take my laptop exactly 7708 years, 292 days, 6 hours, 43 minutes and 52 seconds to iterate up to that value.

As I’ve said – these values depend a lot on the machine. So you can give it a try yourself and maybe share the results. Code below:

static void Main(string[] args)
{
    var startTimestamp = DateTime.Now;
    for (int i = 0; i <= 1000000; i++);
    var elapsedTime = DateTime.Now - startTimestamp;

    Console.WriteLine(string.Format("{0} for 1 billion (9 zeros),", GetReadableString(new TimeSpan(elapsedTime.Ticks * 1000))));
    Console.WriteLine(string.Format("{0} for 1 trillion (12 zeros)", GetReadableString(new TimeSpan(elapsedTime.Ticks * 1000000))));
    Console.WriteLine(string.Format("{0} for 1 quadrillion (15 zeros)", GetReadableString(new TimeSpan(elapsedTime.Ticks * 1000000000))));
    Console.WriteLine(string.Format("{0} for 1 quintillion (18 zeros)", GetReadableString(new TimeSpan(elapsedTime.Ticks * 1000000000000))));
    Console.WriteLine(string.Format("and {0} for it to count up to 1 sextillion (21 zeros).", GetReadableString(new TimeSpan(elapsedTime.Ticks * 1000000000000000))));

    Console.ReadKey();
}

private static string GetReadableString(TimeSpan span)
{
    string formatted = string.Format("{0}{1}{2}{3}{4}",
    span.Duration().Days > 364 ? string.Format("{0:0} year{1}, ", span.Days / 365, span.Days == 365 ? string.Empty : "s") : string.Empty,
    span.Duration().Days > 0 ? string.Format("{0:0} day{1}, ", span.Days % 365, span.Days % 365 == 1 ? String.Empty : "s") : string.Empty,
    span.Duration().Hours > 0 ? string.Format("{0:0} hour{1}, ", span.Hours, span.Hours == 1 ? String.Empty : "s") : string.Empty,
    span.Duration().Minutes > 0 ? string.Format("{0:0} minute{1}, ", span.Minutes, span.Minutes == 1 ? String.Empty : "s") : string.Empty,
    span.Duration().Seconds > 0 ? string.Format("{0:0} second{1}", span.Seconds, span.Seconds == 1 ? String.Empty : "s") : string.Empty);

    if (formatted.EndsWith(", ")) formatted = formatted.Substring(0, formatted.Length - 2);

    if (string.IsNullOrEmpty(formatted)) formatted = "0 seconds";

    return formatted;
}

Then, in the true spirit of an engineer student, I switched to another subject – totally related (for me) – the uniqueness of GUIDs. I had previously asked myself how unique a GUID actually is. And I somewhat got my answer back then but now I think it’s even more clear.

To start – GUIDs are usually represented as 32 hexadecimal digits – meaning 10 more digits than one sixtillion. Then again, each of the digits in a GUID can have values from 0 to F (16 values) compared to a normal integer which only allows 0 to 9 (meaning 10 values). And where in the previous example we counted up to just one, here we’ll go up to F.

So if my math is correct, we take the time from the sixtillion (7708 years) – multiply it by 10.000.000.000 (the ten zeros), then by 1.6 – there are 1.6 more values for each digit – and then by 16 (F).

That’s about 1973248000000000 years – meaning 1973248 million millenias.¬†That’s how much it would take my laptop to iterate trough all the possible values of a GUID. Now how unique is that?




2 Comments

I’m a n00b and usually use bash for my scripting. Is this powershell? Vbs?
How can I utilise this script to show my kids that we don’t in fact need to leave the computer on to count to a quadrillion?
Thx!
Great idea!
Neil

Hi Neil, I’ve done this in a C# Console Application using Visual Studio. If you don’t have VS & want to download it – the Community edition is free.

But I guess you can achieve the same in any language.
1. You get the current date&time / timestamp
2. You create a for loop & iterate (count-up) to 1.000.000
3. You get a new timestamp & make the difference with the current one. This will tell you how much it took your computer to count up to 1 million.

From here on it’s just a matter of multiplying the value you got & extrapolate how much it would take to count up to a quadrillion. If you want a very accurate value you can ofc try & let the computer count up to a quadrillion.

Leave a Reply

Your email address will not be published. Required fields are marked *

Are you a human? * Time limit is exhausted. Please reload the CAPTCHA.