Name scope for memoization Fibonacci [closed]

The name of the pictureThe name of the pictureThe name of the pictureClash 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
enter image description here



Edited:



Unless running the two executions seperately, to obtain aforementioned results, "Execution 2" must be run before "Execution 1".







share|improve this question













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
If this question can be reworded to fit the rules in the help center, please edit the question.








  • 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

















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
enter image description here



Edited:



Unless running the two executions seperately, to obtain aforementioned results, "Execution 2" must be run before "Execution 1".







share|improve this question













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
If this question can be reworded to fit the rules in the help center, please edit the question.








  • 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













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
enter image description here



Edited:



Unless running the two executions seperately, to obtain aforementioned results, "Execution 2" must be run before "Execution 1".







share|improve this question













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
enter image description here



Edited:



Unless running the two executions seperately, to obtain aforementioned results, "Execution 2" must be run before "Execution 1".









share|improve this question












share|improve this question




share|improve this question








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
If this question can be reworded to fit the rules in the help center, please edit the question.




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
If this question can be reworded to fit the rules in the help center, please edit the question.







  • 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




    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











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.






share|improve this answer




























    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.






    share|improve this answer

























      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.






      share|improve this answer























        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.






        share|improve this answer













        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.







        share|improve this answer













        share|improve this answer



        share|improve this answer











        answered Jan 6 at 11:33









        Gareth Rees

        41.2k394168




        41.2k394168












            Popular posts from this blog

            Greedy Best First Search implementation in Rust

            Function to Return a JSON Like Objects Using VBA Collections and Arrays

            C++11 CLH Lock Implementation