loopy: Arrgh! copy-and-append

Description Usage Arguments Details Examples

Description

A very common problem with severe performance consequences is the ‘copy-and-append’ pattern. Save me from this craziness, and document that even the simple example below scales terribly. Do this in three steps. (1) Write a for-loop but pre-allocate the result vector and fill it in. (2) Use an sapply() to avoid thinking about pre-allocation. (3) implement the obvious vectorization.

Usage

1
loopy(n)

Arguments

n

a scalar (length 1) numeric non-negative vector indicating the number of a number will be

Details

Verify using identical() that each solution returns the same result for each implementation.

Verify the inefficiency of copy-and-append using microbenchmark to evaluate the original function with different values of n and plotting the result.

Verify the performance gain for a single value of n across the three implementations.

Find one StackOverflow question where the solution to a significant performance bottleneck is to avoid copy-and-append.

Examples

1
loopy(5)

Bioconductor/DDayChallenge documentation built on May 6, 2019, 7:49 a.m.