使用 R 微基准测试和 system.time 有什么区别?

Sat*_*rma 5 performance r microbenchmark

我想了解 R 的微基准测试和 system.time() 之间的区别?他们如何在内部衡量函数执行时间?

Ric*_*ton 0

在这两种情况下,运行时间都是使用操作系统工具计算的,因此运行时间的计算方式取决于操作系统。

如详细信息部分所述?system.time

system.time 调用函数 proc.time,计算 expr,然后再次调用 proc.time,返回两次 proc.time 调用之间的差值。

proc.time是原始的,C 代码位于src/main/times.c. 该文件状态中的注释:

proc.time() 使用 currentTime() 获取经过的时间,然后使用 getrusage 获取 CPU 时间(在类似 Unix 上),在 Windows 上使用 GetProcessTimes。


从页面的注释部分?microbenchmark

根据底层操作系统的不同,采用不同的计时方法。在 Windows 上,QueryPerformanceCounter 接口用于测量经过的时间。对于Linux,使用clock_gettime API,在Solaris 上使用gethrtime 函数。最后,在 MacOS X 上,未记录的 mach_absolute_time 函数用于避免对 CoreServices 框架的依赖。

我找不到 microbenchmark 存储库,因此您必须下载源代码才能查看确切的详细信息,但计时是由do_microtimingin完成的,它调用, in // /的src/nanotimer.c操作系统相关版本。get_nanotimesrc/nanotimer_nanotimer_gettime.hsrc/nanotimer_nanotimer_macosx.hsrc/nanotimer_nanotimer_rtposix.hsrc/nanotimer_nanotimer_windows.h