Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/grails/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R:如何使system.time打印正在计时的表达式?_R_Timing - Fatal编程技术网

R:如何使system.time打印正在计时的表达式?

R:如何使system.time打印正在计时的表达式?,r,timing,R,Timing,我有大量的R函数相互调用,它们可能需要很多时间,我使用以下方法调用它们: 而且,要找出函数和计时之间的对应关系需要一些时间 我尝试了这个功能: verbose <- function (expr) { ret <- system.time(expr) cat("<<<<<<") print(expr) print(ret) } > verbose(Sys.sleep(2)) <<<<<<NU

我有大量的
R
函数相互调用,它们可能需要很多时间,我使用以下方法调用它们:

而且,要找出函数和计时之间的对应关系需要一些时间

我尝试了这个功能:

verbose <- function (expr) {
  ret <- system.time(expr)
  cat("<<<<<<")
  print(expr)
  print(ret)
}
> verbose(Sys.sleep(2))
<<<<<<NULL
           user          system         elapsed 
     0 (0.00ms)      0 (0.00ms) 2.002 (2.00sec) 

verbose虽然不完全符合您的要求,但我将以下包装器用于
System.time
,并将其作为我个人utils文件的一部分保存:

  s.t <- function(expr, msg="", verbose=TRUE, gcFirst=FALSE, title="") { 
  # wrapper for system.time
  # title is an alternate for msg, where user needs simply give a name to the section being timed.
  # msg is for a custome message before the sytem.time output

    ret <- capture.output(system.time(expr=expr, gcFirst=gcFirst))
    ret <- paste(ret, collapse="\n")

    if (nchar(title))
      msg <- paste0("Time to complete ", title, ":")

    if (verbose){
      if (nchar(msg) == 0)
        cat(ret)
      else 
        cat(paste(msg, collapse=""), ret, sep="\n")
    }
  }  

s.t您的函数
verbose
可以通过添加
substitute
,打印传递给参数
expr
的值

将行
print(expr)
更改为

print(substitute(expr))
然后

冗长的(系统睡眠(2))
对于你的函数
verbose
你可以使用
print(replacement(expr))
你可以使用package-microbenchmark,例如
microbenchmark(f1(),times=1)
对于早期分析来说是很好的,但是当你看到这样复杂的东西时,你真的应该转到
Rprof
summaryRprof
。flodel关于使用
Rprof
的评论绝对值得注意。
  s.t <- function(expr, msg="", verbose=TRUE, gcFirst=FALSE, title="") { 
  # wrapper for system.time
  # title is an alternate for msg, where user needs simply give a name to the section being timed.
  # msg is for a custome message before the sytem.time output

    ret <- capture.output(system.time(expr=expr, gcFirst=gcFirst))
    ret <- paste(ret, collapse="\n")

    if (nchar(title))
      msg <- paste0("Time to complete ", title, ":")

    if (verbose){
      if (nchar(msg) == 0)
        cat(ret)
      else 
        cat(paste(msg, collapse=""), ret, sep="\n")
    }
  }  
print(substitute(expr))
> verbose(Sys.sleep(2))
<<<<<<Sys.sleep(2)
   user  system elapsed 
  0.023   0.007   2.009