PLT 方案中的时间码

发布于 2024-07-08 22:41:47 字数 437 浏览 8 评论 0原文

我想看看一个函数运行需要多长时间。 在 PLT-Scheme 中执行此操作的最简单方法是什么? 理想情况下,我希望能够做这样的事情:

> (define (loopy times)
  (if (zero? times)
      0
      (loopy (sub1 times)))) 
> (loopy 5000000)
0                      ;(after about a second)
> (timed (loopy 5000000))
Took: 0.93 seconds
0
> 

如果我必须使用其他语法,例如 (timed Loopy 5000000)(timed '( Loopy 5000000)),或者如果它返回 cons 或其他内容所花费的时间。

I want to see how long a function takes to run. What's the easiest way to do this in PLT-Scheme? Ideally I'd want to be able to do something like this:

> (define (loopy times)
  (if (zero? times)
      0
      (loopy (sub1 times)))) 
> (loopy 5000000)
0                      ;(after about a second)
> (timed (loopy 5000000))
Took: 0.93 seconds
0
> 

It doesn't matter if I'd have to use some other syntax like (timed loopy 5000000) or (timed '(loopy 5000000)), or if it returns the time taken in a cons or something.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吻风 2024-07-15 22:41:47

在大多数Scheme 实现中计时表达式执行的标准名称是“时间”。 这是 DrRacket 内部的一个示例。

(定义(循环次数)
(如果(零?次)
0
(循环(sub1次))))

(时间(循环 5000000))
cpu时间:1526 实时:1657 gc时间:0
0

如果您花时间对不同的实现进行相互比较,
记住从命令行使用racket而不是直接进行基准测试
在 DrRacket 中(DrRacket 插入调试代码以便提供更好的错误消息)。

The standard name for timing the execution of expressions in most Scheme implementations is "time". Here is an example from within DrRacket.

(define (loopy times)
(if (zero? times)
0
(loopy (sub1 times))))

(time (loopy 5000000))
cpu time: 1526 real time: 1657 gc time: 0
0

If you use time to benchmark different implementations against each other,
remember to use racket from the command line rather than benchmarking directly
in DrRacket (DrRacket inserts debug code in order to give better error messages).

七度光 2024-07-15 22:41:47

找到它...

来自 在线文档

  • (time-apply proc arg-list) 使用 arg-list 中的参数调用过程 proc。 返回四个值:包含应用程序结果的列表、获得该结果所需的 CPU 时间的毫秒数、结果所需的“真实”毫秒数以及用于垃圾回收的 CPU 时间(包含在第一个结果中)。

用法示例:

> (time-apply loopy '(5000000))
(0)
621
887
0

Found it...

From the online documentation:

  • (time-apply proc arg-list) invokes the procedure proc with the arguments in arg-list. Four values are returned: a list containing the result(s) of applying proc, the number of milliseconds of CPU time required to obtain this result, the number of ``real'' milliseconds required for the result, and the number of milliseconds of CPU time (included in the first result) spent on garbage collection.

Example usage:

> (time-apply loopy '(5000000))
(0)
621
887
0
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文