R - 如何“计时”延迟以及用户输入

发布于 2024-09-24 02:24:10 字数 399 浏览 3 评论 0原文

在 R 中创建一个需要很少系统资源的计时器的好方法是什么?

到目前为止,我有一个简单的延迟:

t1=as.numeric(format(Sys.time(), "%s"));
t2=t1; 
while (t2-t1<5) t2=as.numeric(format(Sys.time(), "%s"));

以及相应的计时器:

t1=as.numeric(format(Sys.time(), "%s"));t2=t1;

[Event]

t2=as.numeric(format(Sys.time(), "%s"));
time=t2-t1;

感谢您的阅读。

What would be a good way to create a timer in R. One that would require little system resources.

So far I have a simple delay:

t1=as.numeric(format(Sys.time(), "%s"));
t2=t1; 
while (t2-t1<5) t2=as.numeric(format(Sys.time(), "%s"));

And the corresponding timer:

t1=as.numeric(format(Sys.time(), "%s"));t2=t1;

[Event]

t2=as.numeric(format(Sys.time(), "%s"));
time=t2-t1;

Thanks for reading.

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

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

发布评论

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

评论(3

悲喜皆因你 2024-10-01 02:24:10

如果您只想让 R 暂停并且在给定时间内不占用资源,则可以使用 Sys.sleep 函数。

如果你想要一些更复杂的东西,你可以让 R 做其他事情或其他交互,但在给定的延迟之后发生一些事情,那么最好选择 tcltk 路线。

If you just want to have R pause and not take up resources for a given amount of time then use the Sys.sleep function.

If you want something more sophisticated where you can have R doing other things or other interactions, but have something happen after a given delay then it will be better to go the tcltk route.

メ斷腸人バ 2024-10-01 02:24:10

对于初学者来说,放弃 as.numeric(format()) 业务 - 不这样做可以节省您的“系统资源”,而且无论如何您都可以计算日期/时间类型。其余代码可以保持原样。

否则,计时器通常会在其自己的事件循环中运行。鉴于 R 是单线程的,这很棘手,但如果我记得人们已经做到了,例如使用 R 中包含的 tcltk 包。

For starters, drop the as.numeric(format()) business -- not doing it saves you 'system resources' and you can compute on date / time types anyway. The rest of your code can remain as is.

Otherwise, a timer would commonly run in its own event loop. Given that R is single-threaded this is tricky but if I recall folks have done it, e.g. with the tcltk package included with R.

何以畏孤独 2024-10-01 02:24:10

您只是想要表达式的运行时吗?然后你可以使用 system.time,例如,

system.time(sum(1:1e5))

我是 tcltk 包的新手,但我想知道 Dirk 和 Greg 是否在谈论类似以下内容:

after <- function(ms, func) {
  library(tcltk)
  invisible(.Tcl(paste("after", ms, .Tcl.callback(func))))
}

示例:

> after(7000, function() cat("hi!\n"))
> cat("hello?\n")
hello?
hi!

来自 Revolution Analytics 的文档很有帮助:
http://www.inside-r.org/r-doc/tcltk/ tclRequire
Tcl“after”命令的说明:
http://www.astro.princeton.edu /~rhl/Tcl-Tk_docs/tcl/after.n.html

Did you just want the runtime for an expression? Then you can use system.time, eg,

system.time(sum(1:1e5))

I'm a newbie with the tcltk package, but I wonder if Dirk and Greg were talking about something like the following:

after <- function(ms, func) {
  library(tcltk)
  invisible(.Tcl(paste("after", ms, .Tcl.callback(func))))
}

Example:

> after(7000, function() cat("hi!\n"))
> cat("hello?\n")
hello?
hi!

The docs from Revolution Analytics were helpful:
http://www.inside-r.org/r-doc/tcltk/tclRequire
Description of the Tcl "after" command:
http://www.astro.princeton.edu/~rhl/Tcl-Tk_docs/tcl/after.n.html

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