It is more efficient to reset the counter or allows it to increment and use modulo

Say you need to track the number of times the method is called, and print something when it has been called n times. What would be most effective:

  • Use the long variable _counter and increment it every time the method is called. Every call you check for equality "_counter% n == 0"

  • Use the variable _counter and increment it every time the method is called. When _counter = n, print the message and reset the _counter variable to 0.

Some would say that the difference is not significant, and you are probably right. I'm just wondering which method is most commonly used

+4
source share
6 answers

In this particular case, since you need to have an ANYWAY if statement, I would say that you just need to set it to zero when it reaches the count.

However, for the case when you use the value every time and just want to "wrap rounding to zero when we reach a certain value", then the case is less obvious.

If you can set n to power 2 (2, 4, 8, 16, 32 ...), then you can use the counter % n trick, the same as counter & (n-1) - which makes the operation REALLY fast.

If n not the power of two, then the likelihood that you will end up doing a real break, which is a bad idea, is very expensive to divide compared to regular instructions, and comparing and reset is probably faster than the split option.

Of course, as others have said, if your counter ever reaches the MAX limit for this type, you can have fun and games.

Edit: And of course, if you type something, it probably takes 100 times longer than division, so this is really micro-optimization if n quite large.

+7
source

It depends on the value of n ... but I'm sure resetting and simple equality checking is faster. In addition, resetting the counter is safer; you will never reach the performance limit for your number. Editing: Also consider readability, making micro optimizations can obscure your code.

+1
source

Why not get around both.

If this becomes a problem, then see if it’s worth optimizing.
But it makes no sense to even look at it until it is a problem (there will be much more problems in your algorithms).

 count = (count+1) % countMax; 
+1
source

I believe that it is always better to reset the counter for the following reasons:

  • The code is more understandable to an unfamiliar programmer (for example, a maintenance programmer).
  • You have less chance of arithmetic overflow (possibly bad spelling) when you reset the counter.
0
source

Checking the Guava RateLimiter will give you some idea of ​​a similar implementation of the http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/util/concurrent/RateLimiter.html utility

0
source

Here are the runtimes for 100,000,000 iterations, in ms

modTime = 1258

counterTime = 449

po2Time = 108

As we can see, Power of 2 is superior to other methods today, but only for degrees 2, also our simple counter is almost 2.5 times faster than the module. So why would we like to use module increments in general? In my opinion, I believe that they provide clean code and, if used correctly, they are a great tool for learning

original message

0
source

All Articles