|
I already knew that the "%" (Modulo) operator is translated in a lot of instructions more than one would think at first glance and hence is a bit slower, but as I am trying to scratch every microsecond I can in my project...
I have just made a simple test:
50000 iterations and increasing a variable by one in each match
Loop 1: using i % 3 == 0 and i % 50 == 0
Loop 2: the same but using two helper counters instead. if count3 == 3 and count50 == 50
almost 300% performance difference
I didn't expect that much.
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
Rating helpful answers is nice, but saying thanks can be even nicer.
|
|
|
|
|
Doesn't surprise me that much, it is a division operation after all!
See DIV for 8086 based machines, which provides the remainder - it's an 80-90 cycle instruction, compared to 3 for an ADD or CMP.
http://www.oocities.org/mc_introtocomputers/Instruction_Timing.PDF[^]
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
|
OriginalGriff wrote: it's an 80-90 cycle instruction, compared to 3 for an ADD or CMP
Wow!! I knew it was more, but I didn't know it was 80-90. Amazing.
|
|
|
|
|
What's even better is that it still is, in some cases (64bit division with a non-zero upper half of the 128bit dividend, so a pretty rare case, but it exists).
On the other hand, the latency of addition has been 1 since approximately forever, and with 4 of them in the same cycle. So the chasm between addition and division has widened!
(rumour has it that Ice Lake will feature faster division .. but also faster addition, single-cycle 3-input addition and add-add macro-fusion, so we'll have to see which way that goes)
|
|
|
|
|
% should take the same time as /
The CPU instruction DIV does an integer divide and modulus at the same time.
I am surprised that the divide is only 300% slower, but I am an old fart that coded a divide method on the old 6502 (which does not have a DIV instruction.)
|
|
|
|
|
Depends on how you coded Divide! I have had to play that game with Z80's so many times...
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Division by a constant is not implemented with the division instruction by any reasonable compiler. But the actual implementation is still not that fast.
|
|
|
|
|
Fueled By Caffeine wrote: coded a divide method on the old 6502 (which does not have a DIV instruction.)
Very cool! (Probably painful at the time, but very cool.)
|
|
|
|
|
Divisions are performance heavy, additions are not.
GCS d--(d+) s-/++ a C++++ U+++ P- L+@ E-- W++ N+ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t+ 5? X R+++ tv-- b+(+++) DI+++ D++ G e++ h--- r+++ y+++* Weapons extension: ma- k++ F+2 X
|
|
|
|
|
When possible, I use multiplication instead of division.
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
Me too, if the code has a potentially significant performance impact. Otherwise I use the most readable form.
In some occasions I had to ditch floating point completely and get straight to fixed point integer arithmetic - after having eliminated divisions wherever possible and memory access through matrix (a single pointer going forward of a fixed amount each cycle is extremely fast and helps any caching mechanism).
GCS d--(d+) s-/++ a C++++ U+++ P- L+@ E-- W++ N+ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t+ 5? X R+++ tv-- b+(+++) DI+++ D++ G e++ h--- r+++ y+++* Weapons extension: ma- k++ F+2 X
|
|
|
|
|
|
Test in VS2017 - C++ Console App, Windows 7 (OEM Personalized Edition)
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
Rating helpful answers is nice, but saying thanks can be even nicer.
|
|
|
|
|
If you can arrange it to divide by a power of two, you can use the AND operator. Then look how fast that is going to be.
I have lived with several Zen masters - all of them were cats.
His last invention was an evil Lasagna. It didn't kill anyone, and it actually tasted pretty good.
|
|
|
|
|
I had thought about it, but not sure if it is going to be easy to only use masking. The dynamic part are periods / frequencies
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
Rating helpful answers is nice, but saying thanks can be even nicer.
|
|
|
|
|
|
I find that very surprising. Not long I did some tests because I work on a high performance application and found the difference to be so minor that I didn't care about it.
I would like to see your code if you don't mind. Please e-mail it to me or post on my personal page if you prefer it not be shared publicly.
"They have a consciousness, they have a life, they have a soul! Damn you! Let the rabbits wear glasses! Save our brothers! Can I get an amen?"
|
|
|
|
|
I can't write you privately, you don't have the option active.
Write me an "a-mail" to this answer and I will answer you back with the code.
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
Rating helpful answers is nice, but saying thanks can be even nicer.
|
|
|
|
|
forget my previous message... I found it. (Spam filter)
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
Rating helpful answers is nice, but saying thanks can be even nicer.
|
|
|
|
|
|
I think we surrender ...
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
port == SERIAL
(sounds like cereal)
|
|
|
|
|
Let's see what you come up with tomorrow!
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I was closer than I thought with "FrayBent-O's"!
Whenever you find yourself on the side of the majority, it is time to pause and reflect. - Mark Twain
|
|
|
|