R 提供了内置函数 Rprof( ) 对代码的性能进行分析。在分析过程中,会有一个抽样
程序,并且是和后续代码一起运行的,直到分析结束。默认情况下,抽样程序基本上每隔
20 毫秒就会记录一下当前 R 在运行哪个函数。这样,如果某个函数运行得很慢,那么很可
能大部分时间都在调用这个函数。
这种抽样方法可能不会得到非常精确的结果,但是大多数情况下,它都可以满足要求。
在下面的例子中,我们将使用 Rprof( ) 分析调用 my_cumsum1( ) 时的执行过程,并尝
试找出使代码变慢的部分。
使用 Rprof( ) 的方法非常简单:调用 Rprof( ) 开始分析,运行你想分析的代码;
再调用 Rprof(NULL) 停止分析;最后调用 summaryRprof( ) 查看分析结果:
x <- rnorm(1000)
tmp <- tempfile(fileext = ".out")
Rprof(tmp)
for (i in 1:1000) {
my_ _cumsum1(x)
}
Rprof(NULL)
summaryRprof(tmp)
## $by.self
## self.time self.pct total.time total.pct
## "c" 2.42 82.88 2.42 82.88
## "my_cumsum1" 0.46 15.75 2.92 100.00
## "+" 0.04 1.37 0.04 1.37
## $by.total
## total.time total.pct self.time self.pct
## "my_cumsum1" 2.92 100.00 0.46 15.75
## "c" 2.42 82.88 2.42 82.88
## "+" 0.04 1.37 0.04 1.37
##
## $sample.interval
## [1] 0.02
##
## $sampling.time
## [1] 2.92
注意到,我们使用 tempfile( ) 创建了一个临时文件来储存分析数据。如果不向
Rprof( )提供这样一个文件,它会在当前工作目录中自动创建一个 Rprof.out 文件。
summaryRprof( )同样也有这样的默认行为。
分析结果将分析数据汇总为可读格式:$by.self 通过 self.time 对时间进行排序,
$by.total 通过 total.time 对时间排序。更具体地说,self.time 只是在函数内部
运行代码所花的时间,而 total.time 是函数的总运行时间。
要想找出代码瓶颈,我们应该更关心 self.time,因为它代表的是每个函数运行的
独立时间。
之前的分析结果显示 c( ) 占了整个运行中绝大部分时间,即 y <-c(y, sum_x) 是
拖慢函数运行的主要原因。
也可以对 my_cumsum2( ) 进行同样的分析。分析结果显示大部分时间用在了 my_
cumsum2( ) 上,不过这是正常的,因为这是代码所做的唯一一件事。换句话说,my_
cumsum2( ) 中没有某个特定函数会占用大部分的运行时间:
tmp <- tempfile(fileext = ".out")
Rprof(tmp)
for (i in 1:1000) {
my_ _cumsum2(x)
}
Rprof(NULL)
summaryRprof(tmp)
## $by.self
## self.time self.pct total.time total.pct
## "my_cumsum2" 1.42 97.26 1.46 100.00
## "-" 0.04 2.74 0.04 2.74
##
## $by.total
## total.time total.pct self.time self.pct
## "my_cumsum2" 1.46 100.00 1.42 97.26
## "-" 0.04 2.74 0.04 2.74
##
## $sample.interval
## [1] 0.02
##
## $sampling.time
## [1] 1.46
在实际情况中,我们想要分析的代码通常很复杂,它有可能涉及很多不同的函数。如
果只看每个函数的时间,这样的分析结果可能就不够用了。幸运的是,Rprof( ) 支持按
行分析,当指定 line.profiling = TRUE,并使用 source(..., keep.source =
TRUE) 时,它会告诉我们每行代码的运行时间。
我们用以下代码在 code/my_cumsum1.R 中创建一个脚本文件:
my_cumsum1 <- function(x) {
y <- numeric( )
sum_x <- 0
for (xi in x) {
sum_x <- sum_x + xi
y <- c(y, sum_x)
}
y
}
x <- rnorm(1000)
for (i in 1:1000) {
my_ _cumsum1(x)
}
然后,调用 Rprof( ) 和 source( ) 分析这个脚本文件:
tmp <- tempfile(fileext = ".out")
Rprof(tmp, line.profiling = TRUE)
source("code/my_cumsum1.R", keep.source = TRUE)
Rprof(NULL)
summaryRprof(tmp, lines = "show")
## $by.self
## self.time self.pct total.time total.pct
## my_cumsum1.R#6 2.38 88.15 2.38 88.15
## my_cumsum1.R#5 0.26 9.63 0.26 9.63
## my_cumsum1.R#4 0.06 2.22 0.06 2.22
##
## $by.total
## total.time total.pct self.time self.pct
## my_cumsum1.R#14 2.70 100.00 0.00 0.00
## my_cumsum1.R#6 2.38 88.15 2.38 88.15
## my_cumsum1.R#5 0.26 9.63 0.26 9.63
## my_cumsum1.R#4 0.06 2.22 0.06 2.22
##
## $by.line
## self.time self.pct total.time total.pct
## my_cumsum1.R#4 0.06 2.22 0.06 2.22
## my_cumsum1.R#5 0.26 9.63 0.26 9.63
## my_cumsum1.R#6 2.38 88.15 2.38 88.15
## my_cumsum1.R#14 0.00 0.00 2.70 100.00
##
## $sample.interval
## [1] 0.02
##
## $sampling.time
## [1] 2.7
这次显示的不再是函数名称,而是脚本文件中的行号。我们通过 $by.self 中最上面
的几行就可以轻松找出花费时间最多的代码行。my_cumsum1.R#6 代表 y<- c(y,
sum_x),这个结果与之前的分析结果相同。

