Re: What make function with huge list so slow

2019-08-25 Thread Windson Yang
> Figure out how much memory fib_dp is holding on to right before it returns > the answer. fib(40) is a _big_ number! And so is fib(39), and > fib(38), and fib(37), etc. By the time you're done, you're holding > on to quite a huge pile of storage here. Depending on how much phys

Re: What make function with huge list so slow

2019-08-25 Thread Alan Bawden
Windson Yang writes: > 'I'm just running them in succession and seeing how long they'. The full > code looks like this, this is only an example.py here. and I run 'time > python3 example.py' for each function. > > def fib_dp(n): > dp = [0] * (n+1) > if n <= 1: > r

Re: What make function with huge list so slow

2019-08-24 Thread Windson Yang
'I'm just running them in succession and seeing how long they'. The full code looks like this, this is only an example.py here. and I run 'time python3 example.py' for each function. def fib_dp(n): dp = [0] * (n+1) if n <= 1: return n dp[0], dp[1] = 0, 1

Re: What make function with huge list so slow

2019-08-24 Thread Chris Angelico
On Sun, Aug 25, 2019 at 1:43 PM Windson Yang wrote: > > Thank you, Chris. I tried your suggestions. I don't think that is the reason, > fib_dp_look() and fib_dp_set() which also allocation a big list can return in > 2s. (Please don't top-post) Are you running each function more than once, or j

Re: What make function with huge list so slow

2019-08-24 Thread Windson Yang
Thank you, Chris. I tried your suggestions. I don't think that is the reason, fib_dp_look() and fib_dp_set() which also allocation a big list can return in 2s. Chris Angelico 于2019年8月25日周日 上午11:27写道: > On Sun, Aug 25, 2019 at 12:56 PM Windson Yang wrote: > > > > I have two functions to calculat

Re: What make function with huge list so slow

2019-08-24 Thread Chris Angelico
On Sun, Aug 25, 2019 at 12:56 PM Windson Yang wrote: > > I have two functions to calculate Fibonacci numbers. fib_dp use a list to > store the calculated number. fib_dp2 just use two variables. > > def fib_dp(n): > if n <= 1: > return n > dp = [0] * (n+1) >

What make function with huge list so slow

2019-08-24 Thread Windson Yang
I have two functions to calculate Fibonacci numbers. fib_dp use a list to store the calculated number. fib_dp2 just use two variables. def fib_dp(n): if n <= 1: return n dp = [0] * (n+1) dp[0], dp[1] = 0, 1 for i in range(2, n+1): dp[i] =