anna
2010-Feb-12 18:29 UTC
[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.
Bert Gunter
2010-Feb-12 18:56 UTC
[R] Code working but too slow, any idea for how to speed it up ?(no loop in it)
?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.
Ravi Varadhan
2010-Feb-12 19:11 UTC
[R] Code working but too slow, any idea for how to speed it up ?(no loop in it)
First of all, please send a reproducible code. Your code is wrong and it does not execute: naming a function called "function" is a bad idea. Ravi. ____________________________________________________________________ Ravi Varadhan, Ph.D. Assistant Professor, Division of Geriatric Medicine and Gerontology School of Medicine Johns Hopkins University Ph. (410) 502-2619 email: rvaradhan at jhmi.edu ----- Original Message ----- From: anna <lippelanna24 at hotmail.com> Date: Friday, February 12, 2010 1:30 pm Subject: [R] Code working but too slow, any idea for how to speed it up ?(no loop in it) To: r-help at r-project.org> 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: > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > > PLEASE do read the posting guide > and provide commented, minimal, self-contained, reproducible code.
Dan Davison
2010-Feb-12 21:48 UTC
[R] Code working but too slow, any idea for how to speed it up ?(no loop in it)
anna <lippelanna24 <at> hotmail.com> writes:> > 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.Could you provide some example code creating matrices mat1 and mat2 which have exactly the same structure as the mat1 and mat2 you are using. We don't really want your exact data, but just toy matrices that have exactly the same form as your data matrices. Without that your question's hard to answer as we can't try out your code. failing that, please post the output of str(mat1) and str(mat2).