title: Speeding up my for loop that builds vector element by element by comparing with element above
body: <p>i'm building a little simulation in R to do some analysis on. Building a vector element by element by comparing it to the element above and another vector. Picking the largest of the two and adding a number.</p>
  
  <p>However it is very slow because of the for loop. I'm hoping there are ways to speed this up(vectorizing? apply? aggregate?)</p>
  
  <p>This is the part that needs fixing:</p>
  
  <pre><code>number_of_jobs = 100000 
  arrival_rate = 1 
  service_rate = 2 
  job_in = cumsum(rexp(number_of_jobs, rate = arrival_rate)) 
  servicetimes = rexp(number_of_jobs, rate = service_rate) #first job has no waiting time 
  
  #initialize first job
  job_out = vector()
  job_out[1] = job_in[1] + servicetimes[1] 
  #loop over all remaining jobs.
  for (i in 2:number_of_jobs){
      job_out[i] = max(job_out[i-1], job_in[i])  + servicetimes[i]
  }
  </code></pre>