[R] how to speed up the process of R
Joshua Wiley
jwiley.psych at gmail.com
Fri Mar 16 05:52:12 CET 2012
I think you would also get a speedup by instantiating your list at
size. For example:
a <- vector("list", 100000)
so that memory is already set aside and there is not incremental
copying and increasing (although R may be clever and manage to avoid
this for you). It is really rather difficult to give any meaningful
advice without some details about your situation. As much as
possible, you want to rely on vectorized code. It may not be possible
to vectorize all of your code, and there are still some tricks you can
do with loops to get out a bit more speed, but often the biggest
improvements come by bypassing the loop altogether. I gave a trivial
example of how to do this in your prior example, but it nevertheless
shows the phenomenal speed difference and (hopefully) motivates why
you want to do that whenever possible. part of the trick is to think
that way and see how your code (which you may be thinking of in terms
of a loop right now) can be recast into vector operations where
possible.
Modest gains are also possible by instantiating objects at their final
size and choosing efficient storage modes (e.g., integer if possible
over numeric, etc.). If it is a function, byte compiling may help,
see cmpfun() in compiler. You can also use things like Rprof() to see
where your bottlenecks really are so you spend your time optimizing
the slowest code. If you have an algorithm at the core that is
running slowly, you could use something like the Rcpp & inline
packages to rewrite just that piece in C++ and then call it from R so
that the heaviest lifting is done in a fast, compiled language, but
you still get the ease and flexibility of R for the rest of it.
Cheers,
Josh
On Thu, Mar 15, 2012 at 9:41 PM, Joshua Wiley <jwiley.psych at gmail.com> wrote:
> Hi,
>
> Compare this:
>
> a<-list()
> system.time(for(i in 1:100000){
> a[[i]]<-i*1000
> })
>
> system.time(a2 <- as.vector(1000 * (1:100000), "list"))
> all.equal(a, a2)
>
> ## > system.time(for(i in 1:100000){
> ## + a[[i]]<-i*1000
> ## + })
> ## user system elapsed
> ## 28.66 0.02 28.84
> ## > system.time(a2 <- as.vector(1000 * (1:100000), "list"))
> ## user system elapsed
> ## 0 0 0
> ## > all.equal(a, a2)
> ## [1] TRUE
>
> Cheers,
>
> Josh
>
> On Thu, Mar 15, 2012 at 5:00 PM, mrzung <mrzung46 at gmail.com> wrote:
>> hi
>> I'm simulating some experiment by "for" function.
>> The problem is that it takes too much time.
>>
>> for example,
>>
>> a<-list()
>>
>> for(i in 1:100000000){
>> a[[i]]<-i*1000
>> }
>>
>> is there anyway to speed up the process?
>> I heard there is solution but i don't have idea.
>>
>> Can anyone help me?
>>
>> --
>> View this message in context: http://r.789695.n4.nabble.com/how-to-speed-up-the-process-of-R-tp4476794p4476794.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>
>
>
> --
> Joshua Wiley
> Ph.D. Student, Health Psychology
> Programmer Analyst II, Statistical Consulting Group
> University of California, Los Angeles
> https://joshuawiley.com/
--
Joshua Wiley
Ph.D. Student, Health Psychology
Programmer Analyst II, Statistical Consulting Group
University of California, Los Angeles
https://joshuawiley.com/
More information about the R-help
mailing list