I realize that this is my fault, I misunderstood the `memoize` and I search 
the mail list and find this issue already solved.
Sorry to raise this issue again.
The right way to use memoization with recursion is to use the "memoized" 
method in stead of call the raw "unmemoized" method.
@Paulo Sérgio Almeida 's solution just the thing i wanted.
BTW, I found the lazy way is for effective then TCO, the loop-recur I mean, 
and memoization and using build-in lazy funciton perform better.

On Saturday, April 13, 2013 2:48:45 PM UTC+8, Leonardo Borges wrote:
>
> or you can also store it in a var: 
>
> (defn fib [n] 
>    (if  (or (zero? n) (= n 1)) 
>        1 
>        (+  (fib (dec n) )  (fib (- n 2))))) 
>
> (time (fib 30)) ;; "Elapsed time: 265.472 msecs" 
>
> (def fib-memo (memoize fib)) 
>
> (time (fib-memo 30)) ;; "Elapsed time: 222.122 msecs" 
>
> (time (fib-memo 30)) ;; "Elapsed time: 0.058 msecs" 
>
> (time (fib-memo 30)) ;; "Elapsed time: 0.042 msecs" 
> Leonardo Borges 
> www.leonardoborges.com 
>
>
> On Sat, Apr 13, 2013 at 4:46 PM, Cedric Greevey 
> <cgre...@gmail.com<javascript:>> 
> wrote: 
> > To get the benefit of memoization, you need to store the memoized 
> function 
> > and use it again. (memoize fib-nocur) returns a function that contains 
> an 
> > internal memory of past results. If you call (memoize fib-nocur) again 
> you 
> > get a second function with an (empty!) internal memory. If you don't use 
> the 
> > first one more than once you don't see any benefit from its memory. 
> > 
> > So, try this: 
> > 
> > (let [f (memoize fib-nocur)] 
> >   (time (f 30)) 
> >   (time (f 30)) 
> >   (time (f 30))) 
> > 
> > and see if the second and third times are much shorter than the first 
> one. 
> > 
> > 
> > On Sat, Apr 13, 2013 at 12:52 AM, Liao Pengyu 
> > <aris...@gmail.com<javascript:>> 
> wrote: 
> >> 
> >> Hi, there. I have a question about the memoization in clojure. 
> >> I compare two functions to test the performance improvement of 
> >> memoization: 
> >> (defn fib [n] 
> >>    (if  (or (zero? n) (= n 1)) 
> >>        1 
> >>       (+  (fib (dec n) )  (fib (- n 2))))) 
> >> 
> >> (time (fib 30)) 
> >> get the result: 
> >> "Elapsed time: 316.65 msecs" 
> >> 1346269 
> >> 
> >> And then test for memoization: 
> >> user> (time ((memoize fib-nocur) 30)) 
> >> "Elapsed time: 308.729 msecs" 
> >> 1346269 
> >> user> (time ((memoize fib-nocur) 30)) 
> >> "Elapsed time: 314.942 msecs" 
> >> 1346269 
> >> user> (time ((memoize fib-nocur) 30)) 
> >> "Elapsed time: 308.657 msecs" 
> >> 1346269 
> >> 
> >> Seems no effect. Since I just test it in nrepl and have no experience 
> >> about using clojure in project, I wander was the memoization really 
> works? 
> >> Look forward to your responses 
> >> 
> >> -- 
> >> -- 
> >> You received this message because you are subscribed to the Google 
> >> Groups "Clojure" group. 
> >> To post to this group, send email to clo...@googlegroups.com<javascript:> 
> >> Note that posts from new members are moderated - please be patient with 
> >> your first post. 
> >> To unsubscribe from this group, send email to 
> >> clojure+u...@googlegroups.com <javascript:> 
> >> For more options, visit this group at 
> >> http://groups.google.com/group/clojure?hl=en 
> >> --- 
> >> You received this message because you are subscribed to the Google 
> Groups 
> >> "Clojure" group. 
> >> To unsubscribe from this group and stop receiving emails from it, send 
> an 
> >> email to clojure+u...@googlegroups.com <javascript:>. 
> >> For more options, visit https://groups.google.com/groups/opt_out. 
> >> 
> >> 
> > 
> > 
> > -- 
> > -- 
> > You received this message because you are subscribed to the Google 
> > Groups "Clojure" group. 
> > To post to this group, send email to clo...@googlegroups.com<javascript:> 
> > Note that posts from new members are moderated - please be patient with 
> your 
> > first post. 
> > To unsubscribe from this group, send email to 
> > clojure+u...@googlegroups.com <javascript:> 
> > For more options, visit this group at 
> > http://groups.google.com/group/clojure?hl=en 
> > --- 
> > You received this message because you are subscribed to the Google 
> Groups 
> > "Clojure" group. 
> > To unsubscribe from this group and stop receiving emails from it, send 
> an 
> > email to clojure+u...@googlegroups.com <javascript:>. 
> > For more options, visit https://groups.google.com/groups/opt_out. 
> > 
> > 
>

-- 
-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to