用 Rprof 进行性能分析的更多相关文章

  1. 使用VisualVM进行性能分析及调优(转)

    VisualVM 是一款免费的\集成了多个 JDK 命令行工具的可视化工具,它能为您提供强大的分析能力,对 Java 应用程序做性能分析和调优.这些功能包括生成和分析海量数据.跟踪内存泄漏.监控垃圾回 ...

  2. [转]设置Android手机以使用ARM Streamline进行性能分析(二)

    原文因为arm社区改版访问不到了,原作者鲍方,原文地址,这篇是从google cache里挖出来的,希望能帮到要对cocos2dx优化的各位   Posted by Fang Bao, Leave C ...

  3. 使用 VisualVM 进行性能分析及调优

    VisualVM 是一款免费的性能分析工具.它通过 jvmstat.JMX.SA(Serviceability Agent)以及 Attach API 等多种方式从程序运行时获得实时数据,从而进行动态 ...

  4. Linux C++程序进行性能分析工具gprof使用入门

    性能分析工具 软件的性能是软件质量的重要考察点,不论是在线服务程序还是离线程序,甚至是终端应用,性能都是用户体验的关键.这里说的性能重大的范畴来讲包括了性能和稳定性两个方面,我们在做软件测试的时候也是 ...

  5. 【转载】利用window.performance.timing进行性能分析

    利用window.performance.timing进行性能分析   性能分析... window.performance.timing中相关属性语义: // .navigationStart 准备 ...

  6. [转]程序进行性能分析工具gprof使用入门

    性能分析工具 软件的性能是软件质量的重要考察点,不论是在线服务程序还是离线程序,甚至是终端应用,性能都是用户体验的关键.这里说的性能重大的范畴来讲包括了性能和稳定性两个方面,我们在做软件测试的时候也是 ...

  7. 【Java VisualVM】使用 VisualVM 进行性能分析及调优

    转载:https://blog.csdn.net/lmb55/article/details/79267277 一.概述 开发大型 Java 应用程序的过程中难免遇到内存泄露.性能瓶颈等问题,比如文件 ...

  8. 用 dotTrace 进行性能分析时,各种不同性能分析选项的含义和用途

    对 .NET 程序进行性能分析,dotTrace 能应对绝大多数的场景.在开启一个进程进行性能分析之前,我们会看到一些性能分析选项(Profiler Options).本文将介绍这几个选项的含义,并用 ...

  9. Linux使用sar进行性能分析

    转:https://blog.csdn.net/xusensen/article/details/54606401#sar%E7%AE%80%E4%BB%8B Linux使用sar进行性能分析 Lin ...

随机推荐

  1. org.apache.commons.beanutils.BeanUtils的常见用法

    import org.apache.commons.beanutils.BeanUtils BeanUtils1. public static void copyProperty(Object bea ...

  2. Oracle Database Memory Structures

    Oracle Database creates and uses memory structures for various purposes. For example, memory stores ...

  3. 模拟退火算法(run away poj1379)

    http://poj.org/problem?id=1379 Run Away Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: ...

  4. 终于修好了MacBook

    之前由于Trackpad故障,陆家嘴苹果店开了维修单,让我在2周内去更换,详见第二次去苹果店维修MacBook. 后来由于购买了AppleCare进行延保,又担心放在那维修时间长,就懒得去更换了. 昨 ...

  5. ubuntu下安装meshlab

    PPA 安装,打开终端,输入以下命令: sudo add-apt-repository ppa:zarquon42/meshlab sudo apt-get update sudo apt-get i ...

  6. 由于dns服务为启动导致的GI集群启动故障

    1.物业由于突然断电导致grid集群重新启动后rac数据库无法正常启动,对集群进行检查,结果如下,发现其中有4个数据库状态为instance shutdown.[root@node1 ~]# su - ...

  7. UA-* headers

    HTTP The Definitive Guide Request headers are headers that make sense only in a request message. The ...

  8. tomcat启动错误:ZipException

    [/opt/apache-tomcat-/webapps/secsight.war] -Dec- ::] org.apache.catalina.core.ContainerBase.addChild ...

  9. dp\dpi\px\pt\em单位长度理解

    屏幕都有固定的物理长宽度属性和分辨率 比如电脑.比如手机屏幕 例如有手机屏幕尺寸是1.5英寸x2英寸,屏幕分辨率为240x320, 那么可以推算水平方向每英寸的像素数(dpi)是:240/1.5=16 ...

  10. (2.11)Mysql之SQL基础——存储过程与变量

    (2.11)Mysql之SQL基础——存储过程 关键字:mysql存储过程 查看存储过程: []SELECT * FROM information_schema.ROUTINES WHERE ROUT ...