购买
下载掌阅APP,畅读海量书库
立即打开
畅读海量书库
扫码下载掌阅APP

第3章
R性能监控包

本章主要介绍了R语言性能相关的3个工具包,帮助读者找到程序中性能的瓶颈。

3.1 R语言本地缓存工具memoise

问题

如何提高R程序性能?

引言

缓存技术在计算机系统中运用得非常广泛,对于高并发访问的应用来说,缓存技术是性价比最高的性能提升方案,特别是对于重复性计算,缓存能为我们节省大量的CPU时间,可能达到99%。R语言以统计计算著名,但其中很多算法包都是在进行大量重复的计算。优化正在进行,改变已经开始。以Hadley Wickham为代表的R语言领军人物,正在让R快起来!

3.1.1 memoise介绍

memoise是一个非常简单的缓存包,以本地缓存为基础,减少单机的重复性计算,从而间接地提升了单机的CPU性能。当用相同的参数对同一个函数执行第二次计算的时候,如果可以直接用第一次计算过的结果作为第二次计算的结果,而不是再重算一遍,那么我们就节省了CPU时间。

memoise包的API非常简单,只有2个函数。

·memoize:定义缓存函数,把一个函数计算的结果加载到本地缓存。

·forget:重置缓存函数,把一个函数的计算结果从本地缓存中删除。

3.1.2 memoise安装

本节使用的系统环境是:

·Win764bit

·R:3.0.1x86_64-w64-mingw32/x64b4bit

注 memoise安装同时支持Windows 7环境和Linux环境。

memoise安装的过程如下:


~ R # 
启动R 
程序
> install.packages("memoise") # 
安装memoise 
包
> library(memoise) # memoise 
加载

3.1.3 memoise使用

下面我们进行缓存实验,假设一个函数运行时CPU耗时1秒。


> fun <- memoise(function(x) { Sys.sleep(1); runif(1) })# 
定义缓存函数,运行时停1 
秒
> system.time(print(fun())) # 
第一次执行fun 
函数,等待1 
秒完成
[1] 0.05983416
用户 
系统 
流逝
   0    0    1
> system.time(print(fun())) # 
第二次执行fun 
函数,从缓存中获得结果,瞬时完成
[1] 0.05983416
用户 
系统 
流逝
   0    0    0
> forget(fun) # 
重置缓存函数,清空函数的缓存数据
[1] TRUE
> system.time(print(fun())) # 
第三次执行fun 
函数,等待1 
秒完成
[1] 0.6001663
用户 
系统 
流逝
   0    0    1
> system.time(print(fun())) # 
第四次执行fun 
函数,从缓存中获得结果,瞬时完成
[1] 0.6001663
用户 
系统 
流逝
   0    0    0

对上面执行过程的具体描述如下:

(1)通过memoise()函数,创建缓存函数fun()。

(2)第一次执行fun()函数,等待1秒。

(3)第二次执行fun()函数,无等待,直接从缓存中获得结果。

(4)通过forget()函数,清空缓存中fun()函数的结果。

(5)第三次执行fun()函数,由于fun()函数的结果被清空,所以重新执行fun()函数,等待1秒。

(6)第四次执行fun()函数,无等待,直接从缓存中返回结果。

3.1.4 memoise()函数源代码分析

memoise项目是一个典型的以面向对象编程方式实现的R语言项目,R语言的面向对象编程将在本书姊妹篇《R的极客理想—高级开发篇》一书中给大家详细介绍。memoise项目的源代码在Gihub上可以找到,项目地址是https://github.com/hadley/memoise。

1.memoise()函数源代码

接下来,我们解读memoise()函数的源代码,memoise()函数主要做了3件事。

(1)new_cache()函数创建新的缓存空间,给f()函数。

(2)生成f()函数的hash值,作为key。

(3)返回缓存后的f函数引用。


memoise <- memoize <- function(f) { # 
定义同名函数memoise 
和memoize
   cache <- new_cache() # 
创建新的缓存空间
  memo_f <- function(...) { # 
生成hash 
值
    hash <- digest(list(...))
    if (cache$has_key(hash)) {
      cache$get(hash)
    } else {
      res <- f(...)
      cache$set(hash, res)
      res
    }
}
attr(memo_f, "memoised") <- TRUE
return(memo_f)
}

2.forget()函数源代码

forget()函数主要做了2件事。

(1)检查环境中是否缓存了f()函数。

(2)如果有f()函数的缓存,则清空f()函数的缓存值。


