[Rd] The cost of method dispatch

Prof Brian Ripley ripley at stats.ox.ac.uk
Fri Aug 25 10:35:04 CEST 2006


People often ask for base functions to be made generic.  There is a 
measurable cost:

> system.time(for(i in 1:1e6)seq(length=5))
[1] 18.603  0.016 18.621  0.000  0.000
> system.time(for(i in 1:1e6) seq.default(length=5))
[1] 12.515  0.012 12.533  0.000  0.000
> system.time(for(i in 1:1e6) NULL)
[1] 0.234 0.017 0.251 0.000 0.000
> system.time(for(i in 1:1e6) rep(1, 5))
[1] 26.380  0.020 26.405  0.000  0.000
> system.time(for(i in 1:1e6) rep.default(1, 5))
[1] 17.504  0.017 17.525  0.000  0.000
> system.time(for(i in 1:1e6) rep.int(1, 5))
[1] 3.400 0.006 3.407 0.000 0.000

on one system (64-bit) and

> system.time(for(i in 1:1e6) seq(length=5))
[1] 23.00  0.00 23.25    NA    NA
> system.time(for(i in 1:1e6) seq.default(length=5))
[1] 11.20  0.02 11.44    NA    NA
> system.time(for(i in 1:1e6) NULL)
[1] 0.28 0.00 0.28   NA   NA
> system.time(for(i in 1:1e6) rep(1, 5))
[1] 30.21  0.02 30.70    NA    NA
> system.time(for(i in 1:1e6) rep.default(1, 5))
[1] 19.46  0.00 19.67    NA    NA
> system.time(for(i in 1:1e6) rep.int(1, 5))
[1] 1.97 0.00 1.97   NA   NA

on another (32-bit Windows).

This may help explain why we have .int functions (e.g. rep.int and from 
2.4.0, sort.int).

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-devel mailing list