Search code examples
loopsfor-looptestingjuliabenchmarking

Time average of set of trials in Julia?


In Julia, I have a function that times each iteration of a for loop using @elapsed macro, shown below. Instead of timing each iteration, I would like to perform each "kth" iteration 100 times, and record the average of those 100 trials as the "time" of each iteration, stored in time_vector array. Is there any way to modify this function to accomplish this?

 function timeloop2(k)
    time_vector = zeros(k)
    for x = 1:k
         #for each iteration k, do 100 trials and compute "average" of 100 trials?
         #how to store each average of 100 trials in time_vector?
         t = @elapsed begin    
             L₁=rand(10,k)                
             L₂=rand(k,10)
             L₁*L₂
         end 
         time_vector[x] = t
     end 
     time_vector  
  end 

Solution

  • If I've understood correctly, this should work for this specific case

    function timeloop2(k)
        time_vector = zeros(k)
        for x = 1:k
             t = @elapsed for j in 1:100
                 L₁=rand(10,k)                
                 L₂=rand(k,10)
                 L₁*L₂
             end
             time_vector[x] = t/100
         end 
         time_vector  
      end 
    

    However, perhaps you would be interested in using BenchmarkTools for benchmarking in general.