home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l460 / 3.ddi / DEMOS.DI$ / BENCH.M < prev    next >
Encoding:
Text File  |  1993-03-07  |  4.2 KB  |  217 lines

  1. function result = bench(ntimes)
  2. %BENCH    MATLAB Benchmark
  3. %
  4. %    BENCH times five different MATLAB tasks and compares the
  5. %    execution speed with the speed of several other computers.
  6. %    The problem sizes are chosen so that each task requires about
  7. %    one second on a Sun SPARC-2. The five tasks are:
  8. %    
  9. %     Loops     For loops and "zeros".   Strings and "malloc".
  10. %     LU        MATLAB's "LINPACK".      Primarily floating point.
  11. %     Sparse    Solve sparse system.     Mixed integer and floating point.
  12. %     3-D       Surf plot of "peaks".    3-D polygonal fill graphics.
  13. %     2-D       plot(fft(eye)).          2-D line drawing graphics.
  14. %
  15. %    BENCH runs each of the five tasks 10 times.
  16. %    BENCH(N) runs each of the five tasks N times.
  17. %    TIMES = BENCH(N) returns a vector with the five execution times.
  18. %    On a Sun SPARC-2, BENCH(N) would nominally produce [N N N N N].
  19. %    Smaller values are faster, larger are slower.
  20. %
  21. %    A final bar chart shows speed, which is inversely proportional to 
  22. %    time.  Here, longer bars are faster machines, shorter bars are slower.
  23. %
  24. %    CAVEAT: Fluctuations of five or 10 percent in the measured times
  25. %    of repeated runs on a single machine are not uncommon.
  26. %    Your own mileage may vary.
  27.  
  28. %    C. Moler, 1-5-92, 8-26-92, 11-23-92.
  29. %    Copyright (c) 1984-93 by The MathWorks, Inc.
  30.  
  31.  
  32. machines = str2mat( ...
  33.    'HP 720', ...
  34.    'HP 710', ...
  35.    'Iris Indigo', ...
  36.    'IBM RS/6000, 320', ...
  37.    'SPARC-2',...
  38.    'SPARC-IPC',...
  39.    'Tadpole', ...
  40.    'SPARC-1');
  41.  
  42. times = [ ...
  43.   3.05 3.87 5.36 5.29 4.48;
  44.   5.26 5.62 5.62 9.14 6.36;
  45.   6.79 11.1 8.13 10.2 9.16; 
  46.   12.8 4.49 8.78 17.6 14.5;
  47.   10.0 10.0 10.0 10.0 10.0;
  48.   15.7 21.6 19.3 19.0 19.9;
  49.   12.4 15.4 15.2 26.5 23.0;
  50.   16.6 24.1 20.3 30.5 28.6];
  51.  
  52.  
  53. help bench
  54.  
  55. clf
  56. set(gcf,'pos','default')
  57.  
  58. if nargin < 1, ntimes = 10; end
  59.  
  60. % The problem size, n, for each task has been chosen 
  61. % so that the task takes about one second on a SPARC-2.
  62.  
  63. % Loops
  64.  
  65. figtext(.33,.5,'MATLAB Benchmark')
  66. figtext(.40,.42,'Loops')
  67. axis off
  68. drawnow
  69.  
  70. n = 375;
  71. A = [];
  72. r = 1;
  73. tic
  74. for k = 1:ntimes
  75.    for j = 1:n
  76.       clear A;
  77.       r = rem(pi*r,1);
  78.       m = fix(100*r);
  79.       A = zeros(m,m);
  80.    end
  81. end
  82. t(1) = toc
  83.  
  84. % LU
  85.  
  86. clf
  87. figtext(.33,.5,'MATLAB Benchmark')
  88. figtext(.45,.42,'LU')
  89. axis off
  90. drawnow
  91.  
  92. n = 167;
  93. A = randn(n,n);
  94. tic
  95. for k = 1:ntimes
  96.    lu(A);
  97. end
  98. t(2) = toc
  99.  
  100. % Sparse
  101.  
  102. clf
  103. figtext(.33,.5,'MATLAB Benchmark')
  104. figtext(.42,.42,'Sparse')
  105. axis off
  106. drawnow
  107.  
  108. n = 36;
  109. A = delsq(numgrid('L',n));
  110. b = sum(A)';
  111. spparms('autommd',0);
  112. tic
  113. for k = 1:ntimes
  114.    x = A\b;
  115. end
  116. t(3) = toc
  117.  
  118. % 3-D
  119.  
  120. clf
  121. n = 24;
  122. [x,y,z] = peaks(n);
  123. ax = [-3 3 -3 3 -8 8];
  124. tic
  125. for k = 1:ntimes
  126.    surf(x,y,z);
  127.    axis(ax);
  128.    drawnow;
  129. end
  130. t(4) = toc
  131.  
  132. % 2-D
  133.  
  134. clf
  135. n = 52;
  136. tic
  137. for k = 1:ntimes
  138.    plot(fft(eye(n)));
  139.    axis('square')
  140.    drawnow;
  141. end
  142. t(5) = toc
  143.  
  144. % Compare with other machines.
  145.  
  146. machines = str2mat(machines,'This computer');
  147. times = [times; 10/ntimes*t];
  148. totals = sum(times')';
  149. speeds = 50*100./totals;
  150. [speeds,k] = sort(speeds);
  151. machines = machines(k,:);
  152. times = times(k,:);
  153. m = size(machines,1);
  154.  
  155. % Horizontal bar chart
  156. % Highlight this machine with another color.
  157.  
  158. me = find(k==length(k));
  159. clf
  160. [x,y] = bar(speeds);
  161. plot(y,x,'y');
  162. title('Relative Speed')
  163. hold on
  164. k = 5*me;
  165. plot(y(k-4:k),x(k-4:k),'m')
  166.  
  167. % Add machine names
  168.  
  169. for k = 1:m
  170.    text(5,k,machines(k,:))
  171. end
  172. hold off
  173.  
  174. % Display report in new figure
  175.  
  176. figh = figure('pos',get(gcf,'pos')+[100 -300 0 0]);
  177. drawnow
  178. x0 = .10;
  179. y0 = .85;
  180. dx = .10;
  181. dy = .06;
  182. figtext(x0+.30,y0,'Execution Time')
  183. x = x0+5*dx/2;
  184. y = y0-3*dy/2;
  185. tasks = [' Loops';'    LU';'Sparse';'   3-D';'   2-D'];
  186. for j = 1:5
  187.    figtext(x,y,tasks(j,:))
  188.    x = x + dx;
  189. end
  190. drawnow
  191. for i = 1:m
  192.    y = y0-(m-i+5/2)*dy;
  193.    x = x0;
  194.    h = figtext(x,y,machines(i,:));
  195.    if i == me, set(h,'color','m'), end
  196.    x = x + 5*dx/2;
  197.    for j = 1:5
  198.       h = figtext(x,y,sprintf('%6.1f',times(i,j)));
  199.       if i == me, set(h,'color','m'), end
  200.       x = x + dx;
  201.    end
  202.    drawnow
  203. end
  204.  
  205. % Pause before cleaning up.
  206.  
  207. disp(' ')
  208. disp('Pause.  Touch any key to close the report and finish.')
  209. disp(' ')
  210. pause
  211. close(figh)
  212. clf
  213.  
  214. % Output if requested.
  215. if nargout == 1, result = t; end
  216.  
  217.