Skip to main content
tag for performance
Source Link
Toby Speight
  • 81.7k
  • 14
  • 101
  • 308

I created a function yes.seq that takes two arguments, a pattern pat and data dat, the function looks for the presence of a pattern in the data and in the same sequence.

forFor example:

dat <- letters[1:10]
dat
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"
pat <- c('a',"c","g")
 
 yes.seq(pat = pat,dat = dat)
[1] TRUE

because this sequence is in the pattern and in the same order.

"a" "b" "c" "d" "e" "f" "g" "h" "i" "j"

ifIf, for example, expandwe reverse the datepattern, then we get FALSE:

yes.seq(pat = pat,dat =  **rev(dat)**   )
[1] FALSE

Here is my function:

yes.seq <- function(pat , dat){  
  lv <- rep(F,length(pat))
  k <- 1     
  for(i in 1:length(dat)){        
            if(dat[i] == pat[k]) 
              {
              lv[k] <- TRUE
              k <- k+1 
              }       
    if(k==length(pat)+1) break
  }
  return(  all(lv)   )
}

The problem is that I am not satisfied with the speed of this function, can. Can you help me with that?

I created a function yes.seq that takes two arguments, a pattern pat and data dat, the function looks for the presence of a pattern in the data and in the same sequence

for example

dat <- letters[1:10]
dat
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"
pat <- c('a',"c","g")
 
 yes.seq(pat = pat,dat = dat)
[1] TRUE

because this sequence is in the pattern and in the same order

"a" "b" "c" "d" "e" "f" "g" "h" "i" "j"

if, for example, expand the date, then we get FALSE

yes.seq(pat = pat,dat =  **rev(dat)**   )
[1] FALSE

Here is my function

yes.seq <- function(pat , dat){  
  lv <- rep(F,length(pat))
  k <- 1     
  for(i in 1:length(dat)){        
            if(dat[i] == pat[k]) 
              {
              lv[k] <- TRUE
              k <- k+1 
              }       
    if(k==length(pat)+1) break
  }
  return(  all(lv)   )
}

The problem is that I am not satisfied with the speed of this function, can you help me with that?

I created a function yes.seq that takes two arguments, a pattern pat and data dat, the function looks for the presence of a pattern in the data and in the same sequence.

For example:

dat <- letters[1:10]
dat
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"
pat <- c('a',"c","g")
 
 yes.seq(pat = pat,dat = dat)
[1] TRUE

because this sequence is in the pattern and in the same order.

"a" "b" "c" "d" "e" "f" "g" "h" "i" "j"

If, for example, we reverse the pattern, then we get FALSE:

yes.seq(pat = pat,dat =  **rev(dat)**   )
[1] FALSE

Here is my function:

yes.seq <- function(pat , dat){  
  lv <- rep(F,length(pat))
  k <- 1     
  for(i in 1:length(dat)){        
            if(dat[i] == pat[k]) 
              {
              lv[k] <- TRUE
              k <- k+1 
              }       
    if(k==length(pat)+1) break
  }
  return(  all(lv)   )
}

I am not satisfied with the speed of this function. Can you help me with that?

Source Link
mr.T
  • 103
  • 2

Speed up a function that checks for a sequence

I created a function yes.seq that takes two arguments, a pattern pat and data dat, the function looks for the presence of a pattern in the data and in the same sequence

for example

dat <- letters[1:10]
dat
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"
pat <- c('a',"c","g")
 
 yes.seq(pat = pat,dat = dat)
[1] TRUE

because this sequence is in the pattern and in the same order

"a" "b" "c" "d" "e" "f" "g" "h" "i" "j"

if, for example, expand the date, then we get FALSE

yes.seq(pat = pat,dat =  **rev(dat)**   )
[1] FALSE

Here is my function

yes.seq <- function(pat , dat){  
  lv <- rep(F,length(pat))
  k <- 1     
  for(i in 1:length(dat)){        
            if(dat[i] == pat[k]) 
              {
              lv[k] <- TRUE
              k <- k+1 
              }       
    if(k==length(pat)+1) break
  }
  return(  all(lv)   )
}

The problem is that I am not satisfied with the speed of this function, can you help me with that?