35 Profiling

35.1 Has somebody already solved the problem?

  1. Q: What are faster alternatives to lm? Which are specifically designed to work with larger datasets?

    A: Within the Cran task view for HighPerformanceComputing we can find for example the speedglm package and its speedlm() function. We might not gain any performance improvements on small datasets:

    However on bigger datasets it can make a difference:

    For further speed improvements, you might consider switching your linear algebra libraries as stated in ?speedglm::speedlm

    The functions of class ‘speedlm’ may speed up the fitting of LMs to large data sets. High performances can be obtained especially if R is linked against an optimized BLAS, such as ATLAS.

    Note that there are many other opportunities mentioned in the task view, also some that make it possible to handle data which is not in memory.

    When it comes to pure speed a quick google search on r fastest lm provides a stackoverflow thread where someone already solved this problem for us…

  2. Q: What package implements a version of match() that’s faster for repeated lookups? How much faster is it?

    A: Again google gives a good recommendation for the search term r faster match:

    On my laptop fastmatch::fmatch() is around 25 times as fast as match().

  3. Q: List four functions (not just those in base R) that convert a string into a date time object. What are their strengths and weaknesses?

    A: At least these functions will do the trick: as.POSIXct(), as.POSIXlt(), strftime(), strptime(), lubridate::ymd_hms(). There might also be some in the timeseries packages xts or zoo and in anytime. An update on this will follow.

  4. Q: How many different ways can you compute a 1d density estimate in R?

    A: According to Deng and Wickham (2011) density estimation is implemented in over 20 R packages.

  5. Q: Which packages provide the ability to compute a rolling mean?

    A: Again google r rolling mean provides us with enough information and guides our attention on solutions in the following packages:
    • zoo
    • TTR
    • RcppRoll
    • caTools

    Note that an exhaustive example on how to create a rolling mean function is provided in the textbook.

  6. Q: What are the alternatives to optim()?

    A: Depending on the use case a lot of different options might be considered. For a general overview we would suggest the corresponding taskview on Optimization.

