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

  • Home
  • SEARCH
  • 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 6774071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:44:36+00:00 2026-05-26T15:44:36+00:00

Below is a simplified version of a segment of code that I’m working on

  • 0

Below is a simplified version of a segment of code that I’m working on (a lot of additional calculations are left out to avoid confusion). It’s just a modified form of the cumsum function. I don’t want to re-invent the wheel, so does this function already exist? If not, what scheme would provide the best speed?

#Set up the data   
set.seed(1)   
junk <- rnorm(1000000)   
junk1 <- rnorm(1000000)   
cumval <- numeric(1000000)   

#Initialize the accumulator   
cumval[1] <- 1   

#Perform the modified cumsum
system.time({   
for (i in 2:1000000) cumval[i] <- junk[i] + (junk1[i] * cumval[i-1])       
})   

#Plot the result
plot(cumval, type="l")    
  • 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-05-26T15:44:36+00:00Added an answer on May 26, 2026 at 3:44 pm

    This algorithm is something that fits the compiler package perfectly!

    #Set up the data   
    set.seed(1)   
    junk <- rnorm(1000000)   
    junk1 <- rnorm(1000000)
    
    # The original code
    f <- function(junk, junk1) {
      cumval <- numeric(1000000)
      cumval[1] <- 1
      for (i in 2:1000000) cumval[i] <- junk[i] + (junk1[i] * cumval[i-1])
      cumval
    }
    system.time( f(junk, junk1) ) # 4.11 secs
    
    # Now try compiling it...
    library(compiler)
    g <- cmpfun(f)
    system.time( g(junk, junk1) ) # 0.98 secs
    

    …so it would be interesting to know if this algorithm is in any way “typical” – in that case the compiler could perhaps be even more optimized for situations like this…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a method (the code below is a simplified version) that parses small
I have below a working query that needs to be simplified. The reason is
I have a simple macro (simplified version below). At the moment it assumes that
Ok, here is setup. Note the code below is SIMPLIFIED version. PHP 5.2 ENGINE:
The code below is a simplified version of the pattern my project is using.
A simplified version of the code is posted below (white space, comments, etc. removed
I've pasted a very simplified version of my SQL query below. The problem that
Below is a simplified version of a query that I have already created. The
Below is a simplified version of a sign-up page I'm working on. When the
Below is a simplified version of code I've written to override a class method

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.