Displaying 5 results from an estimated 5 matches for "s_diff2".
2024 Jun 16
1
slowness when I use a list comprehension
This can be vectorized. Try
ix <- seq_along(vec2)
S_diff2 <- sapply(seq_len(N1-(N2-1)*ratio_sampling), \(j)
sum((vec1[(ix-1)*ratio_sampling+j] - vec2[ix])**2))
On Sun, Jun 16, 2024 at 11:27?AM Laurent Rhelp <laurentRHelp at free.fr> wrote:
>
> Dear RHelp-list,
>
> I try to use the package comprehenr to replace a for loop by a lis...
2024 Jun 16
2
slowness when I use a list comprehension
...(tictoc)
library(comprehenr)
ratio_sampling <- 500
## size of the first serie
N1 <- 70000
## size of the second serie
N2 <- 100
## mock data
set.seed(123)
vec1 <- rnorm(N1)
vec2 <- runif(N2)
## 1. with the "for" loops
## the square differences will be stored in a vector
S_diff2 <- numeric((N1-(N2-1)*ratio_sampling))
tic()
for( j in 1:length(S_diff2)){
? sum_squares <- 0
? for( i in 1:length(vec2)){
??? sum_squares = sum_squares + ((vec1[(i-1)*ratio_sampling+j] -
vec2[i])**2)
? }
? S_diff2[j] <- sum_squares
}
toc()
## 0.22 sec elapsed
which.max(S_diff2)
## 7...
2024 Jun 16
1
slowness when I use a list comprehension
...(tictoc)
library(comprehenr)
ratio_sampling <- 500
## size of the first serie
N1 <- 70000
## size of the second serie
N2 <- 100
## mock data
set.seed(123)
vec1 <- rnorm(N1)
vec2 <- runif(N2)
## 1. with the "for" loops
## the square differences will be stored in a vector
S_diff2 <- numeric((N1-(N2-1)*ratio_sampling))
tic()
for( j in 1:length(S_diff2)){
sum_squares <- 0
for( i in 1:length(vec2)){
sum_squares = sum_squares + ((vec1[(i-1)*ratio_sampling+j] -
vec2[i])**2)
}
S_diff2[j] <- sum_squares
}
toc()
## 0.22 sec elapsed
which.max(S_diff2)
## 7...
2024 Jun 16
1
slowness when I use a list comprehension
...t manipulations from the script.
library(microbenchmark)
ratio_sampling <- 500
## size of the first serie
N1 <- 70000
## size of the second serie
N2 <- 100
## mock data
set.seed(123)
vec1 <- rnorm(N1)
vec2 <- runif(N2)
dloop <- function( N1, M2, ratio_sampling, vec1, vec2 ) {
S_diff2 <- numeric(
N1-(N2-1)*ratio_sampling
)
for( j in 1:length(S_diff2) ) {
sum_squares <- 0
for( i in 1:length(vec2)){
sum_squares <- (
sum_squares
+ (
vec1[ (i-1)*ratio_sampling+j ]
- vec2[i]
)**2
)
}
S_diff2[j] &...
2024 Jun 16
1
slowness when I use a list comprehension
...t manipulations from the
script.
library(microbenchmark)
ratio_sampling <- 500
## size of the first serie
N1 <- 70000
## size of the second serie
N2 <- 100
## mock data
set.seed(123)
vec1 <- rnorm(N1)
vec2 <- runif(N2)
dloop <- function( N1, M2, ratio_sampling, vec1, vec2 ) {
S_diff2 <- numeric(
N1-(N2-1)*ratio_sampling
)
for( j in 1:length(S_diff2) ) {
sum_squares <- 0
for( i in 1:length(vec2)){
sum_squares <- (
sum_squares
+ (
vec1[ (i-1)*ratio_sampling+j ]
- vec2[i]
)**2
)
}
S_diff2[j] &...