An Eager Avocado

Eager Avocado

I give myself very good advice, but I very seldom follow it.

String manipulation efficiency

,

String concatenation can be done with paste, paste0, sprintf, or .Internal(sprintf).

sprintf vs paste

There’s not much difference in performance among these options.

x = 3
y = 5.7
microbenchmark(paste0("The value of x is ", x, " and y is ", y, "."),
               sprintf("The value of x is %s and y is %s.", x, y),times = 1000, unit = 's')
## Unit: seconds
##                                                   expr       min
##  paste0("The value of x is ", x, " and y is ", y, ".") 3.797e-06
##     sprintf("The value of x is %s and y is %s.", x, y) 3.804e-06
##         lq         mean     median         uq        max neval cld
##  4.014e-06 4.481342e-06 4.1710e-06 4.3995e-06 4.9731e-05  1000   a
##  3.956e-06 4.368050e-06 4.0965e-06 4.3070e-06 1.9968e-05  1000   a

sprintf vs .Internal(sprintf)

Replacing sprintf by .Internal(sprintf) results in a small gain in performance, which can turn out to be significant if the operation is repeated a lot.

sprintf("Annie eats %d apples in %.2f minutes, %d oranges in %.2f minutes", 4,5.2,7,7.3)
## [1] "Annie eats 4 apples in 5.20 minutes, 7 oranges in 7.30 minutes"
.Internal(sprintf("Annie eats %d apples in %.2f minutes, %d oranges in %.2f minutes", 4,5.2,7,7.3))
## [1] "Annie eats 4 apples in 5.20 minutes, 7 oranges in 7.30 minutes"
sprintf("%d-%d : %.2f", 1:20, 4:23, 5.4)
##  [1] "1-4 : 5.40"   "2-5 : 5.40"   "3-6 : 5.40"   "4-7 : 5.40"  
##  [5] "5-8 : 5.40"   "6-9 : 5.40"   "7-10 : 5.40"  "8-11 : 5.40" 
##  [9] "9-12 : 5.40"  "10-13 : 5.40" "11-14 : 5.40" "12-15 : 5.40"
## [13] "13-16 : 5.40" "14-17 : 5.40" "15-18 : 5.40" "16-19 : 5.40"
## [17] "17-20 : 5.40" "18-21 : 5.40" "19-22 : 5.40" "20-23 : 5.40"
.Internal(sprintf("%d-%d : %.2f", 1:20, 4:23, 5.4))
##  [1] "1-4 : 5.40"   "2-5 : 5.40"   "3-6 : 5.40"   "4-7 : 5.40"  
##  [5] "5-8 : 5.40"   "6-9 : 5.40"   "7-10 : 5.40"  "8-11 : 5.40" 
##  [9] "9-12 : 5.40"  "10-13 : 5.40" "11-14 : 5.40" "12-15 : 5.40"
## [13] "13-16 : 5.40" "14-17 : 5.40" "15-18 : 5.40" "16-19 : 5.40"
## [17] "17-20 : 5.40" "18-21 : 5.40" "19-22 : 5.40" "20-23 : 5.40"
## Unit: microseconds
##                                                                                                         expr
##             sprintf("Annie eats %d apples in %.2f minutes, %d oranges in %.2f minutes",      4, 5.2, 7, 7.3)
##  .Internal(sprintf("Annie eats %d apples in %.2f minutes, %d oranges in %.2f minutes",      4, 5.2, 7, 7.3))
##                                                                     sprintf("%d-%d : %.2f", 1:20, 4:23, 5.4)
##                                                          .Internal(sprintf("%d-%d : %.2f", 1:20, 4:23, 5.4))
##     min      lq     mean  median      uq    max neval cld
##   2.077  2.2620  2.60793  2.4165  2.6700 16.791   100  a 
##   1.617  1.6555  2.18368  1.7185  1.7895 17.429   100  a 
##  16.906 17.1305 18.04683 17.3845 17.8390 33.835   100   b
##  16.335 16.5585 17.28377 16.7080 17.0795 31.896   100   b