Displaying 5 results from an estimated 5 matches for "ratio_sampl".
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 list
> comprehension.
>
> I wrote t...
2024 Jun 16
2
slowness when I use a list comprehension
...use it is very
slower compared to the for loops. May you please explain to me why the
list comprehension is slower in my case.
Here is my example. I do the calculation of the square difference
between the values of two vectors vec1 and vec2, the ratio sampling
between vec1 and vec2 is equal to ratio_sampling. I have to use only the
500th value of the first serie before doing the difference with the
value of the second serie (vec2).
Thank you
Best regards
Laurent
library(tictoc)
library(comprehenr)
ratio_sampling <- 500
## size of the first serie
N1 <- 70000
## size of the second serie
N...
2024 Jun 16
1
slowness when I use a list comprehension
...use it is very
slower compared to the for loops. May you please explain to me why the
list comprehension is slower in my case.
Here is my example. I do the calculation of the square difference
between the values of two vectors vec1 and vec2, the ratio sampling
between vec1 and vec2 is equal to ratio_sampling. I have to use only the
500th value of the first serie before doing the difference with the
value of the second serie (vec2).
Thank you
Best regards
Laurent
library(tictoc)
library(comprehenr)
ratio_sampling <- 500
## size of the first serie
N1 <- 70000
## size of the second serie
N...
2024 Jun 16
1
slowness when I use a list comprehension
...ugh such thinking speeds up Python also.
A key step in that direction is to stop using lists directly for numeric calculations... use them to manage numeric vactors. In some cases you can switch to matrices or arrays to remove even more list 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:...
2024 Jun 16
1
slowness when I use a list comprehension
...ugh such thinking speeds up
Python also.
A key step in that direction is to stop using lists directly for numeric
calculations... use them to manage numeric vactors. In some cases you can
switch to matrices or arrays to remove even more list 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:...