[Rd] performance of nchar

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue Oct 25 19:34:48 CEST 2005


On Tue, 25 Oct 2005, Jennifer Lai wrote:

> Hi,
>    Is nchar function knowingly slow in R? I'm doing some string
> formatting that requires multiple call to nchar, and nchar seems to be
> very slow.

Is 70 usec 'very slow'?

Both nchar and sprintf are vectorized, so why not make use of that?

> str <- rep("foo", 10000)
> system.time(nchar(str))
[1] 0 0 0 0 0
> system.time(for(i in 1:10000) nchar(str[i]))
[1] 0.98 0.00 0.98 0.00 0.00

Not making use of vectorization is known to be slow.
In this case it is match.arg() taking the time:

> str <- rep("foo", 10000)
> Rprof()
> system.time(for(i in 1:10000) nchar(str[i]))
[1] 0.92 0.00 0.91 0.00 0.00
> Rprof(NULL)
> summaryRprof()
...
$by.total
                total.time total.pct self.time self.pct
"system.time"        0.94     100.0      0.00      0.0
"eval"               0.88      93.6      0.08      8.5
"nchar"              0.86      91.5      0.04      4.3
"match.arg"          0.82      87.2      0.04      4.3
"deparse"            0.58      61.7      0.12     12.8

so you could write a stripped-down version if really needed.

>
> Experiment 1, pass nchar inside sprintf, and it takes 0.7 seconds
> > system.time(for (i in 1:10000)
> + str = sprintf('0005%020d', nchar(op))
> + )[3]
> [1] 0.7
>
> Experiment 2, get the length of op separately using nchar, and then pass
> the value to sprintf.
> > len = nchar(op)
> > system.time(for (i in 1:10000)
> + str = sprintf('0005%020d', len)
> + )[3]
> [1] 0.03
>
> Experiment 3, time nchar for 10000 iterations
> > system.time(for (i in 1:10000)
> + nchar(op)
> + )[3]
> [1] 0.66
>
> Is there any faster way of getting the length of string in R?
>
> Thank you in advance for your help!
>
>
> Sincerely,
> Jennifer
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>
>

-- 
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