简体   繁体   中英

Do I need to do something special to use clojure.core/time?

I'm calling clojure.core/time which is documented as "Evaluates expr and prints the time it took. Returns the value of expr"

Eg:

(time (expensive))

Macroexpanding it shows that it does store the value as a let so after outputting the time it should return immediately with the value in the let expression.

When I make the call however with an expensive computation, I see a delay and then get the time back, but then have to wait for a significant time (sometimes +10 seconds or more) for the result to appear.

Can anyone explain what's going on here?

PS: This is using clojure 1.3.0 if that makes any difference.

Maybe you are returning something lazy, of which the elements are only produced when fed to the REPL? In that case, you might want to wrap it in a dorun which forces all elements to be produced.

If you could provide the details of your expensive computation, we could see if this is true.

Useful addition from Savanni D'Gerinel's comment:

The proper syntax is probably (time (doall (computation))) if you want to return the result and (time (dorun (computation))) if you do not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM