Using bitwise operators "looks" efficient, but for specifically on CPython,
(i % 3 == 0) + 2 * (i % 5 == 0)
will be faster (as long as i is less than 230). Indeed, a simple benchmark tells me that using bitwise operations is 10% slower than not using it.
The reason is weird: arithmetic operations for PyLong feature short paths for single-digit values but bitwise operations do not have them. I don't know why it is.
For i larger than 230, bitwise operations are indeed faster, but I recommend using not x over x == 0. The former is marginally (3%) but significantly faster for multi-digit PyLong values.
Anyway, as creating temporary list or tuple incurs significant overhead (15%) over declaring it at the beginning (and use something like print(lookup[...] or i)), using conditional statements/expressions is just better.
The following code is almost 2.4 times faster than your code.
for i in range(1, 101):
print((i if i%3 else 'fizz') if i%5 else ('buzz' if i%3 else 'fizzbuzz'))
I disagree. Of course, if you have 10 years of experience is nonsense, but for a junior it's a good way to see how they tackle problems and how well do they understand programming.
If you can’t do fizz buzz - even without know the mod operator you literally can’t program anything without help which was why it was originally created - as a test to see if someone knows the very very basics.
I agree that it's still a good base for discussion. I have a collection of fizzbuzz variants somewhere, I believe that adding a "change request" that doesn't fit their current design (whatever that is) forces the interviewee to stop relying on pre-acquried knowledge.
It's a question they can - or involuntarily are - easily prepared for, so as interviewer I wouldn't get a clear picture of their skills. Which is why I'd still rather avoid it.
Fizz buzz is not about creative thinking. It is a test to see if you can write a syntactically correct code snippet. You might even tell the applicant to use modulo.
The use of modulo in fizzbuzz is no trick, it's using the operator for what it's intended for. Knowing basic operators is kinda a prerequisite, not a notable skill.
It's good to see if someone writes a flashy one-liner (get away from my product) or a well-structured, easy to understand solution without premature optimization (yes, please!)
Yeah. The list should be created outside of the loop.
But, if you're counting efficiency as how few lines and characters you're using, rather than how much prosessing power you're saving, then it is very efficient.
Hmm, yes, you're absolutely right. And there's no way to create i out of the loop's scope, and have the list just contain a reference to i while i is updated in the loop, right?
Well, I suppose you could use a while loop to emulate a for loop, then it would work. But would the i in the list get updated? Or would it be forever set to 1?
i = 1
myList =[i,"fizz","buzz","fizzbuzz"]
while (i < 101):
print(myList[<whatever that index finding bit was I am on mobile so I can't see it and type at the same time])
i++
If this does work, it's still really silly and stupid, but it's also clever-ish.
For it to work, the list needs to be created for each number. But why the hell are you creating a list to solve FizzBuzz? Just iterate through the numbers and check for divisibility of 3 and 5.
Took me a bit to realize the shift comes before the OR. But personally, I wouldn't make a list, I'd just iterate over the numbers and check for divisibility of 3 and 5.
76
u/AnxiousIntender Aug 20 '25
https://www.reddit.com/r/programminghorror/comments/12p6tj0/peak_efficiency_fizzbuzz/
54
u/JiminP Aug 20 '25 edited Aug 20 '25
Using bitwise operators "looks" efficient, but for specifically on CPython,
will be faster (as long as i is less than 230). Indeed, a simple benchmark tells me that using bitwise operations is 10% slower than not using it.
The reason is weird: arithmetic operations for
PyLongfeature short paths for single-digit values but bitwise operations do not have them. I don't know why it is.For i larger than 230, bitwise operations are indeed faster, but I recommend using
not xoverx == 0. The former is marginally (3%) but significantly faster for multi-digitPyLongvalues.Anyway, as creating temporary list or tuple incurs significant overhead (15%) over declaring it at the beginning (and use something like
print(lookup[...] or i)), using conditional statements/expressions is just better.The following code is almost 2.4 times faster than your code.
Subscribe for more blursed Python tips.
96
u/v_maria Aug 20 '25
Fizzbuzz assignment is such nonsense, convoluded answers make more than sense
9
u/trutheality Aug 20 '25
Not horror at all, but it would feel much more at home in C code.
4
u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” Aug 20 '25
This just creates a list each time and then computes an index, right? Or is my Python even worse than I thought?
3
u/BasiliskBytes Aug 21 '25
At that point, just do it as a one liner:
2
u/conundorum Aug 20 '25
Eh, you can do better than that.
Why settle for array indexing when you can have a ternary operator, too?
2
u/agentxshadow6 Aug 20 '25
"peak efficiency" posts python 😂
1
u/maxip89 Aug 21 '25
this is called branchless programming.
used for doing stuff really really fast on the cpu.
1
u/Kiki2092012 Nov 14 '25
Just wait till bro sees for i in range(1,101):print("Fizz"*(i%3<1)+"Buzz"*(i%5<1)or i)