PLT-Scheme 中的时间码 [英] Time Code in PLT-Scheme

查看:55
本文介绍了PLT-Scheme 中的时间码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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
> 

如果我必须使用诸如 (timed loopy 5000000)(timed '(loopy 5000000)) 之类的其他语法,或者如果它返回在缺点或其他东西中花费的时间.

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.

推荐答案

在大多数 Scheme 实现中,用于定时执行表达式的标准名称是时间".这是 DrRacket 中的一个示例.

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

(定义(循环时间)(如果(零?次)0(loopy (sub1 times))))

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

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

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

如果您使用时间来对不同的实现进行基准测试,记住从命令行使用球拍而不是直接进行基准测试在 DrRacket 中(DrRacket 插入调试代码是为了提供更好的错误信息).

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).

这篇关于PLT-Scheme 中的时间码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