How long does an x++ increment take?

Asked

Viewed 138 times

0

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


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

I can’t wear something like Thread.Sleep() so that there is no constant referral. Assuming I can’t change that condition programmatically due to the possibility that it might be physical (like a sensor being broken or something like that), I thought I’d test a counter to "simulate time". At least, it was the first thing that came to mind.

  • I think you could benchmark, because the results are too variant for each machine, in the case of ++ and -- is likely to be imperceptible, even using millionth of a second to calculate.

  • I gave an answer, if you have a specific reason for the question, add, that I supplement the answer.

  • 4

    Hdeiro, I suggest you create another answer by exposing your real problem, including some relevant code snippet. There are several mechanisms in Java to deal with events and timing and you can’t know which one is best suited just for this information. Hug!

  • 1

    Search on Scheduler, probably what you need. Scheduling task routines, or simply sending loops.

  • The application is in my stage. Monday I add!

  • @Hdeiro You can accept an answer if it solved your problem. You can vote on all the posts on the site as well. Did any help you more? Something needs to be improved?

  • I solved otherwise using Joda-time. :)

Show 2 more comments

2 answers

10

Very little. It is a few processor cycles (it itself can be 1, depends on the processor). It is even difficult to measure. Measuring things like this is complicated because the time it takes to make the code run and make the measurement will be far greater than the time it takes to make the code run.

Anyway it is irrelevant to know, this is one of the fastest operations that exists in the processor. And if you need it, you will have to use.

What slows something down in code is not that. Don’t worry about that kind of detail, especially using Java.

After editing the question it is clear that this does not make sense at all, and here is as curiosity. You cannot use one of the fastest operators to spend time. Even if it was slow it would only bring problems to the application. The problem is different and the solution does not go through what was asked. It’s worth opening another question explaining the real problem you’re facing so people can help you and give you a proper solution.

9

It does not make much sense to speak in the time it takes a specific instruction to execute, outside the context of the program as a whole, due to the pipeline processor. Not every instruction (especially in Java) takes the same time to execute, some may take a [amortized] cycle, and others may take several cycles. If your increment occurs right 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 (i.e. the program will take the same time with or without this instruction). Because the processor will "fit" the increment in the "waiting time" of the previous instruction.

In addition, an increment operation is something very simple, which should not take more than the minimum necessary for the 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 the access to any value (say, an element of an array) can suffer several Misses cache (or even provoke a pagination) the increment itself will take very little time.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.