5

我开始了解这个microbenchmark R包的一些功能。我从 Hadley Wickham 的这个出版物中实现了一个示例代码,并收到一个错误,我找不到任何准确的信息,我无法处理。提前感谢您的任何解释/提示等。

示例代码:

library(microbenchmark)

f <- function() NULL
microbenchmark(
  NULL,
  f()
)

控制台输出:

Error in microbenchmark(NULL, f()) : 
  Measured negative execution time! Please investigate and/or contact the package author.

更新。这是我的seesionInfo()控制台输出:

> sessionInfo()
R version 3.0.2 (2013-09-25)
Platform: x86_64-w64-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=Polish_Poland.1250  LC_CTYPE=Polish_Poland.1250    LC_MONETARY=Polish_Poland.1250
[4] LC_NUMERIC=C                   LC_TIME=Polish_Poland.1250    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] ggplot2_0.9.3.1      microbenchmark_1.3-0

loaded via a namespace (and not attached):
 [1] colorspace_1.2-4   dichromat_2.0-0    digest_0.6.3       grid_3.0.2         gtable_0.1.2       labeling_0.2      
 [7] MASS_7.3-29        munsell_0.4.2      plyr_1.8           proto_0.3-10       RColorBrewer_1.0-5 reshape2_1.2.2    
[13] scales_0.2.3       stringr_0.6.2      tools_3.0.2    

更新 2.包的作者要求我提供一些进一步的信息:

  • R 变量R.version

    R.version _
    platform x86_64-w64-mingw32
    arch x86_64
    os mingw32
    system x86_64, mingw32
    status
    major 3
    minor 0.2
    year 2013
    month 09
    day 25
    svn rev 63987
    language R
    version.string R version 3.0.2 (2013-09-25) 昵称飞盘帆船

  • 我电脑中 CPU 的品牌、型号和速度:

处理器:Intel(R) Core(TM) i7-2600K CPU @ 3.40GHz 3.70 GHz

内存:16,0 GB

系统类型:64位

更新 3。

我注意到上面代码的修改之一确实返回了正确的结果:

> ### 1 
> f <- function(){NULL} 
> microbenchmark(NULL, f())
Error in microbenchmark(NULL, f()) : 
  Measured negative execution time! Please investigate and/or contact the package author.
> 
> 
> ### 2 
> f <- function(){ } 
> microbenchmark(NULL, f())
Error in microbenchmark(NULL, f()) : 
  Measured negative execution time! Please investigate and/or contact the package author.
> 
> 
> ### 3 
> f <- function(){NULL} 
> microbenchmark(f())
Unit: nanoseconds
 expr min lq median uq  max neval
  f()   0  1      1  1 7245   100
> 
> ### 4 
> f <- function(){ } 
> microbenchmark(f())
Error in microbenchmark(f()) : 
  Measured negative execution time! Please investigate and/or contact the package author.
4

3 回答 3

3

根据您使用的操作系统,您计算机上安装的高性能计时器子系统驱动程序可能存在问题。

QueryPerformanceCounter在 Windows 领域,通过和QueryPerformanceFrequency函数访问 HPT 。QPF 告诉您计数器的频率,从而告诉您计数器的准确性;QPC / QPF 以秒为单位为您提供一个值,通常是计算机启动的时间。

问题是驱动程序对此 API 的支持有时参差不齐。AMD过去特别有问题,我亲身经历过。

您可以尝试在线搜索您的 CPU 和/或主板的驱动程序,以查看是否缺少驱动程序。这可能会解决这个问题。

编辑:

@MatthewLundberg 指出不同内核上的 rdtsc 指令有时会略有偏差。解决此问题的一种廉价方法是更改​​程序的 cpu 亲和性,使其仅在一个内核上运行。

假设您使用的是 Win Vista 或更高版本,请进入任务管理器,右键单击正在运行您的代码的进程,选择“关联...”并将其限制为仅一个处理器(第一个 CPU 很好)。

于 2013-12-29T16:46:40.840 回答
2

正如另一个答案所述,Windows 计时器似乎没有足够的精度来测量执行时间,即执行时间小于 1 纳秒。nanotimer.c如果我们将文件中的包源简单更改为do_microtiming()C函数...

if (start < end) {
    const nanotime_t diff = end - start;
    if (diff < overhead) {
        ret[i] = R_NaReal;
        n_under_overhead++;
    } else {
        ret[i] = diff - overhead;
    }
} else if( start == end ) { // <----- This elseif is our minor edit
  error( "Start and end have same time. Not enough precision to measure execution time" );
} else {
    error("Measured negative execution time! Please investigate and/or "
          "contact the package author.");
}

然后测试一下...

f <- function() NULL
microbenchmark( f() )
#Error in microbenchmark(f()) : 
#  Start and end have same time. Not enough precision to measure execution time

使用当前驱动程序,您似乎无法在您的(和我的)Windows 系统上测量亚纳秒时间。

所以执行时间不是负数,它太小了,你无法测量它。

于 2014-01-05T16:19:31.617 回答
0

解决此问题的另一种方法是增加您进行基准测试的表达式的工作量。我在尝试了解该microbenchmark函数的工作原理时遇到了同样的问题,并通过将表达式(测试函数f1f2f3)更改为更具挑战性的内容来避免该问题:

library(microbenchmark)

f1 <- function() { factorial(10) }
f2 <- function() { 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10 }
f3 <- function() { factorial(16) / (11 * 12 * 13 * 14 * 15 * 16) }
benchmarkResults <- microbenchmark(f1(), f2(), f3(), times = 1000L)
print(benchmarkResults)
于 2015-08-15T14:58:56.293 回答