Hmm, there could be more. the version with the most used dependencies. the longest in code, the longest in running, the most inefficient,one with mixed languages, with most verbosity .... It's a hell, and we're trapped in it now
Even better, as long as the denominator in the division is known at compile time, the compiler will transform the division into some adds, muls, and shifts using some modulo arithmetic magic
Indeed. If this is the cause of your performance issues, bravo, because if that's true, it's the most optimized code on the planet.
Also, written code is for humans to read to specify to them what you intend. That a computer can make sense of it is mostly a nice side effect. Otherwise, we'd all be pushing bits manually.
I wonder how this works for interpreted languages like Python. (Talking about CPython implementation)
I guess it's not possible to optimize the modulo operation since you have to check under the hood thinks like if the operands are integers before doing the actual operation
The overhead of the interpreter will outweigh the cost of simple operations, making it a moot point. If you care, the speed difference seems to be about 18%:
C:\Users\Chris Kaynor>python
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:06:47) [MSC v.1914 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import timeit
>>> timeit.timeit('a=1\na%2')
0.036897699999999034
>>> timeit.timeit('a=1\na+2')
0.031352899999999906
And just to prove its not optimizing it under the hood:
Afaik the jit compiler will make assumptions about the types and optimize based on them. As long as those assumptions hold, you get close to native speed.
If you think that then maybe you shouldn't be coding. Just sayin.
EDIT: After the downvotes I feel I must clarify what I meant. If you can't have faith in your code then why code at all? I also never said anything about one's code, just don't trust that the compiler will always fix things or optimize things for you - because it doesn't, I've seen it first-hand looking at disassembly listings and teasing out what triggers the compiler to vectorize code and how easy it is for it to miss something that should and could be vectorized. The compiler is not smart and if you're going to write code you should be able to learn how to put good practices to use and be able to stand behind your work, period.
I'd say that it is way safer to assume the compiler is smart than to trust your code is smart lol
This sounds like an excuse to write shitty code. "The compiler will take care of it! durrr"
EDIT: Nor was I talking about anyone's code in the first place - just the compiler. You should never assume the compiler is just magically going to optimize everything.
Of course it's "smart" enough to optimize that. Any programmer with decent experience knows that. Not every performance sensitive piece of code is just a modulo being optimized into a bitwise and.
Some things are too complex to quickly and easily paste into godbolt in an isolated fashion, while still being relevant to what is actually output when the production binary is linked. It's easier to just look at a disassembly listing at that point, especially if you want to see what the link-time optimizations are doing too. For complicated code it's better to see what the actual binaries the compiler is outputting are like, such as when there are large complex algorithms with gigabytes of data structures being handled. Not everything is an isolated little chunk of code you can extract and paste into godbolt.
It’s the bitwise and operator. It takes two numbers, and for each of its binary digits, ands them together like so:
00101011 &
01100101 =
00100001
When you bitwise and with 1, which is binary 00000001, it ignores the first seven bits(since they are all 0s, so they always produce 0 when anded). In essence, it discards all but the last bit of the number. This will leave you with either a 0 or a 1 in the ones place of the result - like how in our decimal system we have a ones, tens, and hundreds place, binary has ones, twos, fours, eights, etc. If this bit is a 0, the number must be even, and if it’s a 1 the number must be odd.
It seems complicated when I explain it, but for computers this is many times easier to perform than modulus, since they do pretty much everything on a bitwise level.
I hate to pile on, but the number of programmers that don't know what modulus means in the mathematical sense, and then that it exists as an operator in all common programming languages boggles my mind.
Interesting. Wonder what his thought process was, there. I mean, I have irrational hatred of things too so I get it, but that's one I haven't come across before.
Division (and by extension modulo) is significantly slower than any other basic math operation (addition, subtraction, multiplication). It's still fast enough that you generally don't need to think about it, but it's something to keep in mind when working with especially performance-critical applications.
it's something to keep in mind when working with especially performance-critical applications.
I come from application development so this is way out of my wheelhouse, but I'd be curious to see an app where this sort of optimization is the best bang for the buck.
I'm sure they exist; but not generally in my world for sure. I'm a little gunshy of people espousing these sorts of micro-optimizations for a few 10's of ms of gain over a few hours of use, when the app is generally not well designed to begin with, and/or normal network latency would overshadow the gains by orders of magnitude.
Sure, by "especially" I really do mean "especially". In stuff like hot loops that'll be running a tight sequence of code on ten million array entries, that sort of thing.
That may look bad, but think of the processor that is calculating number.toString() 10 times and getting the same last character over and over. That processor is cold and weak, but now, thanks to all this unnecessary computation, it's getting a workout and a bit of extra heat in these cold, cold times. This isn't bad code. This is thoughtful code, helpful code, like lifting heavy bags to add fuel to your dumpster fire.
242
u/abrams666 Nov 04 '21
Is this "isEven" thing a competition?
148
u/fatnote Nov 04 '21
I can't even
63
u/Rainbow-Dev Nov 04 '21
Ah, time for the age-old debate of x%2 vs x&1
39
u/EquationTAKEN Nov 04 '21
I always knew 3.14 was even.
32
u/vashy96 Nov 04 '21
And it's way better than the "is-even" node.js package.
22
u/seeroflights Nov 04 '21
Image Transcription: Code
I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!
52
u/RogueFox771 Nov 04 '21
That's... Actually an interesting approach. Credit for thinking way outside the box
7
u/Kyyken Nov 04 '21
"just use a logarithm"
6
u/RejectAtAMisfitParty Nov 04 '21
Lately I’ve been learning that modulo is an advanced concept
6
u/campbellm Nov 04 '21
I hate to pile on, but the number of programmers that don't know what
modulusmeans in the mathematical sense, and then that it exists as an operator in all common programming languages boggles my mind.5
u/sohang-3112 Nov 04 '21
Did he take some drugs before writing this, or is he a natural?
4
u/TinyBreadBigMouth Nov 04 '21
4
u/xiipaoc Nov 04 '21
That may look bad, but think of the processor that is calculating
number.toString()10 times and getting the same last character over and over. That processor is cold and weak, but now, thanks to all this unnecessary computation, it's getting a workout and a bit of extra heat in these cold, cold times. This isn't bad code. This is thoughtful code, helpful code, like lifting heavy bags to add fuel to your dumpster fire.3
u/--var Nov 04 '21
Late to the show again! But in javascript, you can easily test this with a regular expression, obviously.
2
u/JonMW Nov 04 '21
It took me, like, four tries to figure out how it worked. This did not improve matters.
2
u/TeenFlash Nov 04 '21
wouldn't it be funny if it returned the wrong output despite all this?
1
u/HerissonMignion Nov 04 '21
r/badIsEven
1
u/Drakethos Nov 04 '21
modulus people , modulus 😮💨
1
u/not4u2see Nov 04 '21 edited Nov 04 '21
When all you know is the if statement
1
u/redddooot Nov 11 '21
FYI use
str.slice(-1)to get last character, so much better-1
u/dasmarcy Nov 04 '21
At some point, I didn't find it too bad, but the more I look at it the worse it gets.
-6
u/TheAwesome98_Real evil Nov 04 '21
return (number/2).isInteger();-16
u/dryu12 Nov 04 '21
If it is stupid but it works, it is not stupid.