Jon Clayden
2009-Aug-11 16:51 UTC
[Rd] readBin() arg check has unnecessary overhead (patch included)
Dear all, The version of readBin() in R-devel includes a use of match(), through `%in%`, which can affect its performance significantly. By using primitives instead of the rather expensive call to match(), I reduce the time spent inside readBin() by more than 30% in some of my code (part of the tractor.base package). A simple patch that does this is given below. This passes "make check-devel" fine, and I don't see that it could produce unexpected behaviour -- though I may, of course, be wrong. Regards, Jon --- R-devel/src/library/base/R/connections.R 2009-08-07 01:52:16.000000000 +0100 +++ R-devel-mod/src/library/base/R/connections.R 2009-08-11 16:22:30.000000000 +0100 @@ -193,6 +193,6 @@ swap <- endian != .Platform$endian if(!is.character(what) || length(what) != 1L - || !(what %in% c("numeric", "double", "integer", "int", "logical", - "complex", "character", "raw"))) + || !any(what == c("numeric", "double", "integer", "int", "logical", + "complex", "character", "raw"))) what <- typeof(what) .Internal(readBin(con, what, n, size, signed, swap)) -- Jonathan D. Clayden, Ph.D. Research Fellow Radiology and Physics Unit UCL Institute of Child Health 30 Guilford Street LONDON WC1N 1EH United Kingdom t | +44 (0)20 7905 2708 f | +44 (0)20 7905 2358 w | www.homepages.ucl.ac.uk/~sejjjd2/ w | www.diffusion-mri.org.uk/people/1
Martin Maechler
2009-Aug-12 12:57 UTC
[Rd] readBin() arg check has unnecessary overhead (patch included)
>>>>> Jon Clayden <jon.clayden at gmail.com> >>>>> on Tue, 11 Aug 2009 17:51:46 +0100 writes:> Dear all, > The version of readBin() in R-devel includes a use of match(), through > `%in%`, which can affect its performance significantly. By using > primitives instead of the rather expensive call to match(), I reduce > the time spent inside readBin() by more than 30% in some of my code > (part of the tractor.base package). A simple patch that does this is > given below. This passes "make check-devel" fine, and I don't see that > it could produce unexpected behaviour -- though I may, of course, be > wrong. actually, %in% is liked by programmeRs for its inherent robustness combined with "expressiveness" (<-> readability) inspite of its potential efficiency loss wrt to '==' ... and indeed, your patch fails in one case where the original code works: readBin(., NA_character_, ...) However that case can also be checked explicitly, and I will implement the corresponding patch. Martin Maechler, ETH Zurich > Regards, > Jon > --- R-devel/src/library/base/R/connections.R 2009-08-07 01:52:16.000000000 +0100 > +++ R-devel-mod/src/library/base/R/connections.R 2009-08-11 > 16:22:30.000000000 +0100 > @@ -193,6 +193,6 @@ > swap <- endian != .Platform$endian > if(!is.character(what) || length(what) != 1L > - || !(what %in% c("numeric", "double", "integer", "int", "logical", > - "complex", "character", "raw"))) > + || !any(what == c("numeric", "double", "integer", "int", "logical", > + "complex", "character", "raw"))) > what <- typeof(what) > .Internal(readBin(con, what, n, size, signed, swap)) > -- > Jonathan D. Clayden, Ph.D. > Research Fellow > Radiology and Physics Unit > UCL Institute of Child Health > 30 Guilford Street > LONDON WC1N 1EH > United Kingdom > t | +44 (0)20 7905 2708 > f | +44 (0)20 7905 2358 > w | www.homepages.ucl.ac.uk/~sejjjd2/ > w | www.diffusion-mri.org.uk/people/1 > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel
Apparently Analagous Threads
- Capturing all warnings (with messages)
- R --interactive and readline() creates infinite loop
- Creating an environment with attributes in a package
- New package for medical image manipulation: tractor.base
- New package for medical image manipulation: tractor.base