Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how long will it take to overflow a long by mere increment (starting from zero)?

Tags:

java

If I have code like this

    for (long i = 0; i < Long.MAX_VALUE; i++)
    {
        //do something trivial
    }

How long will it take theoretically for the loop to finish?

like image 999
pdeva Avatar asked Jul 31 '10 00:07

pdeva


2 Answers

Suppose, just for argument's sake, that you've got a very fast computer that can perform about 2 billion loop iterations per second (a 2 GHz machine could just barely do that if there really isn't anything in the loop). Since Long.MAX_VALUE is 2 billion times 4 billion, that loop will take around 4 billion seconds, or something over 120 years.

There's no point in starting that loop today. Wait until computers get faster, and then it will be done sooner.

like image 169
Greg Hewgill Avatar answered Oct 04 '22 19:10

Greg Hewgill


I believe the answer you are looking for is: It will not finish in your lifetime.

Edit: I should qualify that--On existing hardware or any hardware I can conceive of; perhaps when quantum computing becomes popular you will have to re-evaluate this question.

like image 21
Bill K Avatar answered Oct 04 '22 17:10

Bill K