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

I call clojure.core / time, which is documented as "Calculates expr and prints the time it takes. Returns the value of expr"

For instance:

(time (expensive)) 

Macro expansion shows that it stores the value as let so after printing the time that it should immediately return with the value in the let expression.

When I make a call, but with an expensive calculation, I see a delay, and then return the time, but then I must wait a considerable time (sometimes +10 seconds or more) for the result to appear.

Can anyone explain what is going on here?

PS: clojure 1.3.0 is used, if that matters.

+4
source share
1 answer

Perhaps you are returning something lazy from which elements are only created when submitted to the REPL? In this case, you can wrap it in dorun , which forces all elements to be created.

If you could provide detailed information about your expensive calculations, we could make sure that this is true.

Useful addition from Savanni D'Gerinel's comment:

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

+6
source

Source: https://habr.com/ru/post/1385339/


All Articles