How long does it take to overflow with a long simple increment (starting from zero)?

If I have code like this

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

How long will it take theoretically to complete the cycle?

+7
java
source share
5 answers

Suppose, simply for the argument, that you have a very fast computer that can execute about 2 billion cycles per second (a 2 GHz machine can just do this if there really is nothing in the cycle), since Long.MAX_VALUE is 2 billion times 4 billion, this loop will take about 4 billion seconds, or something more than 120 years.

There is no point in starting this cycle today. Wait until the computers are faster, and then this will be done earlier.

+15
source share

I believe that the answer you are looking for is: it will not end in your life.

Edit: I have to qualify this: on existing equipment or on any equipment that I can imagine; perhaps when quantum computing becomes popular, you will have to reevaluate this question.

+4
source share

This may take a lifetime.

But according to the title of the question, I think you are trying to run the loop so many times, and you plan to break the loop when a certain condition arises. those. you have

 if (ancondition == true) { break; } 

And you just need to make sure that you are not getting any exception due to overflow. (for example: you are going to monitor the seismic sensor for many years.) Thus, you can safely walk in this area.

But, on the other hand, this is not a good approach to programming. You can definitely improve the code using a different method. (Maybe periodic checks)

+2
source share

one possible answer is zero per second. the compiler can be very aggressive and optimize your whole loop, since apparently it does nothing interesting ...

here's a great story: compilers have optimized a loop that looks for an example of scrolling through Fermat's last theorem. because the theorem is true (that's why we are told), and there is no counter example, the cycle should never end. but compilers decide that it should end.

http://blog.regehr.org/archives/161

(the article mentions that the java compiler is not allowed to complete the farm loop)

+1
source share

put it on wolframalpha.com

(2 ^ 63 -1) / (2 * 10 ^ 9) seconds

2 ^ 63-1 is the current value of MAX_VALUE, and 2 * 10 ^ 9 is a 2 GHz processor, which increases the cycle after each clock cycle.

now put

(2 ^ 63 -1) / (4 * 10 ^ 9) seconds

which represents a doubling of the speed of the machine to 4 GHz.

The fastest supercomputer makes 1.75 petaflops (10 ^ 15). This is a parallel application, therefore it is not one big cycle, but if you ask "how long will it take to make 2 ^ 63-1 flops", on this machine it

http://www.wolframalpha.com/input/?i=(2 ^ 63 + -1) / (1.75 * 10 ^ 15) + seconds

The fastest speed with which we got the transistor for operation is 500 GHz. from http://en.wikipedia.org/wiki/Moore 's_law

“the transistor worked above 500 GHz at 4.5 K (-451 ° F / -268.65 ° C) [41], and simulations showed that it can operate at 1 THz (1000 GHz). However, this test only tested single transistor. "

therefore, if you connect this to tungsten alpha, you will get 7 months. Thus, a single transistor is not a loop processing unit, but you understand that. Superior equipment is within our technological reach, even to overturn iterations of the 2 ^ 63-1 cycle. If you think that OP will be "how long it takes to make 2 ^ 63 bit flip", then this machine meets these criteria and easily completes in a lifetime.

But when we move on to 128-bit computing, we have no chance ...

0
source share

All Articles