Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9271901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:44:37+00:00 2026-06-18T15:44:37+00:00

I am trying to optimize the following code. dim <- c(10000,100) m <- matrix(sample(0:10,

  • 0

I am trying to optimize the following code.

dim <- c(10000,100)

m <- matrix(sample(0:10, prod(dim), replace = TRUE), nrow = dim[1], ncol = dim[2])

system.time({

  output <- matrix(0, nrow = dim[1], ncol = dim[2])

  for (i in 1:dim[1]){        
    output[i,1] <- m[i,1]       
    for (j in 2:dim[2]){          
      output[i,j] <- output[i, j-1] * 0.5 + m[i,j]          
    }        
  }
})

Conceptually, it is quite similar to a simple cumulative sum:

system.time({

  output <- matrix(0, nrow = dim[1], ncol = dim[2])

  for (i in 1:dim[1]){       
      output[i,] <- cumsum(m[i,])        
  }
})

The problem is, the first part of the code is about 100 times slower. Is there any way to build a customized version of cumsum() that would do the trick ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T15:44:38+00:00Added an answer on June 18, 2026 at 3:44 pm

    Your case is exactly the same as generating a AR(1) model with coefficient 0.5. You can use the filter function to generate the data. filter also support higher order recursion, convolution or mixture of them(think about the ARMA model). You may have a look ofconvolve for other convolutions. Also, you could compiler your code to speed up the loop. In my code, complied loop and uncompiled loop code is about 111 and 162 times slower than filter respectively.

    library(compiler)
    library(rbenchmark)
    
    CustomCumsum<-function(x,alpha){
    out<-x[1]
    for(i in 2:length(x))
        out[i] <- out[i-1]*alpha+x[i]
    out
    }
    
    compiledCustomCumsum<-cmpfun(CustomCumsum)
    
    FilterCustomCumsum<-function(x,alpha) as.numeric(filter(x,alpha, method = "recursive"))
    
    x<-rnorm(1000)
    # Test whether they are the same
    identical(compiledCustomCumsum(x,0.5) , FilterCustomCumsum(x,0.5) )
    
    benchmark(
    CustomCumsum=CustomCumsum(x,0.5),compiledCustomCumsum=compiledCustomCumsum(x,0.5),          FilterCustomCumsum=FilterCustomCumsum(x,0.5)
    )
    

    output:

                      test replications elapsed relative user.self sys.self user.child sys.child
    2 compiledCustomCumsum          100    8.89  111.125      8.78     0.01         NA        NA
    1         CustomCumsum          100   13.02  162.750     11.84     0.50         NA        NA
    3   FilterCustomCumsum          100    0.08    1.000      0.08     0.00         NA        NA
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to optimize the following code below to avoid having to copy and
I'm trying to optimize the following. The code bellow does this : If a
I'm trying to optimize a procedure that has code like the following: CREATE TABLE
Following code is simplified version of the code that I am trying to optimize.
I am trying some way to optimize following sql statement: exe_sql DELETE FROM tblEvent_type
I'm trying to optimize my code using Neon intrinsics. I have a 24-bit rotation
I'm trying to optimize my C++ code. I've searched the internet on using dynamically
I have the following jQuery code below I am trying to use (I know
I'm working on a bit of code and I'm trying to optimize it as
I'm programming a big game in Java and I'm trying to optimize the code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.