forget <- function(f) {
  if (!is.function(f)) return(FALSE) # 
判断函数是否在缓存中
  env <- environment(f)
  if (!exists("cache", env, inherits = FALSE)) return(FALSE)
  cache <- get("cache", env) # 
清空函数的缓存值
  cache$reset()
  TRUE
}

3.私有函数:new_cache()函数源代码

new_cache()函数主要做了3件事。

(1)在new_cache()函数里,定义cache对象,保存在env的环境中。

(2)通过new_cache()函数,构造list类型对象,包括reset,set,get,has_key,keys方法。

(3)通过list对象,对cache对象进行访问操作。


new_cache <- function() {
  cache <- NULL # 
定义cache 
对象
  cache_reset <- function() { # 
定义清空缓存的函数
    cache <<- new.env(TRUE, emptyenv())
  }
  cache_set <- function(key, value) { # 
定义设置缓存的函数
    assign(key, value, env = cache)
  }
  cache_get <- function(key) { # 
定义取得缓存的函数
    get(key, env = cache, inherits = FALSE)
  }
  cache_has_key <- function(key) { # 
检查缓存是否存在
   exists(key, env = cache, inherits = FALSE)
  }
  cache_reset() # 
运行清空缓存
  list( # 
返回缓存对象
    reset = cache_reset,
    set = cache_set,
    get = cache_get,
    has_key = cache_has_key,
    keys = function() ls(cache)
  )
}

从源代码中,我们不仅能发现memoise包的设计思想,还能感受到作者对R语言深刻的理解。memoise包的代码简洁、高效,值得大家用心学习。

3.2 R语言性能监控工具Rprof

问题

如何找到性能瓶颈?

引言

随着R语言使用越来越深入,R语言的计算性能问题越来越突显。如何能清楚地了解一个算法对CPU的耗时,将成为性能优化的关键因素。值得庆幸的是,R的基础库提供了性能监控的函数Rprof()。

3.2.1 Rprof()函数介绍

Rprof()函数是R语言核心包自带的一个性能数据日志函数,可以打印出函数的调用关系和CPU耗时的数据。然后通过summaryRprof()函数分析Rprof()生成的日志数据,获得性能报告。最后使用profr库的plot()函数,对报告进行可视化。

3.2.2 Rprof()函数的定义

本节使用的系统环境是:

·Win764bit

·R:3.0.1x86_64-w64-mingw32/x64b4bit

注 Rprof和profr同时支持Windows 7环境和Linux环境。

Rprof()函数在基础包utils中定义,不用安装,直接可以使用。查看Rprof()函数的定义。


~ R # 
启动R 
程序
> Rprof # 
查看Rprof() 
函数定义
function (filename = "Rprof.out", append = FALSE, interval = 0.02,
memory.profiling = FALSE, gc.profiling = FALSE, line.profiling = FALSE,
numfiles = 100L, bufsize = 10000L)
{
if (is.null(filename))
filename <- ""
invisible(.External(C_Rprof, filename, append, interval,
memory.profiling, gc.profiling, line.profiling, numfiles,
bufsize))
}
<bytecode: 0x000000000d8efda8>

Rprof()函数用来生成记录性能指标的日志文件,通常我们只需要指定输出文件(filename)就可以了。

3.2.3 Rprof()函数使用:股票数据分析案例

以本书6.6节股票数据作为测试数据集,数据文件为000000_0.txt,1.38MB,在随书的代码文件中可以找到。本节只是性能测试,关于数据的业务含义,请参看6.6节内容。


> bidpx1<-read.csv(file="000000_0.txt",header=FALSE)
> names(bidpx1)<-c("tradedate","tradetime","securityid","bidpx1","bidsize1",
  "off erpx1", "offersize1")
> bidpx1$securityid<-as.factor(bidpx1$securityid)
> head(bidpx1)
  tradedate tradetime securityid bidpx1 bidsize1 offerpx1 offersize1
1 20130724 145004 131810 2.620    6960 2.630  13000
2 20130724 145101 131810 2.860   13880 2.890   6270
3 20130724 145128 131810 2.850  327400 2.851   1500
4 20130724 145143 131810 2.603   44630 2.800  10650
5 20130724 144831 131810 2.890   11400 3.000  77990
6 20130724 145222 131810 2.600 1071370 2.601  35750
> object.size(bidpx1)
1299920 bytes

字段的具体解释:tradedate是交易日期,tradetime是交易时间,securityid是股票ID,bidpx1是买一价,bidsize1是买一量,offerpx1是卖一价,offersize1是卖一量。

