简体   繁体   中英

Does a long variable name slow performance in r?

For example would the following two run at different speeds if there were in a function that run millions of trials.

the.name.of.a.random.variable.in.r <- some.value

variable <- some.value

Then expanding on this how does this relate to names of functions.

My simple benchmarking experiment would indicate that it does not matter:

the.name.of.a.random.variable.in.r <- 1:1000
the.name.of.a.random.variable.in.r.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <- 1:1000

variable <- 1:1000

microbenchmark::microbenchmark(long=sum(the.name.of.a.random.variable.in.r), 
                               short=sum(variable), 
                               verylong=sum(the.name.of.a.random.variable.in.r.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa))

# Unit: nanoseconds
# expr     min  lq    mean median     uq   max neval cld
# long     970 987 1147.48 1036.5 1057.5 10468   100   a
# short    969 985 1038.82 1030.5 1053.5  1841   100   a
# verylong 968 988 1070.16 1036.0 1062.5  3961   100   a

The R interpreter uses symbol objects to store names. Code statements are parsed to expression objects which are 'syntactically correct collections of tokens', to quote the definition (see the R language symbol definitions and the subsequent topic on expressions for further information). In short, a lengthy variable name is not used directly but is converted to a symbol object which is used when parsing expressions that subsequently may (or may not) be evaluated. Accordingly, the length of the name of a variable is not relevant to performance as R's parser uses a token for the internal symbol object rather than the symbol's character name, which is simply an attribute of the symbol.

The R language definitions cover argument matching in function calls, which is a fairly complex process described in this section of the on-line manual . In brief, symbol lookups are used when matching named arguments in a function call, and the performance cost of determining which symbol corresponds to which name is not likely to be a hindrance to overall performance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM