Last active
September 5, 2020 17:57
-
-
Save alpharaoh/0dd252d5cf456383ac67fe1386f0f0e5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
############ Conclusion: | |
# Normal wins every time in speed; by more than 180s at 200,000,000 turns (intel i5 4570R running on a single thread) | |
# That being said, generator method can be more useful and scalable in some situations | |
############ | |
from itertools import count | |
import time | |
class generator: | |
def __init__(self, end: int): | |
self.a = count(start=1, step=1) #Infinite generator that increments by 1 | |
self.b = self.fuzzbuzz(self.a) | |
for i in range(1, end + 1): | |
next(self.b) | |
#print(next(self.b)) #Uncomment if you want to see output | |
def fuzzbuzz(self: object, b: iter) -> iter: | |
while True: | |
self.n = next(b) | |
yield \ | |
"Fuzzbuzz" * int(self.n % 15 == 0) or \ | |
"Buzz" * int(self.n % 5 == 0) or \ | |
"Fuzz" * int(self.n % 3 == 0) \ | |
or self.n | |
class normal: | |
def __init__(self, end: int): | |
self.normal(end + 1) | |
def normal(self, end: int): | |
for i in range(1, end): | |
a = \ | |
"FuzzBuzz" * int(i % 15 == 0) \ | |
or "Buzz" * int(i % 5 == 0) \ | |
or "Fuzz" * int(i % 3 == 0) \ | |
or i | |
#print(a) #Uncomment if you want to see output | |
if __name__ == "__main__": | |
test = 50000 #Turns of fuzzbuzz | |
t0 = time.time() | |
generator(test) | |
t1 = time.time() | |
generator_total = t1 - t0 | |
t2 = time.time() | |
normal(test) | |
t3 = time.time() | |
normal_total = t2 - t3 | |
print("*" * 100) | |
if generator_total > normal_total: | |
diff = round(generator_total,4) - round(normal_total,4) | |
print(f"Normal was faster than generator by {diff}s") | |
if normal_total > generator_total: | |
diff = round(normal_total,4) - round(generator_total,4) | |
print(f"Generator was faster than normal by {diff}s") | |
print("*" * 100) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment