【问题标题】:R - How to get the time elapsed between two points?R - 如何获得两点之间经过的时间?
【发布时间】:2017-08-16 15:48:59
【问题描述】:

我尝试使用Sys.time 来获取两点之间经过的时间。但是,它并没有以我喜欢的方式输出。

这就是现在的样子:

a <- Sys.time
...running stuff between these two points...
b <- Sys.time
c <- b - a
c
Time difference of 1.00558 hours

我只想要数字和单位。我知道要得到我能做的数字:

c[[1]]

但是,有时c 的结果可以给我几秒钟或几分钟。我只想要我有数字并且单位以小时为单位的实例。有谁知道使用 Sys.time() (或任何替代方法)我会得到类似以下内容的方法:

if (units == "hours")
{
  if (number => 1)
  {
      #do something
  }
}

【问题讨论】:

  • 可能是difftime(b, a, units="hours") 什么的
  • 顺便说一句,你真的不应该使用c作为变量名,因为它是一个重要的函数。
  • 正如@Frank 所说,您可以指定 difftime 对象的单位。您还可以将其与数字进行比较,因此如果您的 difftime 对象以小时为单位,您可以执行 mydiff>=2,如果超过 2 小时,它将返回 True。

标签: r time


【解决方案1】:

使用base R的difftime可以得到不同单位的时差。休息是格式化。

a = Sys.time()
Sys.sleep(5)    #do something
b = Sys.time()    
paste0(round(as.numeric(difftime(time1 = b, time2 = a, units = "secs")), 3), " Seconds")
#[1] "5.091 Seconds"

【讨论】:

    【解决方案2】:

    tictoc 包通常返回秒。该软件包中的其他解决方案手动将其转换为其他单位,但我发现它仍然看起来不正确。相反,使用 toc() 中内置的 func.toc 参数来更改输出。例如:

    toc_min <- function(tic,toc,msg="") {
      mins <- round((((toc-tic)/60)),2)
      outmsg <- paste0(mins, " minutes elapsed")
    }
    

    然后:

    tic()
    Sys.sleep(1)
    toc(func.toc=toc_min)
    

    返回

    0.02 minutes elapsed
    

    【讨论】:

      【解决方案3】:

      tictoc 包简化了这种计时。它不返回小时数,但我们可以创建一个新函数,将其基于秒的测量值转换为小时数。

      library(tictoc)
      
      toc_hour <- function() {
        x <- toc()
        (x$toc - x$tic) / 3600
      }
      

      您通常使用tic() 启动计时器并使用toc() 停止它。

      tic()
      Sys.sleep(2)
      toc()
      # 2.02 sec elapsed
      

      调用toc_hour() 而不是toc() 会返回经过的小时数。

      tic()
      Sys.sleep(2)
      toc_hour()
      # 2.25 sec elapsed
      #  elapsed 
      # 0.000625 
      

      它仍然会打印小时数之上的秒数,但如果您捕获结果,它只会存储小时数以供下游分析。

      tic()
      Sys.sleep(2)
      x <- toc_hour()
      
      if(x < 1) {print("This took under an hour")}
      

      【讨论】:

        【解决方案4】:

        我认为 lubridate 是最适合您的解决方案:

        start <- Sys.time()
        ## Do Stuff here
        end <- Sys.time()
        elapsed <- lubridate::ymd_hms(end) - lubridate::ymd_hms(start) 
        message(elapsed)
        

        它应该返回一些有用的东西,比如: "时差12.1小时"

        【讨论】:

        • startend 已经是 POSIXct。您的解决方案中不需要lubridate::ymd_hms
        【解决方案5】:

        您可以评估所有内容作为system.time 函数的参数。它会给你以秒为单位的经过时间。

        paste0(system.time( rnorm(1000000, 0, 1) )[3] / 3600, " hours")
        # "2.58333333334172e-05 hours"
        

        或者,您可以在 cmets 中使用 Frank 的建议。 difftime(b, a, units = "hours") 这可能是大多数情况下的主要解决方案

        【讨论】:

          【解决方案6】:

          也许你可以试试´tictoc´ 包。 如文档中所述,您可以执行以下操作:

          tic()
          
          #Do something
          
          toc(log = TRUE, quiet = TRUE)
          
          #Put the result in a log
          log.txt <- tic.log(format = TRUE)
          
          #Extract only the value
          res <- gsub( " sec elapsed", "", unlist(log.txt))
          
          #Clear the log
          tic.clearlog()
          

          这样,res 只为您提供价值,并且以秒为单位,因此有几个小时非常简单。 此外,如果您不清除日志,您可以连续输入tic()toc() 并将所有内容放入您的log.txt,然后gsub( " sec elapsed", "", unlist(log.txt)) 将为您提供一个字符串向量,每个字符串的值以秒为单位迭代非常有用

          【讨论】:

            猜你喜欢
            • 2012-02-08
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-03-22
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多