3 ms·
Doing Python or R right means using vector/matrix operations in e.g. numpy. Those will be faster. Just think twice before using a for loop. Most of the time it
by fifilura 2y ago
Doing Python or R right means using vector/matrix operations in e.g. numpy. Those will be faster.
Just think twice before using a for loop. Most of the time it is not needed.
- MooseBurger 2y agoI disagree w.r.t. python. You shouldn't need a numerical computing library to do a for loop generally.
- deleted 2y ago[deleted]
- fifilura 2y agoKick the habit. For loops are what "goto" was 20 years ago. Error prone and not possible to abstract for example for parallel execution.
- hatthew 2y agoI think the comparison to goto is a little extreme, but actually not too crazy. I could see a future where the accepted best practice is to use parallelized operations for everything, and only use sequential loops in cases where the underlying concept is inherently sequential (gradient descent, iterative hashing, etc.).
- nickforr 2y agoIf you write the R code using vectorised operations it’s significant orders of magnitude faster (148 seconds to 1.5 milliseconds on posit.cloud). The nested loop becomes: j <- sum((seq_len(100000) - 1) %% u) a <- a + j + r