计算任务:以securityid分组,计算每小时的买一价的平均值和买一量的总量。


> library(plyr) # 
加载plyr 
包,用于数据处理
> fun1<-function(){ # 
数据处理封装到fun1() 
函数
+     datehour<-paste(bidpx1$tradedate,substr(bidpx1$tradetime,1,2),sep="")
+     df<-cbind(datehour,bidpx1[,3:5])
+     ddply(bidpx1,.(securityid,datehour),summarize,price=mean(bidpx1),size=sum(bidsize1))
+ }
> head(fun1())
  securityid     datehour     price       size
1     131810   2013072210  3.445549  189670150
2     131810   2013072211  3.437179  131948670
3     131810   2013072212  3.421000        920
4     131810   2013072213  3.509442  299554430
5     131810   2013072214  3.578667  195130420
6     131810   2013072215  1.833000     718940

以system.time()函数查看fun1()函数运行时间,运行2次,系统耗时近似,确定第二次没有缓存。


> system.time(fun1())
用户 
系统 
流逝
0.08 0.00 0.07
> system.time(fun1())
用户 
系统 
流逝
0.06 0.00 0.06

用Rprof()函数记录性能指标数据,并输出到文件。


> file<-"fun1_rprof.out" # 
定义性能日志的输出文件位置
> Rprof(file)   # 
开始性能监控
> fun1()    # 
执行计算函数
> Rprof(NULL)     # 
停止性能监控,并输出到文件

查看生成的性能指标文件:fun1_rprof.out。


~ vi fun1_rprof.out # 
用vi
编辑器打开文件
sample.interval=20000
"substr" "paste" "fun1"
"paste" "fun1"
"structure" "splitter_d" "ddply" "fun1"
".fun" "" ".Call" "loop_apply" "llply" "ldply" "ddply" "fun1"
".fun" "" ".Call" "loop_apply" "llply" "ldply" "ddply" "fun1"
".fun" "" ".Call" "loop_apply" "llply" "ldply" "ddply" "fun1"
"[[" "rbind.fill" "list_to_dataframe" "ldply" "ddply" "fun1"

我们其实看不明白这个日志,不知道它到底有什么意义。所以,还要通过summaryRprof()函数来解析这个日志。下面通过summaryRprof()函数查看统计报告。


> summaryRprof(file) # 
读入文件
$by.self
            self.time self.pct total.time total.pct
".fun"           0.06    42.86       0.06     42.86
"paste"          0.02    14.29       0.04     28.57
"[["             0.02    14.29       0.02     14.29
"structure"      0.02    14.29       0.02     14.29
"substr"         0.02    14.29       0.02     14.29
$by.total
                    total.time total.pct self.time self.pct
"fun1"                    0.14    100.00      0.00     0.00
"ddply"                   0.10     71.43      0.00     0.00
"ldply"                   0.08     57.14      0.00     0.00
".fun"                    0.06     42.86      0.06    42.86
".Call"                   0.06     42.86      0.00     0.00
""                        0.06     42.86      0.00     0.00
"llply"                   0.06     42.86      0.00     0.00
"loop_apply"              0.06     42.86      0.00     0.00
"paste"                   0.04     28.57      0.02    14.29
"[["                      0.02     14.29      0.02    14.29
"structure"               0.02     14.29      0.02    14.29
"substr"                  0.02     14.29      0.02    14.29
"list_to_dataframe"       0.02     14.29      0.00     0.00
"rbind.fill"              0.02     14.29      0.00     0.00
"splitter_d"              0.02     14.29      0.00     0.00
$sample.interval
[1] 0.02
$sampling.time
[1] 0.14

数据解释:

·$by.self是当前函数的耗时情况,其中self.time为实际运行时间,total.time为累计运行时间。

·$by.total是整体函数调用的耗时情况,其中self.time为实际运行时间,total.time为累计运行时间。

从$by.self观察,我们发现时间主要花在.fun函数上。

·.fun:实际运行时间0.06,占当前函数时间的42.86%。

·paste:实际运行时间0.02,占当前函数时间的14.29%。

·"[[":实际运行时间0.02,占当前函数时间的13.29%。

·"structure":实际运行时间0.02,占当前函数时间的14.29%。

·"substr":实际运行时间0.02,占当前函数时间的14.29%。

对应在$by.total中,从底到上(1~4),按执行顺序排序。

·4fun1:累计运行时间0.14,占总累计运行时间的100%,实际运行时间0.00。

