[PYTHON] I tried using eval (a, b) for Fibonacci, but it wasn't fast

Yes. It finishes a little earlier than the one on the reference site, but I'm not sure, but I have to leave a memo. Reference http://d.hatena.ne.jp/sakurai_youhei/20130107/1357568535

op.py


#!/usr/bin/env python
# -*- coding:utf-8 -*-
#from __future__ import print_function
import sys
import io
import re
import math

####Memory usage and operating time check preparation
from guppy import hpy
import time
start = time.clock()
h = hpy()
####Up to here
#It seems to be posted
def fib(n):
    if n < 2: return n
    return fib(n-2) + fib(n-1)

#print fib(50)

def fib2(n):
    a,b=0,1
    return reduce(lambda x,y: x+[x[-1]+x[-2],], xrange(n-1), [a,b])[n]
print fib2(10000)

def fib3(n):
    a,b=0,1
    for i in range(n):
        a,b=b,eval('a+b')
    print a

#print fib3(500000)

#def fib4(n):
#    n+=3
#    f=[(seq.append(seq[i-2] + seq[i-1]), seq[i-2])[1] for seq in [[0, 1]] #for
#            i in range(2, n)]
#    print f[-1]
#print fib4(500000)

def fib5(n):
    a,b=0,1
    for i in range(n):
        a,b=b,a+b
    print a

#print fib5(500000)


#if __name__ == 'main':
#    print(fib(100))


####Memory usage and operating time output
end = time.clock()
print (h.heap())
print (end - start)

fib (n) and fib2 (n) are the first, and fib3 (n) is the pattern I tried with eval. I'm not sure because eval is used because the calculation result is taken out differently. I don't know.

Something like a comparison result

function processing time F(3)The value of the
fib() 0.008658 seconds 2
fib2() 0.011694 seconds 2
fib3() 0.011055 seconds 2
function processing time F(10)The value of the
fib() 0.010526 seconds 55
fib2() 0.008879 seconds 55
fib3() 0.011909 seconds 55
function processing time F(35)The value of the
fib() 5.782845 seconds 9227465
fib2() 0.011936 seconds 9227465
fib3() 0.010569 seconds 9227465

Pattern 1 seems to take a lot of time. .. .. Well, that's it. There is no point in writing the calculation result, and since it will be a large number like a fool, I will write down only the processing speed after that.

function processing time F(50)The value of the
fib() 7997.16923 seconds -
fib2() 0.009155 seconds -
fib3() 0.010594 seconds -

You don't have to try pattern 1 anymore. .. .. ..

function processing time F(10000)The value of the
fib2() 0.376591 seconds -
fib3() 0.111379 seconds -
fib5() 0.012429 seconds -
function processing time F(100000)The value of the
fib2() 215.1977 92 seconds -
fib3() 1.23247 seconds -
fib5() 0.176561 seconds -
function processing time F(500000)The value of the
fib3() 10.064515 seconds -
fib5() 3.950023 seconds -

bonus ・ When n = 777777 fib5() Processing speed 9.299923

It seems that the format and comparison method may not be fair. Well, when I have to remember how to write some patterns and worry about the processing speed, I wonder which pattern is faster.

13/12/15 Yes. The measured values for a, b = b, a + b, which I received in the comments, have been added to the table in the column of fib5 ().

Recommended Posts

I tried using eval (a, b) for Fibonacci, but it wasn't fast
I tried using Tensorboard, a visualization tool for machine learning
When I tried to make a VPC with AWS CDK but couldn't make it
I tried to implement a recommendation system (content-based filtering)
What is a system call
I tried to make a system that fetches only deleted tweets
I tried to draw a system configuration diagram with Diagrams on Docker
I tried using eval (a, b) for Fibonacci, but it wasn't fast
I tried playing a ○ ✕ game using TensorFlow
I tried drawing a line using turtle
I tried using pipenv, so a memo
I tried using scrapy for the first time
vprof --I tried using the profiler for Python
I tried to use Java with Termux using Termux Arch but it didn't work
When I tried to make a VPC with AWS CDK but couldn't make it
I tried using Pythonect, a dataflow programming language.
I tried reading a CSV file using Python
I tried using firebase for Django's cache server
I thought it would be slow to use a for statement in NumPy, but that wasn't the case.
I tried using a database (sqlite3) with kivy
I tried to make a ○ ✕ game using TensorFlow
When I tried to create a project using Python on Docker with PyCharm, it didn't work, but it worked with Docker Compose.
I tried hosting a Pytorch sample model using TorchServe
I built a Wheel for Windows using Github Actions
[Python] I tried running a local server using flask
I tried drawing a pseudo fractal figure using Python
I tried reading data from a file using Node.js.
I tried a simple RPA for login with selenium
I tried using Python (3) instead of a scientific calculator
AI Gaming I tried it for the first time
PyTorch Learning Note 2 (I tried using a pre-trained model)
I tried to draw a configuration diagram using Diagrams
I tried using parameterized
I tried using argparse
I tried using mimesis
I tried using anytree
I tried using aiomysql
I tried using Summpy
I tried using coturn
I tried using Pipenv
I tried using matplotlib
I tried using "Anvil".
I tried using Hubot
I tried using ESPCN
I tried using openpyxl
I tried using Ipython
I tried using PyCaret
I tried using cron
I tried using ngrok
I tried using Jupyter
I tried using PyCaret
I tried using Heapq
I tried using doctest
I tried using folium
I tried using jinja2
I tried using folium
I tried using time-window
[Git] I tried to make it easier to understand how to use git stash using a concrete example
I tried hosting a TensorFlow deep learning model using TensorFlow Serving
Miscellaneous notes that I tried using python for the matter
I tried to create a bot for PES event notification
I tried to make a stopwatch using tkinter in python
I tried to make a simple text editor using PyQt
[For beginners] I tried using the Tensorflow Object Detection API
I want to specify a file that is not a character string for logrotate, but is it impossible?
[I tried using Pythonista 3] Introduction