35.2 Do as little as possible

  1. Q: How do the results change if you compare mean() and mean.default() on 10,000 observations, rather than on 100?

    A: We start with 100 observations as shown in the textbook:

    In case of 10,000 observations we can observe that using mean.default() preserves only a small advantage over the use of mean():

    When using even more observations - like in the next lines - it seems that mean.default() doesn’t preserve anymore any advantage at all:

  2. Q: The following code provides an alternative implementation of rowSums(). Why is it faster for this input?

    A:

  3. Q: What’s the difference between rowSums() and .rowSums()?

    A: .rowSums() is defined as

    this means, that the internal .rowSums() function is called via .Internal().

    .Internal performs a call to an internal code which is built in to the R interpreter.

    The internal .rowSums() is a complete different function than the “normal” rowSums() function.

    Of course (since they have the same name) in this case these functions are heavily related with each other: If we look into the source code of rowSums(), we see that it is a wrapper around the internal .rowSums(). Just some input checkings, conversions and the special cases (complex numbers) are added:

  4. Q: Make a faster version of chisq.test() that only computes the chi-square test statistic when the input is two numeric vectors with no missing values. You can try simplifying chisq.test() or by coding from the mathematical definition.

    A: Since chisq.test() has a relatively long source code, we try a new implementation from scratch:

    We check if our new implementation returns the same results

    Finally we benchmark this implementation against a compiled version of itself and the original stats::chisq.test():

  5. Q: Can you make a faster version of table() for the case of an input of two integer vectors with no missing values? Can you use it to speed up your chi-square test?

    A:

  6. Q: Imagine you want to compute the bootstrap distribution of a sample correlation using cor_df() and the data in the example below. Given that you want to run this many times, how can you make this code faster? (Hint: the function has three components that you can speed up.)

    Is there a way to vectorise this procedure?

    A: The three components (mentioned in the questions hint) are:

    1. sampling of indices
    2. subsetting the data frame/conversion to matrix (or vector input)
    3. the cor() function itself.

    Since a run of lineprof like shown in the textbook suggests that as.matrix() within the cor() function is the biggest bottleneck, we start with that:

    Remember the outgoing function:

    First we want to optimise the second line (without attention to the cor() function itself). Therefore we exclude the first line from our optimisation approaches and define i within the global environment:

    Then we define our approaches, check that their behaviour is correct and do the first benchmark:

    According to the resulting medians, lower and upper quartiles of our benchmark all three new versions seem to provide more or less the same speed benefit (note that the maximum and mean can vary a lot for these approaches). Since the second version is most similar to the code we started, we implement this line into a second version of cor_df() (if this sounds too arbitrary, note that in the final solution we will come back to the vector input version anyway) and do a benchmark to get the overall speedup:

    Now we can focus on a speedup for the random generation of indices. (Note that a run of linepfrof suggests to optimize cbind(). However, after rewriting cor() to a version that only works with vector input, this step will be unnecessary anyway). We could try differnt approaches for the sequence generation within sample() (like seq(n), seq.int(n), seq_len(n), a:n) and a direct call of sample.int(). In the following, we will see, that sample.int() is always faster (since we don’t include the generation of the sequence into our benchmark). When we look into sample.int() we see that it calls two different internal sample versions depending on the input. Since in our usecase always the second version will be called, we also provide this version in our benchmark:

    The sample.int() versions give clearly the biggest improvement. Since the internal version doesn’t provide any clear improvement, but restricts the general scope of our function, we choose to implement sample.int() in a third version of cor_df() and benchmark our actual achievements:

    As a last step, we try to speedup the calculation of the pearson correlation coefficient. Since quite a lot of functionality is build into the stats::cor() function this seems like a reasonable approach. We try this by working with another cor() function from the WGCNA package and an own implementation which should give a small improvement, because we use sum(x) / length(x) instead of mean(x) for internal calculations:

    In our final benchmark, we also include compiled verions of all our attempts:

    cor_df_c <- compiler::cmpfun(cor_df)
    cor_df2_c <- compiler::cmpfun(cor_df2)
    cor_df3_c <- compiler::cmpfun(cor_df3)
    cor_df4m_c <- compiler::cmpfun(cor_df4m)
    cor_df4v_c <- compiler::cmpfun(cor_df4v)
    cor_df5_c <- compiler::cmpfun(cor_df5)
    
    microbenchmark::microbenchmark(
      cor_df(),
      cor_df2(),
      cor_df3(),
      cor_df4m(),
      cor_df4v(),
      cor_df5(),
      cor_df_c(),
      cor_df2_c(),
      cor_df3_c(),
      cor_df4m_c(),
      cor_df4v_c(),
      cor_df5_c()
    )
    #> 
    #> Unit: milliseconds
    #>          expr      min        lq      mean    median        uq       max
    #>      cor_df() 93.69474 101.58731 136.62427 105.31740 116.14679  366.4755
    #>     cor_df2() 59.74903  64.61351  98.09192  66.39092  69.73457  357.9196
    #>     cor_df3() 54.68245  57.86840  76.77177  60.14916  63.26598  343.4537
    #>    cor_df4m() 78.47800  85.12587 118.09512  88.88758  93.59687  466.6316
    #>    cor_df4v() 75.28063  82.16756 125.07651  84.95143  94.77764  429.5538
    #>     cor_df5() 50.31390  57.05252  81.47288  60.14645  64.18283  398.7600
    #>    cor_df_c() 94.04417 101.76583 132.33930 105.41177 114.30206  504.8655
    #>   cor_df2_c() 59.35191  64.44230  81.22067  67.28446  70.48400  393.6352
    #>   cor_df3_c() 54.57816  58.82453  86.79131  61.68728  65.55480  431.7059
    #>  cor_df4m_c() 77.49631  84.50715 128.58519  87.16920  91.04370 2410.4450
    #>  cor_df4v_c() 73.14337  81.98549 123.93428  85.62008  94.41818  463.7264
    #>   cor_df5_c() 51.35410  56.96752 100.64343  60.76175  66.32210  442.8767
    #>  neval
    #>    100
    #>    100
    #>    100
    #>    100
    #>    100
    #>    100
    #>    100
    #>    100
    #>    100
    #>    100
    #>    100
    #>    100
    #> 
    #> Unit: milliseconds
    #>          expr      min       lq      mean    median        uq        max
    #>      cor_df() 7.885838 9.146994 12.739322  9.844561 10.447188   83.08044
    #>     cor_df2() 1.951941 3.178821  6.101036  3.339742  3.485085   36.25404
    #>     cor_df3() 1.288831 1.943510  4.353773  2.018105  2.145119   66.74022
    #>    cor_df4m() 1.534061 2.156848  6.344015  2.243540  2.370921  234.17307
    #>    cor_df4v() 1.639997 2.271949  5.755720  2.349477  2.453214  196.49897
    #>     cor_df5() 1.281133 1.879911  6.263500  1.932696  2.064292  237.83135
    #>    cor_df_c() 7.600654 9.337239 13.613901 10.009330 10.965688   41.17146
    #>   cor_df2_c() 2.009124 2.878242  5.645224  3.298138  3.469871   34.29037
    #>   cor_df3_c() 1.312291 1.926281  4.426418  1.986948  2.094532   87.97220
    #>  cor_df4m_c() 1.548357 2.179025  3.306796  2.242991  2.355709   23.87195
    #>  cor_df4v_c() 1.669689 2.255087 27.148456  2.341962  2.490419 2263.33230
    #>   cor_df5_c() 1.284065 1.888892  3.884798  1.953774  2.078955   25.81583
    #>  neval cld
    #>    100   a
    #>    100   a
    #>    100   a
    #>    100   a
    #>    100   a
    #>    100   a
    #>    100   a
    #>    100   a
    #>    100   a
    #>    100   a
    #>    100   a
    #>    100   a

    Our final solution benefits most from the switch from data frames to vectors. Working with sample.int gives only little improvement. Reimplementing and compiling a new correlation function adds only minimal speedup.

    To trust our final result we include a last check for similar return values:

    Vectorisation of this problem seems rather difficult, since attempts of using matrix calculus, always depend on building and handling big matrices in the first place.

    We can for example rewrite our correlation function to work with matrices and build a new (vectorised) version of cor_df() on top of that

    However this still doesn’t provide any improvement over the use of lapply():

    Further improvements can be achieved using parallelisation (for example via parallel::parLapply())

