Skip to content
Prev 305499 / 398506 Next

please comment on my function

First thing to do is to run Rprof and see where the time is going;
here it is from my computer:

                      self.time self.pct total.time total.pct
tolower                    4.42    39.46       4.42     39.46
sub                        3.56    31.79       3.56     31.79
nchar                      1.54    13.75       1.54     13.75
canonicalize.language      0.62     5.54      11.14     99.46
!=                         0.52     4.64       0.52      4.64
==                         0.26     2.32       0.26      2.32
&                          0.22     1.96       0.22      1.96
gc                         0.06     0.54       0.06      0.54

more than half the time is in 'tolower' and 'nchar', so it is not all
'sub's problem.

This version runs a little faster since it does not need the 'tolower':

canonicalize.language <- function (s) {
  # s <- tolower(s)
  long <- nchar(s) == 5
  s[long] <- sub("^([[:alpha:]]{2})[-_][[:alpha:]]{2}$","\\1",s[long])
  s[nchar(s) != 2 & s != "c"] <- "unknown"
  s
}
On Fri, Sep 14, 2012 at 12:30 PM, Sam Steingold <sds at gnu.org> wrote: