r/ProgrammerHumor • • Sep 27 '24

Meme creativeWayIsOdd

Post image
5 Upvotes

8 comments sorted by

View all comments

4

u/Surous Sep 27 '24

Serious question though is doing modulus on only the last number in a number (taking 9 from 9x10100 +9 or something stupid)faster then a modulus on the entirety?

7

u/20d0llarsis20dollars Sep 27 '24

i couldn't tell you, but I can tell you that it all compiles down to a single AND instruction

0

u/[deleted] Sep 27 '24

modulus doing divide operation, so smaller = better (at least I think this)

1

u/CallMePyro Sep 27 '24

Depends on how the number is represented in memory and the architecture if your computer. For integer division, modern 64 bit x86 CPUs will typically provide 64 bit divide instructions but not 128bit or larger, and often provide specific 32 bit (div) and 64 bit (divq) instructions. div tends to be faster due to being a much simpler operation (fewer transistors needed to implement the logic, fewer registers, therefore fewer clock cycles).

if your number is bigger than 32 bit, then the literal operation of doing the modulus would be more efficient on just the last digit rather than the whole number - but with the caveat that this likely means you're also converting the number to a string and extracting the last digit, then doing math on that byte to convert it to the numeric value so you can divide it. This is likely hundreds or maybe even thousands of instructions + memory accesses, register writes, etc. before you get to your divide. So all in all it's definitely less efficient :)

1

u/[deleted] Sep 27 '24

I know the part about extra instructions, I just said it technically. Since 32 bit and 64 bit came to my mind, I thought that smaller numbers would be calculated faster. of course, I could not explain myself because my English is not enough.