当前位置:网站首页>Recursive performance test
Recursive performance test
2022-07-24 08:27:00 【micro_ cloud_ fly】
Let's start with a familiar piece of code
package main
import (
"fmt"
"time"
)
func main() {
result := 0
start := time.Now()
for i := 1; i <= 40; i++ {
result = fibonacci(i)
fmt.Printf(" Sequence number %d position : %d\n", i, result)
}
end := time.Now()
delta := end.Sub(start)
fmt.Printf(" The execution time of the program is : %s\n", delta)
}
func fibonacci(n int) (res int) {
if n <= 2 {
res = 1
} else {
res = fibonacci(n-1) + fibonacci(n-2)
}
return
}
I feel that the implementation should be fast
But the implementation results are slow , as follows :
``shell
Sequence number 1 position : 1
Sequence number 2 position : 1
Sequence number 3 position : 2
Sequence number 4 position : 3
Sequence number 5 position : 5
Sequence number 6 position : 8
Sequence number 7 position : 13
Sequence number 8 position : 21
Sequence number 9 position : 34
Sequence number 10 position : 55
Sequence number 11 position : 89
Sequence number 12 position : 144
Sequence number 13 position : 233
Sequence number 14 position : 377
Sequence number 15 position : 610
Sequence number 16 position : 987
Sequence number 17 position : 1597
Sequence number 18 position : 2584
Sequence number 19 position : 4181
Sequence number 20 position : 6765
Sequence number 21 position : 10946
Sequence number 22 position : 17711
Sequence number 23 position : 28657
Sequence number 24 position : 46368
Sequence number 25 position : 75025
Sequence number 26 position : 121393
Sequence number 27 position : 196418
Sequence number 28 position : 317811
Sequence number 29 position : 514229
Sequence number 30 position : 832040
Sequence number 31 position : 1346269
Sequence number 32 position : 2178309
Sequence number 33 position : 3524578
Sequence number 34 position : 5702887
Sequence number 35 position : 9227465
Sequence number 36 position : 14930352
Sequence number 37 position : 24157817
Sequence number 38 position : 39088169
Sequence number 39 position : 63245986
Sequence number 40 position : 102334155
Sequence number 41 position : 165580141
Sequence number 42 position : 267914296
Sequence number 43 position : 433494437
Sequence number 44 position : 701408733
The execution time of the program is : 6.36509246s
When 40 Change it to 50 When , It's almost impossible to wait for the result
The improved code is as follows :
```go
package main
import (
"fmt"
"time"
)
const LIM = 41
var fibs [LIM]uint64
func main() {
var result uint64 = 0
start := time.Now()
for i := 1; i < LIM; i++ {
result = fibonacci(i)
fmt.Printf(" Sequence number %d position : %d\n", i, result)
}
end := time.Now()
delta := end.Sub(start)
fmt.Printf(" The execution time of the program is : %s\n", delta)
}
func fibonacci(n int) (res uint64) {
// Memorize : Check whether Fibonacci is known in the array (n)
if fibs[n] != 0 {
res = fibs[n]
return
}
if n <= 2 {
res = 1
} else {
res = fibonacci(n-1) + fibonacci(n-2)
}
fibs[n] = res
return
}
边栏推荐
- Read and understand move2earn project - move
- "Explanation" change exchange
- Assemble | find the maximum and minimum values
- Move protocol global health declaration, step into Web3 in sports
- Recognition and storage of Graphs
- JMX Console 未授权访问漏洞
- How to write your FAQ page?
- Kotlin学习笔记1——变量、函数
- Vidar-Team战队专访:AS WE DO, AS YOU KNOW.
- Dao race track is booming. What are the advantages of m-dao?
猜你喜欢

jmeter中JSON提取器使用

DGL库中一些函数或者方法的介绍
![[wechat applet development] (II) wechat native bottom tabbar configuration](/img/74/5f5da7ea47f95a25011ba52959f480.png)
[wechat applet development] (II) wechat native bottom tabbar configuration

Install SQL Server database

VIDAR team team exclusive interview: as we do, as you know

「题解」带分数

Will Plato become the risk target of the meta universe? Platofarm has great opportunities

"Problem solution" with score

Brief notes on the key points of distributed system principle introduction

FPGA integrated project - image edge detection system
随机推荐
Wei Xiaoli's "pursuer" is coming
In the next bull market, can platofarm, the leading project in the Web3 world, set foot on the top of the mountain
how to add square on screenshot
Mysql database advanced
Kotin fragment the correct way to get ViewModel instances
Vscode code style notes (vetur)
Private traffic + apps, new opportunities for e-commerce drainage
[Yum] configuration and use of Yum source
[MySQL] 08: aggregate function
Web3≠NFT? A digital Renaissance?
How difficult is it to build a digital collection platform?
Move protocol launched a beta version, and you can "0" participate in p2e
A knight's journey
33 introduction to sparksql, dataframe and dataset
Why does the metauniverse need NFT?
Is it safe to open an account online in Beijing
[ByteDance] ByteDance access (including login and payment)
[tools] a few lines of code can realize complex excel import and export tool classes, which is really strong!!!
MySQL日期格式化
[wechat applet development] (II) wechat native bottom tabbar configuration