Name scope for memoization Fibonacci [closed]
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
1
down vote
favorite
Here's a snippet of code I use to examine the functionality of Python decorator(@memorize
). An example of Fibonacci computation:
def memorize(f):
memo =
def helper(*args):
if args not in memo:
memo[args] = f(*args)
return memo[args]
return helper
def fib(n):
if n==0:
return 0
elif n==1:
return 1
else:
return fib(n-1) + fib(n-2)
Here's the problem:
Different naming is causing a huge speed difference, why is it?
Execution 1:
s = time.time()
fib = memorize(fib)
a = fib(40)
e = time.time()
print(a)
print(e-s)returns
102334155
7.319450378417969e-05
Execution 2:
s = time.time()
memo_fib = memorize(fib)
a = memo_fib(40)
e = time.time()
print(a)
print(e-s)returns
102334155
46.79982662200928
Edited:
A screen copy from running the code
Edited:
Unless running the two executions seperately, to obtain aforementioned results, "Execution 2" must be run before "Execution 1".
python performance python-3.x fibonacci-sequence
closed as off-topic by ÃÂìýÃÂñ á¿¥Ã栨Â, pacmaninbw, t3chb0t, Sam Onela, Peilonrayz Jan 7 at 5:43
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions containing broken code or asking for advice about code not yet written are off-topic, as the code is not ready for review. After the question has been edited to contain working code, we will consider reopening it." â ÃÂìýÃÂñ á¿¥Ã栨Â, pacmaninbw, t3chb0t, Sam Onela, Peilonrayz
add a comment |Â
up vote
1
down vote
favorite
Here's a snippet of code I use to examine the functionality of Python decorator(@memorize
). An example of Fibonacci computation:
def memorize(f):
memo =
def helper(*args):
if args not in memo:
memo[args] = f(*args)
return memo[args]
return helper
def fib(n):
if n==0:
return 0
elif n==1:
return 1
else:
return fib(n-1) + fib(n-2)
Here's the problem:
Different naming is causing a huge speed difference, why is it?
Execution 1:
s = time.time()
fib = memorize(fib)
a = fib(40)
e = time.time()
print(a)
print(e-s)returns
102334155
7.319450378417969e-05
Execution 2:
s = time.time()
memo_fib = memorize(fib)
a = memo_fib(40)
e = time.time()
print(a)
print(e-s)returns
102334155
46.79982662200928
Edited:
A screen copy from running the code
Edited:
Unless running the two executions seperately, to obtain aforementioned results, "Execution 2" must be run before "Execution 1".
python performance python-3.x fibonacci-sequence
closed as off-topic by ÃÂìýÃÂñ á¿¥Ã栨Â, pacmaninbw, t3chb0t, Sam Onela, Peilonrayz Jan 7 at 5:43
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions containing broken code or asking for advice about code not yet written are off-topic, as the code is not ready for review. After the question has been edited to contain working code, we will consider reopening it." â ÃÂìýÃÂñ á¿¥Ã栨Â, pacmaninbw, t3chb0t, Sam Onela, Peilonrayz
1
I can't reproduce your reported timing results. Can you double-check your work, please?
â Gareth Rees
Jan 6 at 8:26
Got it double checked and attached with a screenshot, still couldn't figure out where it goes wrong. @GarethRees
â Logan
Jan 6 at 9:51
2
Aha, you have to do execution 2 before execution 1. That wasn't clear from your original post.
â Gareth Rees
Jan 6 at 11:28
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
Here's a snippet of code I use to examine the functionality of Python decorator(@memorize
). An example of Fibonacci computation:
def memorize(f):
memo =
def helper(*args):
if args not in memo:
memo[args] = f(*args)
return memo[args]
return helper
def fib(n):
if n==0:
return 0
elif n==1:
return 1
else:
return fib(n-1) + fib(n-2)
Here's the problem:
Different naming is causing a huge speed difference, why is it?
Execution 1:
s = time.time()
fib = memorize(fib)
a = fib(40)
e = time.time()
print(a)
print(e-s)returns
102334155
7.319450378417969e-05
Execution 2:
s = time.time()
memo_fib = memorize(fib)
a = memo_fib(40)
e = time.time()
print(a)
print(e-s)returns
102334155
46.79982662200928
Edited:
A screen copy from running the code
Edited:
Unless running the two executions seperately, to obtain aforementioned results, "Execution 2" must be run before "Execution 1".
python performance python-3.x fibonacci-sequence
Here's a snippet of code I use to examine the functionality of Python decorator(@memorize
). An example of Fibonacci computation:
def memorize(f):
memo =
def helper(*args):
if args not in memo:
memo[args] = f(*args)
return memo[args]
return helper
def fib(n):
if n==0:
return 0
elif n==1:
return 1
else:
return fib(n-1) + fib(n-2)
Here's the problem:
Different naming is causing a huge speed difference, why is it?
Execution 1:
s = time.time()
fib = memorize(fib)
a = fib(40)
e = time.time()
print(a)
print(e-s)returns
102334155
7.319450378417969e-05
Execution 2:
s = time.time()
memo_fib = memorize(fib)
a = memo_fib(40)
e = time.time()
print(a)
print(e-s)returns
102334155
46.79982662200928
Edited:
A screen copy from running the code
Edited:
Unless running the two executions seperately, to obtain aforementioned results, "Execution 2" must be run before "Execution 1".
python performance python-3.x fibonacci-sequence
edited Jan 6 at 23:42
asked Jan 6 at 6:05
Logan
1877
1877
closed as off-topic by ÃÂìýÃÂñ á¿¥Ã栨Â, pacmaninbw, t3chb0t, Sam Onela, Peilonrayz Jan 7 at 5:43
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions containing broken code or asking for advice about code not yet written are off-topic, as the code is not ready for review. After the question has been edited to contain working code, we will consider reopening it." â ÃÂìýÃÂñ á¿¥Ã栨Â, pacmaninbw, t3chb0t, Sam Onela, Peilonrayz
closed as off-topic by ÃÂìýÃÂñ á¿¥Ã栨Â, pacmaninbw, t3chb0t, Sam Onela, Peilonrayz Jan 7 at 5:43
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions containing broken code or asking for advice about code not yet written are off-topic, as the code is not ready for review. After the question has been edited to contain working code, we will consider reopening it." â ÃÂìýÃÂñ á¿¥Ã栨Â, pacmaninbw, t3chb0t, Sam Onela, Peilonrayz
1
I can't reproduce your reported timing results. Can you double-check your work, please?
â Gareth Rees
Jan 6 at 8:26
Got it double checked and attached with a screenshot, still couldn't figure out where it goes wrong. @GarethRees
â Logan
Jan 6 at 9:51
2
Aha, you have to do execution 2 before execution 1. That wasn't clear from your original post.
â Gareth Rees
Jan 6 at 11:28
add a comment |Â
1
I can't reproduce your reported timing results. Can you double-check your work, please?
â Gareth Rees
Jan 6 at 8:26
Got it double checked and attached with a screenshot, still couldn't figure out where it goes wrong. @GarethRees
â Logan
Jan 6 at 9:51
2
Aha, you have to do execution 2 before execution 1. That wasn't clear from your original post.
â Gareth Rees
Jan 6 at 11:28
1
1
I can't reproduce your reported timing results. Can you double-check your work, please?
â Gareth Rees
Jan 6 at 8:26
I can't reproduce your reported timing results. Can you double-check your work, please?
â Gareth Rees
Jan 6 at 8:26
Got it double checked and attached with a screenshot, still couldn't figure out where it goes wrong. @GarethRees
â Logan
Jan 6 at 9:51
Got it double checked and attached with a screenshot, still couldn't figure out where it goes wrong. @GarethRees
â Logan
Jan 6 at 9:51
2
2
Aha, you have to do execution 2 before execution 1. That wasn't clear from your original post.
â Gareth Rees
Jan 6 at 11:28
Aha, you have to do execution 2 before execution 1. That wasn't clear from your original post.
â Gareth Rees
Jan 6 at 11:28
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
fib
works by recursively calling the function named fib
:
def fib(n):
if n==0:
return 0
elif n==1:
return 1
else:
return fib(n-1) + fib(n-2)
In your "execution 1" the function named fib
is the memoized version of the function, because you have assigned it like this:
fib = memorize(fib)
But (assuming that you haven't run "execution 1" yet), in your "execution 2" the function named fib
is the original function (not the memoized version of the function, which you have assigned to memo_fib
), so when you call memo_fib
it calls the original fib
and when that recurses it calls the original fib
, bypassing the memoization.
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
fib
works by recursively calling the function named fib
:
def fib(n):
if n==0:
return 0
elif n==1:
return 1
else:
return fib(n-1) + fib(n-2)
In your "execution 1" the function named fib
is the memoized version of the function, because you have assigned it like this:
fib = memorize(fib)
But (assuming that you haven't run "execution 1" yet), in your "execution 2" the function named fib
is the original function (not the memoized version of the function, which you have assigned to memo_fib
), so when you call memo_fib
it calls the original fib
and when that recurses it calls the original fib
, bypassing the memoization.
add a comment |Â
up vote
2
down vote
accepted
fib
works by recursively calling the function named fib
:
def fib(n):
if n==0:
return 0
elif n==1:
return 1
else:
return fib(n-1) + fib(n-2)
In your "execution 1" the function named fib
is the memoized version of the function, because you have assigned it like this:
fib = memorize(fib)
But (assuming that you haven't run "execution 1" yet), in your "execution 2" the function named fib
is the original function (not the memoized version of the function, which you have assigned to memo_fib
), so when you call memo_fib
it calls the original fib
and when that recurses it calls the original fib
, bypassing the memoization.
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
fib
works by recursively calling the function named fib
:
def fib(n):
if n==0:
return 0
elif n==1:
return 1
else:
return fib(n-1) + fib(n-2)
In your "execution 1" the function named fib
is the memoized version of the function, because you have assigned it like this:
fib = memorize(fib)
But (assuming that you haven't run "execution 1" yet), in your "execution 2" the function named fib
is the original function (not the memoized version of the function, which you have assigned to memo_fib
), so when you call memo_fib
it calls the original fib
and when that recurses it calls the original fib
, bypassing the memoization.
fib
works by recursively calling the function named fib
:
def fib(n):
if n==0:
return 0
elif n==1:
return 1
else:
return fib(n-1) + fib(n-2)
In your "execution 1" the function named fib
is the memoized version of the function, because you have assigned it like this:
fib = memorize(fib)
But (assuming that you haven't run "execution 1" yet), in your "execution 2" the function named fib
is the original function (not the memoized version of the function, which you have assigned to memo_fib
), so when you call memo_fib
it calls the original fib
and when that recurses it calls the original fib
, bypassing the memoization.
answered Jan 6 at 11:33
Gareth Rees
41.2k394168
41.2k394168
add a comment |Â
add a comment |Â
1
I can't reproduce your reported timing results. Can you double-check your work, please?
â Gareth Rees
Jan 6 at 8:26
Got it double checked and attached with a screenshot, still couldn't figure out where it goes wrong. @GarethRees
â Logan
Jan 6 at 9:51
2
Aha, you have to do execution 2 before execution 1. That wasn't clear from your original post.
â Gareth Rees
Jan 6 at 11:28