简体   繁体   中英

Timing function in Racket

The syntax for the timing function in Scheme is confusing. 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? 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?

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. 1 You can just add it as the last expression in the body of your time giving you:

(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. But for many cases a single call to void should be fine.

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