简体   繁体   English

我需要做一些特别的事情才能使用clojure.core / time吗?

[英]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" 我正在调用clojure.core / time,记录为“Evaluates expr并打印它所花费的时间。返回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. 宏展开它表明它确实将值存储为let,因此在输出时应该立即返回let表达式中的值。

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. 然而,当我使用昂贵的计算进行呼叫时,我看到延迟然后得到时间,但是必须等待很长时间(有时+10秒或更长时间)才能显示结果。

Can anyone explain what's going on here? 谁能解释一下这里发生了什么?

PS: This is using clojure 1.3.0 if that makes any difference. PS:这是使用clojure 1.3.0,如果这有任何区别。

Maybe you are returning something lazy, of which the elements are only produced when fed to the REPL? 也许你正在返回一些懒惰的东西,其中的元素仅在送入REPL时生成? In that case, you might want to wrap it in a dorun which forces all elements to be produced. 在这种情况下,您可能希望将其包装在一个迫使所有元素生成的dorun中。

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: 来自Savanni D'Gerinel评论的有用补充:

The proper syntax is probably (time (doall (computation))) if you want to return the result and (time (dorun (computation))) if you do not. 正确的语法可能是(time (doall (computation)))如果你想返回结果和(time (dorun (computation)))如果你不想。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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