35.3 Vectorise

  1. Q: The density functions, e.g., dnorm(), have a common interface. Which arguments are vectorised over? What does rnorm(10, mean = 10:1) do?

    A: We can see the interface of these functions via ?dnorm:

    dnorm(x, mean = 0, sd = 1, log = FALSE)
    pnorm(q, mean = 0, sd = 1, lower.tail = TRUE, log.p = FALSE)
    qnorm(p, mean = 0, sd = 1, lower.tail = TRUE, log.p = FALSE)
    rnorm(n, mean = 0, sd = 1).

    They are vectorised over their numeric arguments, which is always the first argument (x, q, p, n), mean and sd. Note that it’s dangerous to supply a vector to n in the rnorm() function, since the behaviour will change, when n has length 1 (like in the second part of this question).

    rnorm(10, mean = 10:1) generates ten random numbers from different normal distributions. The normal distributions differ in their means. The first has mean 10, the second has mean 9, the third mean 8 etc.

  2. Q: Compare the speed of apply(x, 1, sum) with rowSums(x) for varying sizes of x.

    A: We compare regarding different sizes for square matrices:

    The graph is a good indicator to notice, that apply() is not “vectorised for performance”.

  3. Q: How can you use crossprod() to compute a weighted sum? How much faster is it than the naive sum(x * w)?

    A: We can just give the vectors to crossprod() which converts them to row- and column-vectors and then multiplies these. The result is the dot product which is also a weighted sum.

    A benchmark of both alternatives for different vector lengths indicates, that the crossprod() variant is about 2.5 times faster than sum():