我正在了解microbenchmark R包的一些特性。我从Hadley的这出版物中实现了一个示例代码,并收到了一个错误,我找不到任何精确的信息,也无法处理。预先感谢您的任何解释/暗示等。
示例代码:
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.version
R.version _
x86_64-w64-mingw32 32平台
arch x86_64
os mingw32
系统x86_64,mingw32
状态
专业3
小调0.2
2013年
09年月
第25天
svn rev 63987
语言R
version.string R版3.0.2 (2013-09-25)昵称飞盘航行处理器: Intel(R) Core(TM) i7-2600K CPU @ 3.40GHz 3.70 GHz
RAM: 16,0GB
系统类型: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.发布于 2014-01-05 16:19:31
正如另一个答案所述,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系统上,你似乎无法用当前的驱动程序来测量亚纳秒的时间。
所以执行时间不是负的,它太小了,你无法测量它。
发布于 2013-12-29 16:46:40
根据您正在使用的操作系统,计算机上的高性能定时器子系统安装的驱动程序可能会出现问题。
在Windows中,可以通过QueryPerformanceCounter和QueryPerformanceFrequency函数访问HPT。QPF告诉您计数器滴答的频率,从而告诉您计数器的准确性;QPC / QPF以秒为单位给出一个值,通常是计算机被引导的时间。
问题是,对这个API的驱动程序支持有时是不稳定的。AMD在过去特别有过麻烦,我亲身经历过这种情况。
您可以尝试在网上搜索CPU和/或主板的驱动程序,看看是否缺少驱动程序。这可能会解决这个问题。
编辑:
@MatthewLundberg指出,不同核上的rdtsc指令有时略有偏离。解决这个问题的一种廉价方法是改变程序的cpu亲和力,使其只在一个核心上运行。
假设您在Win或更高版本上,进入任务管理器,右键单击正在运行代码的进程,选择“亲和.”并将其限制为一个处理器(第一个CPU很好)。
发布于 2015-08-15 14:58:56
解决此问题的另一种方法是增加您基准测试的表达式的工作量。我在试图理解microbenchmark函数如何工作时遇到了同样的问题,并通过将表达式(测试函数f1、f2和f3)更改为更具挑战性的内容来避免这个问题:
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)https://stackoverflow.com/questions/20827305
复制相似问题