·3.fun:累计运行时间0.06,占总累计运行时间的42.86%,实际运行时间0.06。

·2paste:累计运行时间0.04,占总累计运行时间的28.57%,实际运行时间0.02。

·1splitter_d:累计运行时间0.02,占总累计运行时间的14.297%,实际运行时间0.00。

这样我们就知道了每个调用函数的CPU时间,进行性能优化就从最耗时的函数开始。

3.2.4 Rprof()函数使用:数据下载案例

我们先安装stockPortfolio包,需要通过stockPortfolio下载股票行情的数据,然后使用Rprof()函数监控下载过程的程序耗时情况。


> install.packages("stockPortfolio")  # 
安装stockPortfolio 
包
> library(stockPortfolio)   # 
加载stockPortfolio 
包
> fileName <- "Rprof2.log"
> Rprof(fileName)   # 
开始性能监控
> gr <- getReturns(c("GOOG", "MSFT", "IBM"), freq="week")
   # 
下载Google, 
微软,IBM 
的股票数据
> gr
Time Scale: week
Average Return
       GOOG         MSFT         IBM
0.004871890  0.001270758 0.001851121
> Rprof(NULL) # 
完成性能监控
> summaryRprof(fileName)$by.total[1:8,] # 
查看性能报告
                     total.time total.pct self.time self.pct
"getReturns"               6.76    100.00      0.00     0.00
"read.delim"               6.66     98.52      0.00     0.00
"read.table"               6.66     98.52      0.00     0.00
"scan"                     4.64     68.64      4.64    68.64
"file"                     2.02     29.88      2.02    29.88
"as.Date"                  0.08      1.18      0.02     0.30
"strptime"                 0.06      0.89      0.06     0.89
"as.Date.character"        0.06      0.89      0.00     0.00

从打印出的$by.total前8条最耗时的记录来看,实际运行时间(self.time)主要花在file(2.02)和scan(4.64)。

3.2.5 用profr包可视化性能指标

profr包提供了对Rprof()函数输出的性能报告可视化展示功能,比summaryRprof()的纯文字的报告,要更友好,有利于阅读。首先,我们要安装profr包。


> install.packages("profr") # 
安装profr
> library(profr) # 
加载profr 
包
> library(ggplot2) # 
用ggplot2 
包来画图, 
加载ggplot2 
包

数据可视化的第一个例子,即股票数据分析案例。下面的代码段产生图3-1和图3-2。


> file<-"fun1_rprof.out"> plot(parse_rprof(file)) # plot 
画图
> ggplot(parse_rprof(file)) # ggplot2 
画图

图3-1 第一个例子中用plot画的图

数据可视化的第二个例子,即数据下载案例。下面的代码段生成图3-3和图3-4。


> fileName <- "Rprof2.log"
> plot(parse_rprof(fileName))
> ggplot(parse_rprof(fileName))

图3-2 第一个例子中用ggplot2画的图

图3-3 第二个例子中用plot画的图

图3-4 第二个例子中用ggplot2画的图

3.2.6 Rprof的命令行使用

Rprof的命令行方法,用来方便地查看日志文件。

1.查看Rprof命令行帮助


~ D:\workspace\R\preforemence\Rprof>R CMD Rprof --help
Usage: R CMD Rprof [options] [file]
Post-process profiling information in file generated by Rprof().
Options:
  -h, --help        print short help message and exit # 
打印帮助信息
  -v, --version     print version info and exit # 
打印版本信息
  --lines           print line information # 
打印显示多行
  --total           print only by total # 
只显示总数
  --self            print only by self # 
只显示私有的
  --linesonly       print only by line (implies --lines) # 
只显示单行( 
配合–lines 
使用)
  --min%total=      minimum % to print for 'by total' # 
显示total 
的不低于X 
的百分比
  --min%self=       minimum % to print for 'by self' # 
显示self 
的不低于X 
的百分比

2.Rprof命令行使用

显示完整的报告:


~ D:\workspace\R\preforemence\Rprof>R CMD Rprof fun1_rprof.out
Each sample represents 0.02 seconds.
Total run time: 0.14 seconds.
Total seconds: time spent in function and callees.
Self seconds: time spent in function alone.
   %       total       %        self
