In GNU R the simplest way to measure execution time of a piece code is to use system.time. However, sometimes I want to find out how many times some function can be executed in one second. This is especially useful when we want to compare functions that have significantly different execution speed.

Fortunately times per second benchmark for execution time can be simply evaluated using the following snippet:

tps **<-** **function****(**f, time**)** **{**

gc**()**

i **<-** 0

start **<-** proc.time**()[**3**]**

**repeat** **{**

i **<-** i **+** 1

f**(**i**)**

stop **<-** proc.time**()[**3**]**

**if** **(**stop **–** start **>** time**)** **{**

return **(**i **/** **(**stop **–** start**))**

**}**

**}**

**}**

This function takes two parameters: a function to be benchmarked (f) and how much time is to be used for evaluation (time). It returns an estimate how many times per second function f can be executed.

As a simple application of tps function consider calculating relative speed of standard, lattice and ggplot2 graphics. The following function compares them by plotting histograms:

library**(**ggplot2**)**

library**(**lattice**)**

test **<-** **function****(**n, time**)** **{**

x **<-** runif**(**n**)**

b **<-** c**(**tps**(****function****(**i**)** **{**

hist**(**x, 10, main **=** i**)**

**}**, time**)**,

tps**(****function****(**i**)** **{**

print**(**histogram**(**x, nbin **=** 10, main **=** format**(**i**)))**

**}**, time**)**,

tps**(****function****(**i**)** **{**

print**(**qplot**(**x, binwidth**=**0.1, main **=** i**))**

**}**, time**))**

names**(**b**)** **<-** c**(**“hist”, “histogram”, “qplot”**)**

return**(**b**)**

**}**

The function takes two arguments. First is number of points to sample for the histogram and second is time passed to tps function. On my computer the test gave the following result for 10000 size of the sample and 5 seconds for each function each :

> test**(**10000, 5**)**

hist histogram qplot

192.614770 14.285714 5.544933

We can see that standard hist is over 10 times faster than from histogram from lattice and almost 40 times faster than qplot from ggplot2.

*Related*

To

**leave a comment** for the author, please follow the link and comment on their blog:

** R snippets**.

R-bloggers.com offers

**daily e-mail updates** about

R news and

tutorials on topics such as:

Data science,

Big Data, R jobs, visualization (

ggplot2,

Boxplots,

maps,

animation), programming (

RStudio,

Sweave,

LaTeX,

SQL,

Eclipse,

git,

hadoop,

Web Scraping) statistics (

regression,

PCA,

time series,

trading) and more...

If you got this far, why not

__subscribe for updates__ from the site? Choose your flavor:

e-mail,

twitter,

RSS, or

facebook...