# In search of the fastest FizzBuzz in Python

## Introduction

I wondered what the fastest FizzBuzz was, so I coded some.

## manner

For the function that outputs FizzBuzz from 1 to 100 as standard, measure the time when it is executed 100 times. Do not comment out print even when measuring time.

FizzBuzz problem

``````Write a program that prints numbers from 1 to 100. However, if it is a multiple of 3, print "Fizz" instead of a number, if it is a multiple of 5, print "Buzz", and if it is a multiple of both 3 and 5, print "FizzBuzz".
``````

http://d.hatena.ne.jp/keyword/Fizz-Buzz%CC%E4%C2%EA

## Time measurement method

``````import time
dict = {}
for func in [main1, main2, main3, main4, main5,main6, main7]:
total = 0
for i in range(100):
s = time.time()
func(1,101)
e = time.time()
total += e - s
dict[func.__name__] = total
``````

## main1

For the time being, orthodox.

``````def main1(start,stop):
for i in range(start,stop):
if (i%15) == 0:
print 'FizzBuzz'
elif (i%5) == 0:
print 'Buzz'
elif (i%3) == 0:
print 'Fizz'
else:
print i
``````

## main2

With reference to the Web, I tried to reduce the number of if executions by nesting ifs.

``````def main2(start,stop):
for i in range(start,stop):
if (i%3) == 0:
if (i%5) == 0:
print 'FizzBuzz'
else:
print 'Fizz'
else:
if (i%5) == 0:
print 'Buzz'
else:
print i
``````

## main3

I made a list of the remainder of 15.

``````def main3(start,stop):
fizzbuzz_list = []
for i in range(15):
if (i%15) == 0:
fizzbuzz_list.append('FizzBuzz')
elif (i%5) == 0:
fizzbuzz_list.append('Buzz')
elif (i%3) == 0:
fizzbuzz_list.append('Fizz')
else:
fizzbuzz_list.append(0)

for i in range(start,stop):
print fizzbuzz_list[i%15] if fizzbuzz_list[i%15] else i
``````

## main4

I manually entered the list of main3 in advance.

``````def main4(start,stop):
fizzbuzz_list = ['FizzBuzz', 0, 0, 'Fizz', 0, 'Buzz', 'Fizz', 0, 0, 'Fizz', 'Buzz', 0, 'Fizz', 0, 0 ]
for i in range(start,stop):
print fizzbuzz_list[i%15] if fizzbuzz_list[i%15] else i
``````

## main5

I tried to print once.

``````def main5(start,stop):
fizzbuzz = ''
for i in range(start,stop):
if (i%3) == 0:
if (i%5) == 0:
fizzbuzz += 'FizzBuzz\n'
else:
fizzbuzz += 'Fizz\n'
else:
if (i%5) == 0:
fizzbuzz += 'Buzz\n'
else:
fizzbuzz += str(i) + "\n"
print fizzbuzz
``````

## main6

In the first place, I tried all for and if.

``````def main6(start,stop):
print 1
print 2
print "Fizz"
print 4
print "Buzz"
print "Fizz"
print 7
print 8
print "Fizz"
print "Buzz"
print 11
print "Fizz"
print 13
print 14
print "FizzBuzz"
print 16
print 17
print "Fizz"
print 19
print "Buzz"
print "Fizz"
print 22
print 23
print "Fizz"
print "Buzz"
print 26
print "Fizz"
print 28
print 29
print "FizzBuzz"
print 31
print 32
print "Fizz"
print 34
print "Buzz"
print "Fizz"
print 37
print 38
print "Fizz"
print "Buzz"
print 41
print "Fizz"
print 43
print 44
print "FizzBuzz"
print 46
print 47
print "Fizz"
print 49
print "Buzz"
print "Fizz"
print 52
print 53
print "Fizz"
print "Buzz"
print 56
print "Fizz"
print 58
print 59
print "FizzBuzz"
print 61
print 62
print "Fizz"
print 64
print "Buzz"
print "Fizz"
print 67
print 68
print "Fizz"
print "Buzz"
print 71
print "Fizz"
print 73
print 74
print "FizzBuzz"
print 76
print 77
print "Fizz"
print 79
print "Buzz"
print "Fizz"
print 82
print 83
print "Fizz"
print "Buzz"
print 86
print "Fizz"
print 88
print 89
print "FizzBuzz"
print 91
print 92
print "Fizz"
print 94
print "Buzz"
print "Fizz"
print 97
print 98
print "Fizz"
print "Buzz"
``````

## main7

I tried to make only one line of print statement.

``````def main7(start,stop):
print "1\n2\nFizz\n4\nBuzz\nFizz\n7\n8\nFizz\nBuzz\n11\nFizz\n13\n14\nFizzBuzz\n16\n17\nFizz\n19\nBuzz\nFizz\n22\n23\nFizz\nBuzz\n26\nFizz\n28\n29\nFizzBuzz\n31\n32\nFizz\n34\nBuzz\nFizz\n37\n38\nFizz\nBuzz\n41\nFizz\n43\n44\nFizzBuzz\n46\n47\nFizz\n49\nBuzz\nFizz\n52\n53\nFizz\nBuzz\n56\nFizz\n58\n59\nFizzBuzz\n61\n62\nFizz\n64\nBuzz\nFizz\n67\n68\nFizz\nBuzz\n71\nFizz\n73\n74\nFizzBuzz\n76\n77\nFizz\n79\nBuzz\nFizz\n82\n83\nFizz\nBuzz\n86\nFizz\n88\n89\nFizzBuzz\n91\n92\nFizz\n94\nBuzz\nFizz\n97\n98\nFizz\nBuzz"
``````

## result

main5 became the fastest. After all it seems that it takes time to print.

``````main1 2.381
main2 1.380
main3 2.006
main4 1.723
main5 0.333
main6 1.124
main7 0.336
``````

## Supplement

The function that created main6 ().

``````def sub6(start,stop):
filename = 'fast_fizzbuzz2.py'

fizzbuzz = 'def main1():\n'
for i in range(start,stop):
fizzbuzz += '  print '
if (i%3) == 0:
if (i%5) == 0:
fizzbuzz += '"FizzBuzz"\n'
else:
fizzbuzz += '"Fizz"\n'
else:
if (i%5) == 0:
fizzbuzz += '"Buzz"\n'
else:
fizzbuzz += str(i) + "\n"
f = open(filename,'a' )
f.write(fizzbuzz)
f.close()
``````

The function that created main7 ()

``````def sub7(start,stop):
filename = 'fast_fizzbuzz2.py'

fizzbuzz = 'def main2():\n'
fizzbuzz += '  print "'
for i in range(start,stop):
if (i%3) == 0:
if (i%5) == 0:
fizzbuzz += r'FizzBuzz\n'
else:
fizzbuzz += r'Fizz\n'
else:
if (i%5) == 0:
fizzbuzz += r'Buzz\n'
else:
fizzbuzz += r'%s\n' % str(i)
fizzbuzz += '"'
f = open(filename,'a')
f.write(fizzbuzz)
f.close()
``````