total     seconds     self    seconds    name
100.0       0.14       0.0      0.00     "fun1"
 71.4       0.10       0.0      0.00     "ddply"
 57.1       0.08       0.0      0.00     "ldply"
 42.9       0.06      42.9      0.06     ".fun"
 42.9       0.06       0.0      0.00     ".Call"
 42.9       0.06       0.0      0.00     ""
 42.9       0.06       0.0      0.00     "llply"
 42.9       0.06       0.0      0.00     "loop_apply"
 28.6       0.04      14.3      0.02     "paste"
 14.3       0.02      14.3      0.02     "[["
 14.3       0.02      14.3      0.02     "structure"
 14.3       0.02      14.3      0.02     "substr"
 14.3       0.02       0.0      0.00     "list_to_dataframe"
 14.3       0.02       0.0      0.00     "rbind.fill"
 14.3       0.02       0.0      0.00     "splitter_d"
  %         self       %      total
 self     seconds    total   seconds     name
 42.9       0.06      42.9      0.06      ".fun"
 14.3       0.02      28.6      0.04      "paste"
 14.3       0.02      14.3      0.02      "[["
 14.3       0.02      14.3      0.02      "structure"
 14.3       0.02      14.3      0.02      "substr"

只显示total指标,占用时间不低于50%的部分。


~ D:\workspace\R\preforemence\Rprof>R CMD Rprof --total --min%total=50 fun1_rprof.out
Each sample represents 0.02 seconds.
Total run time: 0.14 seconds.
Total seconds: time spent in function and callees.
Self seconds: time spent in function alone.
   %       total       %        self
 total    seconds     self    seconds    name
 100.0      0.14       0.0      0.00     "fun1"
  71.4      0.10       0.0      0.00     "ddply"
  57.1      0.08       0.0      0.00     "ldply"

我们可以通过Rprof进行性能调优,提高代码的性能,让R语言的计算不再是瓶颈。

3.3 R语言性能可视化工具lineprof

问题

有没有一个可视化的性能监控工具?

引言

数据可视化越来越受到人们的追捧,图形比文字更有表达力,基于HTML的可交互的图形比静态的PNG图片更让人惊喜。R语言已经为数据可视化做了充分的准备,比如,图形可视化包ggplot2,世界地图可视化包ggmap,股票可视化包quantmod,基于HTML的可交互的可视化包googleVis等,简简单单的几行代码,就可以让数据变成图片,再让图片变成会动的图片。本节以“性能报告”为切入点,讲述R语言可视化包lineprof。

3.3.1 lineprof介绍

lineprof是一个数据可视化的项目,目标是更友好地可视化性能监控效果。3.2节通过profr库,可以把性能数据以图片的形式输出,但仅仅是一张静态图片。而lineprof可以做得更好,生成基于shiny的可交互的网页,让用户自己动手发现问题。

lineprof项目也是Hadley Wickham的作品。目前项目只在Github上面发布,项目地址是https://github.com/hadley/lineprof。lineprof库的API主要有以下两类函数。

(1)功能函数:

·focus:设置显示高度zoom。

·auto_focus:自动设置显示高度zoom。

·lineprof:记录CPU和内存的占用。

·shine:用shiny输出。

(2)内部函数:辅助功能函数

·align:源代码对齐。

·find_ex:用于加载demo。

·line_profile:格式化性能监控数据的输出(Rprof)。

·parse_prof:格式化输出。

·reduce_depth:设置输出的深度。

3.3.2 lineprof安装

本节使用的系统环境是:

·Linux:Ubuntu Server 12.04.2LTS 64bit

·R:3.0.1x86_64-pc-linux-gnu

·IP:192.168.1.201

注 lineprof只支持Linux环境。

由于项目lineprof还没有发布到CRAN,在lineprof包安装时,仅支持从Github安装。所以,需要通过devtools包来安装Github上面发布的R语言项目。


~ R # 
启动R 
程序
> library(devtools) # 
加载devtools
> install_github("lineprof") # 
通过devtools 
工具,安装linepro
> install_github("shiny-slickgrid", "wch") # 
安装制定的shiny 
控制台
> library(lineprof) # 
加载lineprof

3.3.3 lineprof使用

我们使用官方提供的例子,介绍lineprof包使用。


> source(find_ex("read-delim.r")) # 
加载脚本文件read-delim.r
> wine <- find_ex("wine.csv") # 
加载测试数据集
> x <- lineprof(read_delim(wine, sep = ","), torture = TRUE) # 
执行read_delim 
算法,
并通过lineprof 
记录性能指标Zooming to read-delim.r (97% of total time)

在例子的资源中,read-delim.r是目标函数的脚本文件,wine.csv是测试数据集,x:lineprof是生成的数据报告。

查看文件:read-delim.r。


