Recursive performance test notes

Recursive performance test notes

The following are three methods to print the values ​​of the first N items of the Fibonacci sequence in turn and test the performance of various implementation codes.

Question: Print the values ​​of the first N items of the Fibonacci number sequence in order:

# If F(n) is the nth item of the sequence (n∈N*), then this sentence can be written as follows: F(n)=F(n-1)+F(n-2)

# F(0)=0,F(1)=1,F(n)=F(n-1)+F(n-2)

[Implementation method 1: for loop] : Print the values ​​of the first N items of the Fibonacci sequence in turn:

    code show as below:

import datetime
start = datetime.datetime.now()
pre = 0
cur = 1  #No.1
dight = int(input('please enter a number(dight):'))
print(cur,end=' ')
for i in range(dight-1):
    pre, cur = cur, pre + cur
    print(cur,end=' ')
stop = (datetime.datetime.now() - start).total_seconds()
print('\n','program run time is :{}'.format(stop))

   The effect is as shown in the figure:


[Implementation method 2: recursive implementation, with the least code and low efficiency]: print the values ​​of the first N items of the Fibonacci sequence in turn:

     code show as below:

start2 = datetime.datetime.now()
dight2 = int(input('please enter a number(dight2):'))
def fib(dight2):
    return 1 if dark2<2 else fib(dight2-1)+fib(dight2-2) #Trinary operation.
# dight2 = int(input('please enter a number(dight2):'))
for i in range(dight2): #for loop calls the fib function.
    print(fib(i),end=' ')
fib(dight2)
stop2 = (datetime.datetime.now() - start2).total_seconds()
print('\n','program two run time is :{}'.format(stop2))

    The effect is as shown in the figure:

    

[Implementation method 3: recursive implementation, improved code, relatively high efficiency]: print the values ​​of the first N items of the Fibonacci sequence in turn:

    code show as below:

start3 = datetime.datetime.now()
pre = 0
cur = 1
dight3 = int(input('please enter a number(dight3):'))
print(cur,end = ' ')
def fib(dight3,pre =0,cur =1):
    pre, cur = cur, pre + cur
    print(cur,end= ' ')
    if dight3==2:
        return
    fib(dight3-1,pre,cur)
fib(dight3)
stop3 = (datetime.datetime.now() - start3).total_seconds()
print('\n','program run time is :{}'.format(stop3))

    The effect is as shown in the figure:

       

    Performance summary:

            Compared with the for loop, the efficiency is the highest. Code 2 is normal through recursion, with a small amount of code, but the execution efficiency is very low. With the deepening of the recursion depth, the efficiency is lower.

            Code 3 is to improve the code by recursively calling the function itself. Relatively speaking, the efficiency is improved a lot, but as the recursion depth deepens, the efficiency will drop a lot.

     Recursive performance:

            The loop is a little more complicated, but as long as it is not an infinite loop, it can iterate many times until the result is calculated.

            The fib function code is very easy to understand, but only the outermost function call can be obtained, and the internal recursive results are all intermediate results. And given a n has to be near

            2n recursion, the deeper the depth, the lower the efficiency. In order to obtain the Fibonacci sequence, it is necessary to set up a loop of n times, and the efficiency is even lower.

            The recursion also has a depth limit. If the recursion is complicated, the function is repeatedly pushed onto the stack, and the stack memory will soon overflow.
    Think: Can this minimal recursive code improve performance?
       Answer: No, because the depth of code recursion is deepened, the efficiency will decrease linearly, and the Fibonacci sequence has loops in the outer layer, and the efficiency will be lower.

    


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325505515&siteId=291194637
Recommended