Dear friend, I have to construct some recursive algorithm for which I used some for loop like: res <- vector(length=10000) res[1] = 0 for (i in 2:(10000+1)) res[i] <- res[i-1]*........some function I have noticed that this is taking too much time. Is there any way to speed up things? Thanks,
On Nov 1, 2010, at 6:12 AM, Megh Dal wrote:> Dear friend, I have to construct some recursive algorithm for which > I used some for loop like: > > res <- vector(length=10000) > res[1] = 0 > for (i in 2:(10000+1)) res[i] <- res[i-1]*........some functionres[2:(10000+1)] <- res[1:10000]*mysteryfunction(2:(10000+1)) # might work if you created res as length 10001. In the current situation you should have been getting an error that you are not telling us about when you got to the end and tried to assign to res[10001]. Anyway, it's more likely the case that the mystery function is eating up time rather than the loop.> > I have noticed that this is taking too much time. Is there any way > to speed up things? >-- David Winsemius, MD West Hartford, CT
On Mon, Nov 1, 2010 at 10:12 AM, Megh Dal <megh700004 at yahoo.com> wrote:> Dear friend, I have to construct some recursive algorithm for which I used some for loop like: > > res <- vector(length=10000) > res[1] = 0 > for (i in 2:(10000+1)) res[i] <- res[i-1]*........some function > > I have noticed that this is taking too much time. Is there any way to speed up things?You can't speed ANYTHING up until you know where the time is going. In your case, is it in "some function" or in R's loop. Try replacing 'some function' with a constant and see what happens. I suspect it will be very quick. > system.time(for (i in 2:(10000+1)) res[i] <- res[i-1]*1.12) user system elapsed 0.076 0.000 0.077 yup. So it's all in 'some function'. Next question? Barry