function(file, header = TRUE, sep = ",", stringsAsFactors = TRUE) {
  # Determine number of fields by reading first line
  first <- scan(file, what = character(1), nlines = 1, sep = sep, quiet = TRUE)
  p <- length(first)
  # Load all fields
  all <- scan(file, what = as.list(rep("character", p)), sep = sep,
    skip = if (header) 1 else 0, quiet = TRUE)
  # Convert from strings to appropriate types
  all[] <- lapply(all, type.convert, as.is = !stringsAsFactors)
  # Set column names
  if (header) {
    names(all) <- first
    rm(first)
  } else {
    names(all) <- paste0("V", seq_along(all))
  }
  # Convert list into data frame
  class(all) <- "data.frame"
  attr(all, "row.names") <- c(NA_integer_, -length(all[[1]]))
  all
}

加载文件wine.csv。


> df<-read.csv(file=wine)
> object.size(df) # 
数据集占内存大小
20440 bytes
> head(df
,3) # 
显示前3 
行数据
   type    alcohol     malic     ash       alcalinity  magnesium       phenols    flavanoids
   nonflavanoids       proanthocyanins     color       hue dilution    proline
1  A       14.23       1.71      2.43      15.6        127             2.80       3.06
   0.28    2.29        5.64      1.04      3.92        1065
2  A       13.20       1.78      2.14      11.2        100             2.65       2.76
   0.26    1.28        4.38      1.05      3.40        1050
3  A       13.16       2.36      2.67      18.6        101             2.80       3.24
   0.30    2.81        5.68      1.03      3.17        1185

x对象:lineprof生成的数据报告。


> x # 
记录性能指标的对象
Reducing depth to 2 (from 8)
Common path:
    time alloc release dups ref src
1  0.002 0.001   0.000    0 #3  read_delim
2  0.049 0.009   0.003   11 #3  read_delim/scan
3  0.026 0.001   0.008    0 #4  read_delim
4  0.379 0.072   0.006   14 #7  read_delim/scan
5  0.003 0.000   0.000    0 #11 read_delim
6  0.106 0.015   0.030    3 #11 read_delim/lapply
7  0.008 0.004   0.000    3 #11 read_delim
8  0.210 0.028   0.077   36 #16 read_delim/rm
9  0.004 0.001   0.000    1 #22 read_delim
10 0.035 0.005   0.004    8 #23 read_delim/[[
11 0.002 0.000   0.000    1 #23 read_delim/length
12 0.001 0.000   0.000    1 #23 read_delim/c
13 0.006 0.004   0.000    1 #23 read_delim
14 0.001 0.000   0.000    0 #23 read_delim/attr<-

用shinySlickgrid库,把性能指标数据可视化,并以网页形式输出。


> library(shinySlickgrid) # 
加载shinySlickgrid 
库
> shine(x) # 
启动shiny 
程序
Loading required package: shiny
Shiny URLs starting with /lineprof will mapped to /home/conan/R/x86_64-pc-linuxgnu-
library/3.0/lineprof/www
Shiny URLs starting with /slickgrid will mapped to /home/conan/R/x86_64-pc-linuxgnu-
library/3.0/shinySlickgrid/slickgrid
Listening on port 6742

shiny会自动在后台打开一个Web服务器,默认的Web访问端口是6742,之后就可以通过浏览器远程访问了。打开浏览器地址栏输入http://192.168.1.201:6742,如图3-5所示。

图3-5 可视化性能监控

在图3-5中,网页的表格一共有6列,其中#是行号,source code是监控的目标函数源代码,t是当前行执行的总(total)时间(秒),r是释放(released)内存量,a是分配(allocated)内存量,d是重复(duplicates)次数。下面给出函数性能数据的解释。

·#6(第6行),用于加载数据,总时间占用0.309s,分配内存0.064mb,方法重复次数为14次

·#15(第15行),用于清除数据,总时间占用0.179s,释放内存0.065mb,方法重复次数为37次

通过lineprof可视化工具,我们生成的报告更灵活,更直观,更好看,支持在网页上进行用户交互,甚至是网页版本的。如果再和3.2节R语言性能监控工具Rprof的效果比较一下,你会惊叹R语言是如此强大,它的进步如此神速。当然,R语言具有无限的潜力,需要更多人来推动它进步,希望你也能给它助力。 mwlDvywdYI2bdegVk7MWieX803Jlsv6/tW86T4g1JGydaglyVH8ls76SUszhH4w/

点击中间区域
呼出菜单
上一章
目录
下一章
×