[R] Code working but too slow, any idea for how to speed it up ?(no loop in it)

Bert Gunter gunter.berton at gene.com
Fri Feb 12 19:56:15 CET 2010


?profile 


Bert Gunter
Genentech Nonclinical Statistics

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of anna
Sent: Friday, February 12, 2010 10:29 AM
To: r-help at r-project.org
Subject: [R] Code working but too slow, any idea for how to speed it up ?(no
loop in it)


Hello my friends, 
here is a code I wrote with no loops on matrix that is taking too long (2
seconds and I call him 720 times --> 12 minutes):

mat1 and mat2 are both matrix with 103 columns and 164 rows.

sequence <- matrix(seq(1 : ncol(mat1)))

returns <- apply(sequence, 1, function, mat1= mat1, mat2 = mat2, day = 1)

function<- function(mat1, mat2, colNb, day){           
    mat1<- matrix(mat1[, colNb] )
    mat2<- matrix(mat2[, colNb])
    nbDays <- length(mat1)       
    returns <- abs(mat1[1:(nbDays - day)]) * ((as.ts(lag(mat2,
day))/as.ts(mat2))^mat1[1:(nbDays - day)] - 1)
    return(returns)
 }

I am wondering if the fact of using lag and ts is not slowering down the
code, any idea?

-----
Anna Lippel
-- 
View this message in context:
http://n4.nabble.com/Code-working-but-too-slow-any-idea-for-how-to-speed-it-
up-no-loop-in-it-tp1483110p1483110.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.



More information about the R-help mailing list