简体   繁体   English

球拍中的计时功能

[英]Timing function in Racket

The syntax for the timing function in Scheme is confusing. Scheme中计时功能的语法令人困惑。 If I want to check CPU cycles I should be able to wrap my entire code in (time codestuff) and it should produce that should it not? 如果我想检查CPU周期,我应该可以将我的整个代码包装在(时间码)中,它应该产生这样的代码吗? I get an error whenever I try to wrap it in (time). 每当我尝试将其包装在(时间)中时,都会收到错误消息。

lambda: no expression after a sequence of internal definitions in: lambda

What other choices do I have to check CPU usage for running my program in Scheme? 在Scheme中运行程序时,我还需要检查CPU使用率的哪些其他选择?

Unfortunately, time doesn't seem to work if the last form in the time body isn't an expression. 不幸的是,如果时间主体中的最后一个形式不是表达式,那么时间似乎不起作用。 Fortunately, (void) is an expression that is (relatively) free. 幸运的是, (void)是一个(相对)自由的表达式。 1 You can just add it as the last expression in the body of your time giving you: 1您可以将其添加为时间的最后一个表达式,从而为您提供:

(time
  .... your code here ....
  (void))

So for example, the following code will give you a result like this: 因此,例如,以下代码将为您提供如下结果:

> (time
    (define x 
      (for/list ([i (in-range 1000000)])
        i))
    (void))
cpu time: 148 real time: 149 gc time: 124

1 As with anything in CS and timing, whether or not void is going to matter here does depend. 1与CS和时间安排中的任何内容一样,此处是否要考虑空缺也取决于。 But for many cases a single call to void should be fine. 但在许多情况下,单次调用void应该没问题。

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

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