▲ 911 ▼ I wish (i.imgur.com) submitted 2 years ago by nave@lemmy.zip to c/lemmyshitpost@lemmy.world 187 comments fedilink hide all child comments
[–] Pulsar@lemmy.world 6 points 2 years ago (1 child) https://realpython.com/python-modulo-operator/#how-to-check-if-a-number-is-even-or-odd I just wonder why module is the wrong solution. permalink fedilink source parent hideshow 2 child comments replies: [–] mellejwz@lemmy.world 3 points 2 years ago (3 children) 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. permalink fedilink source parent hideshow 6 child comments replies: [–] dukk@programming.dev 5 points 2 years ago (1 child) That’s what I was thinking too… Although, I wouldn’t be surprised if most languages convert modulo 2 to this automatically. permalink fedilink source parent hideshow 2 child comments replies: [–] mellejwz@lemmy.world 1 point 2 years ago (1 child) That could also be the case. permalink fedilink source parent hideshow 2 child comments replies: [–] dukk@programming.dev 2 points 2 years ago That’s the main issue with premature optimization: do it the “optimized” way and it may still be inefficient, or do it the obvious way and let the compiler turn it into its most optimized form. (Of course, not the case with all languages, but most mainstream compilers optimize the code to a decent extent.) permalink fedilink source parent [–] herrvogel@lemmy.world 3 points 2 years ago Modern compilers and interpreters are smart enough to figure out what you're trying to do and automatically do that for you. permalink fedilink source parent [–] ziviz@lemmy.sdf.org 2 points 2 years 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. permalink fedilink source parent
[–] mellejwz@lemmy.world 3 points 2 years ago (3 children) 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. permalink fedilink source parent hideshow 6 child comments replies: [–] dukk@programming.dev 5 points 2 years ago (1 child) That’s what I was thinking too… Although, I wouldn’t be surprised if most languages convert modulo 2 to this automatically. permalink fedilink source parent hideshow 2 child comments replies: [–] mellejwz@lemmy.world 1 point 2 years ago (1 child) That could also be the case. permalink fedilink source parent hideshow 2 child comments replies: [–] dukk@programming.dev 2 points 2 years ago That’s the main issue with premature optimization: do it the “optimized” way and it may still be inefficient, or do it the obvious way and let the compiler turn it into its most optimized form. (Of course, not the case with all languages, but most mainstream compilers optimize the code to a decent extent.) permalink fedilink source parent [–] herrvogel@lemmy.world 3 points 2 years ago Modern compilers and interpreters are smart enough to figure out what you're trying to do and automatically do that for you. permalink fedilink source parent [–] ziviz@lemmy.sdf.org 2 points 2 years 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. permalink fedilink source parent
[–] dukk@programming.dev 5 points 2 years ago (1 child) That’s what I was thinking too… Although, I wouldn’t be surprised if most languages convert modulo 2 to this automatically. permalink fedilink source parent hideshow 2 child comments replies: [–] mellejwz@lemmy.world 1 point 2 years ago (1 child) That could also be the case. permalink fedilink source parent hideshow 2 child comments replies: [–] dukk@programming.dev 2 points 2 years ago That’s the main issue with premature optimization: do it the “optimized” way and it may still be inefficient, or do it the obvious way and let the compiler turn it into its most optimized form. (Of course, not the case with all languages, but most mainstream compilers optimize the code to a decent extent.) permalink fedilink source parent
[–] mellejwz@lemmy.world 1 point 2 years ago (1 child) That could also be the case. permalink fedilink source parent hideshow 2 child comments replies: [–] dukk@programming.dev 2 points 2 years ago That’s the main issue with premature optimization: do it the “optimized” way and it may still be inefficient, or do it the obvious way and let the compiler turn it into its most optimized form. (Of course, not the case with all languages, but most mainstream compilers optimize the code to a decent extent.) permalink fedilink source parent
[–] dukk@programming.dev 2 points 2 years ago That’s the main issue with premature optimization: do it the “optimized” way and it may still be inefficient, or do it the obvious way and let the compiler turn it into its most optimized form. (Of course, not the case with all languages, but most mainstream compilers optimize the code to a decent extent.) permalink fedilink source parent
[–] herrvogel@lemmy.world 3 points 2 years ago Modern compilers and interpreters are smart enough to figure out what you're trying to do and automatically do that for you. permalink fedilink source parent
[–] ziviz@lemmy.sdf.org 2 points 2 years 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. permalink fedilink source parent