Comment on I wish
BeigeAgenda@lemmy.ca 11 months ago
Good job my young padawan, let me teach you about the modulo operator …
Comment on I wish
BeigeAgenda@lemmy.ca 11 months ago
Good job my young padawan, let me teach you about the modulo operator …
mryessir@lemmy.sdf.org 11 months ago
Actually the modulo operator is the wrong solution.
BeigeAgenda@lemmy.ca 11 months ago
No its not the wrong solution! Premature optimization is a waste of time.
Using if or case is not a solution because it is way too verbose and very easy to introduce an error.
Modulo is a solution, and using bit-wise and is another faster solution.
droans@lemmy.world 11 months ago
It’s only the wrong solution if you’re writing something where every operation needs to be accounted for. Modulo is a great, easy, readable method otherwise.
Not too certain on C++, but I think this would be the cleanest implementation that still somewhat optimizes itself:
mryessir@lemmy.sdf.org 11 months ago
You call it premature optimization. I call it obvious.
You use a flat head as a Phillip’s.
Maalus@lemmy.world 11 months ago
You can call it whatever you like, the fact of the matter remains - code readibility is more important than most optimizations you can ever hope to make.
Bad programmers optimize everything and produce code that is not understandabe and 0.001% “faster”
BeigeAgenda@lemmy.ca 11 months ago
I call it making assumptions that may be incorrect, and do you know if the compiler will do the optimization anyway in this case?
TheManuz@lemmy.world 11 months ago
Wrong means that it doesn’t produce the right output.
How is the modulo operator the wrong solution?
mryessir@lemmy.sdf.org 11 months ago
You are right. I have been biased.
Pulsar@lemmy.world 11 months ago
realpython.com/python-modulo-operator/#how-to-che…
I just wonder why module is the wrong solution.
mellejwz@lemmy.world 11 months ago
Not neccessarily wrong, but you could also check the first bit. If it’s 1 the number is uneven, if it’s 0 the number is even. That seems to be more efficient.
ziviz@lemmy.sdf.org 11 months ago
Huh… That makes sense. Til. Ran some tests but speed is pretty similar. Only 4% faster using bitmath or 300 milliseconds difference after 10mil runs.
herrvogel@lemmy.world 11 months ago
Modern compilers and interpreters are smart enough to figure out what you’re trying to do and automatically do that for you.