How long does an x ++ increment take? [closed]

0

How long on average an increment of type i ++ takes to be done.

Given the answers and comments placed here: I asked this question because I have a method to send emails when certain condition is reached.

I can not use something like Thread.Sleep() to avoid constant forwarding. Assuming that I can not change that condition programmatically because of the possibility of it being physical (such as a sensor being broken or something like that), I thought of testing a counter to "simulate time." At least it was the first thing that came to mind.

    
asked by anonymous 12.11.2015 / 17:34

2 answers

10

Very little. It's a few processor cycles. It is even difficult to measure. Measuring things like this is tricky, because the time it takes to make the code execute and to do the measurement will be much higher than the work done.

Anyway, it's irrelevant to know, this is one of the fastest operations on the processor. And if you need it, you'll have to use it.

What slows down something in the code is not this. Do not worry about that kind of detail. Even more using Java.

After editing the question, it is clear that this does not make sense at all. And here it is like curiosity. You can not use one of the faster operators to spend time. Even if it was slow, you would only bring problems to the application. The problem is another and the solution does not go through what was asked. It pays to open another question explaining the real problem you are facing so that people can help you and give you an appropriate solution.

    
12.11.2015 / 17:40
9

It does not make much sense to speak of the time a specific instruction takes to execute, outside the context of the program as a whole, due to Pipeline of the processor. Not all instruction (especially in Java) takes the same time to execute, some can take one cycle [amortized] and others can take several cycles. If its increment occurs soon after an operation that takes several cycles - and these two operations are independent of each other - then the time that this instruction will take is effectively zero (ie the program will take the same time with or without this instruction) . Because the processor will "fit" the increment in the "wait time" of the previous instruction.

In addition, an incremental operation is something very simple, which should take no more than the minimum necessary pro processor to execute an "atomic" instruction (again, amortized can reach a single cycle), assuming of course that the variable is in a register. What is reasonable to assume, even if access to any value (say, an element of an array) can suffer multiple cache misses (or even cause a pagination) increment in si will take very little time.

    
12.11.2015 / 17:55