# Thread Subject: timing a loop

 Subject: timing a loop From: anyani himura Date: 5 May, 2012 19:30:16 Message: 1 of 3 how do i time this loop A=randn(500); for i=1:500      for j=1:500    B(i,j)=sum(A(:,i).*A(:,j));    end end i tried the following with tic toc but i get an enormous lines of timing. how do i get the single time taken for the whole process? A=randn(500); for i=1:500      for j=1:500   tic; B(i,j)=sum(A(:,i).*A(:,j)); toc    end end Elapsed time is 0.000042 seconds. Elapsed time is 0.000005 seconds. Elapsed time is 0.000004 seconds. Elapsed time is 0.000004 seconds. ... ... ... Thanks
 Subject: timing a loop From: Nasser M. Abbasi Date: 5 May, 2012 19:34:43 Message: 2 of 3 On 5/5/2012 2:30 PM, anyani himura wrote: > how do i time this loop > A=randn(500); > for i=1:500 > for j=1:500 > B(i,j)=sum(A(:,i).*A(:,j)); > end > end > > i tried the following with tic toc but i get an enormous lines of timing. how do i >get the single time taken for the whole process? > > A=randn(500); > for i=1:500 > for j=1:500 > tic; B(i,j)=sum(A(:,i).*A(:,j)); toc > end > end > Elapsed time is 0.000042 seconds. > Elapsed time is 0.000005 seconds. > Elapsed time is 0.000004 seconds. > Elapsed time is 0.000004 seconds. > ... > ... > ... > > Thanks Just put the tic/toc pair outside the loop? ----------------------------- A=randn(500); t0=tic; for i=1:500    for j=1:500      B(i,j)=sum(A(:,i).*A(:,j));    end end toc(t0) ---------------
 Subject: timing a loop From: Steven_Lord Date: 7 May, 2012 02:34:27 Message: 3 of 3 "anyani himura" wrote in message news:jo3v48\$asf\$1@newscl01ah.mathworks.com... > how do i time this loop A=randn(500); > for i=1:500 > for j=1:500 > B(i,j)=sum(A(:,i).*A(:,j)); > end end > > i tried the following with tic toc but i get an enormous lines of timing. > how do i get the single time taken for the whole process? Don't put tic/toc around the body of the nested loops; put it around the loops as a whole. tic > A=randn(500); > for i=1:500 > for j=1:500 > B(i,j)=sum(A(:,i).*A(:,j)); > end end toc If for whatever reason you want to know how long each inner loop iteration takes, call TOC with an output. > A=randn(500); times = zeros(500, 500); > for i=1:500 > for j=1:500 tic; > B(i,j)=sum(A(:,i).*A(:,j)); times(i, j) = toc; > end end -- Steve Lord slord@mathworks.com To contact Technical Support use the Contact Us link on http://www.mathworks.com

Separated by commas
Ex.: root locus, bode

### What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.