4 ms·
In some languages (e.g., R), the performance penalty for such code is enormous.
by gbrown 10y ago
In some languages (e.g., R), the performance penalty for such code is enormous.
- disgruntledphd2 10y agoYes and no. While typically vectorised code will run faster than a loop (because its a loop in C) i.e. rowSums is faster than a typical apply call, there actually isn't that much difference between a for loop over columns to sum and apply over columns to sum. The real performance penalty is growing objects as you go. If you preallocate a list or vector of the appropriate length, then there typically isn't much difference. That being said, you'll pry lapply(x, fun) from my cold dead hands (lapply(x, function (x) g(f(x)) is even better :) )