A
A
A_Degteryov2020-09-08 21:08:37
R
A_Degteryov, 2020-09-08 21:08:37

What is the easiest way to radically speed up R code?

I am not a programmer, but a scientist, I use R to develop new algorithms and conduct computational experiments. In terms of the speed of translating ideas into ready-made tools, the preparation of some non-standard scientific graphics, R is simply excellent and never ceases to please. But in some cases, especially with fairly massive calculations, the speed of code execution is depressing. Everything is already parallelized, the fastest designs are used, but this seems to be the limit. Cycles cannot be used, everything has to be turned into future_apply and analogues that break the brain and are extremely inconvenient for debugging, but their speed is no longer enough. Where should you move? As usual, you want to get the maximum result with minimal effort. So far I see options:

  1. Rcpp - as far as I understand, this is the most popular way when there is not enough speed. In terms of technology, there are no questions, it seems that there are step-by-step instructions and everything should work. But it’s not very attractive that you have to learn a second language and then maintain the code in two languages ​​at once. I’m not a very programmer anyway, no one has canceled the main work tasks, so most likely there won’t be time to do this closely. As far as I understand, C++ is quite complex in terms of obtaining optimal code, so it can take a significant amount of time to really achieve acceleration of critical places.
  2. Inserting Functions in Fortran. Not the most popular solution, but it seems that there are also some instructions on how to run it. Just like with C++, all the problems of learning a second language and maintaining bilingual code are there. It attracts that if you understand a little about the basics, then most likely you will quickly be able to get code that is acceptable in terms of performance, without delving into any particular programming nuances. Actually, this was and remains the main feature of the language, because of which it is used in scientific calculations. It does not attract that if problems arise, few people can be asked for advice, the community is already especially gone. Well, in general, it is difficult to perceive the study of Fortran today as an investment in the future.
  3. Julia. A beautiful modern language, much more attractive to learn than Fortran. There are no questions about the language itself, but in terms of the ecosystem, it is still frankly damp. Tips from the net and code from books often no longer work, there are still very few experienced "gurus" who can give you the right answers. Tried calling Julia from R and via XRJulia and via JuliaCall but couldn't get it to work. Perhaps after some time there will be new working ways to connect them, but so far such a bunch does not look very reliable. And switching completely to Julia means throwing away all your experience that you have with R. You can call R from Julia, it works, but this is already the most extreme case. Well, in terms of speed - in order to get a code in Julia that is somehow comparable in speed to C ++ or Fortran, you need to try very hard. It doesn't work out for everyone. As an oversized shirt, Julia might be a good option, but if you want speed here and now, then I'm not sure that this is the best thing you can think of.

What are some ideas?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Amirullo, 2021-01-10
@Amirullo

"the fastest constructions are used" - exactly? I don't know anyone who could say that about their code =)
Your optimization suggestions seem very scary to me. Isn't it easier to switch to python if you are already considering such complex decisions?
Where is your data stored? Hopefully in data.table? very, very fast. and there is room for further optimization. Extremely superficial proposal, but still, it can help.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question