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()

Recommended Posts

In search of the fastest FizzBuzz in Python
Review of the basics of Python (FizzBuzz)
FizzBuzz in Python
Google search for the last line of the file in Python
Check the behavior of destructor in Python
The result of installing python in Anaconda
The basics of running NoxPlayer in Python
Binary search in Python
the zen of Python
Linear search in Python
Binary search in Python (binary search)
Output the number of CPU cores in Python
[Python] Sort the list of pathlib.Path in natural sort
Get the caller of a function in Python
Match the distribution of each group in Python
View the result of geometry processing in Python
Make a copy of the list in Python
Find the divisor of the value entered in python
Find the solution of the nth-order equation in python
About the behavior of Model.get_or_create () of peewee in Python
Solving the equation of motion in Python (odeint)
Output in the form of a python array
Search by the value of the instance in the list
[Python] Explore the characteristics of the titles of the top sites in Google search results
Experience the good calculation efficiency of vectorization in Python
Towards the retirement of Python2
Fizzbuzz in Python (in one line)
How to get the number of digits in Python
Download the file in Python
In search of the best random dot stereogram (RDS).
Find the difference in Python
The story of building the fastest Linux environment in the world
About the ease of Python
Equivalence of objects in Python
Binary search in Python / C ++
Algorithm in Python (binary search)
Learn the design pattern "Chain of Responsibility" in Python
Implement the solution of Riccati algebraic equations in Python
Reproduce the execution example of Chapter 4 of Hajipata in Python
Let's use the open data of "Mamebus" in Python
Implemented the algorithm of "Algorithm Picture Book" in Python3 (Heapsort)
[Python] Outputs all combinations of elements in the list
Implementation of quicksort in Python
Get the URL of the HTTP redirect destination in Python
A reminder about the implementation of recommendations in Python
About the features of Python
Reproduce the execution example of Chapter 5 of Hajipata in Python
To do the equivalent of Ruby's ObjectSpace._id2ref in Python
Check the asymptotic nature of the probability distribution in Python
The Power of Pandas: Python
Try scraping the data of COVID-19 in Tokyo with Python
Find out the apparent width of a string in python
I tried the accuracy of three Stirling's approximations in python
Measure the execution result of the program in C ++, Java, Python.
Check the operation of Python for .NET in each environment
[Memo] The mystery of cumulative assignment statements in Python functions
Calculate the square root of 2 in millions of digits with python
Have the equation graph of the linear function drawn in Python
Implemented the algorithm of "Algorithm Picture Book" in Python3 (Bubble Sort)
Get the number of specific elements in a python list
Python --Find out number of groups in the regex expression