1 | if 0
|
---|
2 | % run under matlab 5
|
---|
3 | a = rand(2,3);
|
---|
4 | b = rand(3,4);
|
---|
5 | c = rand(3);
|
---|
6 | flops(0); a+a; flops
|
---|
7 | flops(0); a.*a; flops
|
---|
8 | flops(0); a./a; flops
|
---|
9 | flops(0); a<a; flops
|
---|
10 | flops(0); exp(a); flops
|
---|
11 | flops(0); log(a); flops
|
---|
12 | flops(0); a*a'; flops
|
---|
13 | flops(0); a*b; flops
|
---|
14 | flops(0); a/b'; flops
|
---|
15 |
|
---|
16 | ns = 1:10;
|
---|
17 | f = [];
|
---|
18 | for n = ns
|
---|
19 | c = rand(n);
|
---|
20 | c = c*c';
|
---|
21 | flops(0); chol(c); f(n) = flops;
|
---|
22 | end
|
---|
23 | f = [1 5 14 30 55 91 140 204 285 385];
|
---|
24 | x = [ones(size(ns)); ns; ns.^2; ns.^3];
|
---|
25 | a = f/x
|
---|
26 | plot(ns,f - a*x)
|
---|
27 | end
|
---|
28 |
|
---|
29 | niter = 2000;
|
---|
30 | a = rand(100);
|
---|
31 | b = rand(100);
|
---|
32 | if 0
|
---|
33 | tic; for i=1:niter ones(100); end; t_ones=toc/niter;
|
---|
34 | fprintf('time for ones: %g\n', t_ones);
|
---|
35 | else
|
---|
36 | t_ones = 0;
|
---|
37 | end
|
---|
38 | t = [];
|
---|
39 | tic; for i=1:niter a+4; end; t(1)=toc/niter;
|
---|
40 | tic; for i=1:niter a+b; end; t(2)=toc/niter;
|
---|
41 | tic; for i=1:niter a.*b; end; t(3)=toc/niter;
|
---|
42 | t_arith = mean(t) - t_ones;
|
---|
43 | fprintf('average for +,*: %g\n', t_arith);
|
---|
44 | if 0
|
---|
45 | % By time, == takes 2 flops, but this is processor dependent.
|
---|
46 | t = [];
|
---|
47 | tic; for i=1:niter a>b; end; t=toc/niter;
|
---|
48 | fprintf('time for >: %g\tflops = %g (should be %g)\n', t, t/t_arith, 2);
|
---|
49 | %tic; for i=1:niter a==b; end; t=toc/niter;
|
---|
50 | tic; for i=1:niter a~=b; end; t=toc/niter;
|
---|
51 | fprintf('time for ~=: %g\tflops = %g\n', t, t/t_arith);
|
---|
52 | end
|
---|
53 | if 0
|
---|
54 | tic; for i=1:niter randn(100); end; t=toc/niter;
|
---|
55 | t = t-t_ones;
|
---|
56 | fprintf('time for randn: %g\tflops = %g\n', t, t/t_arith);
|
---|
57 | end
|
---|
58 | if 0
|
---|
59 | tic; for i=1:niter a.^(0.51); end; t=toc/niter;
|
---|
60 | e1 = exp(1);
|
---|
61 | tic; for i=1:niter e1.^a; end; t=toc/niter;
|
---|
62 | t = t-t_ones;
|
---|
63 | fprintf('time for pow: %g\tflops = %g\n', t, t/t_arith);
|
---|
64 | if 0
|
---|
65 | tic; for i=1:niter a.^(0.5); end; toc
|
---|
66 | tic; for i=1:niter sqrt(a); end; toc
|
---|
67 | end
|
---|
68 | end
|
---|
69 | tic; for i=1:niter sqrt(a); end; t_sqrt=toc/niter;
|
---|
70 | fprintf('time for sqrt: %g\tflops = %g (should be %g)\n', t_sqrt, t_sqrt/t_arith, flops_sqrt);
|
---|
71 | tic; for i=1:niter exp(a); end; t_exp=toc/niter;
|
---|
72 | fprintf('time for exp: %g\tflops = %g (should be %g)\n', t_exp, t_exp/t_arith, flops_exp);
|
---|
73 | tic; for i=1:niter pow2(a); end; t_pow2=toc/niter;
|
---|
74 | fprintf('time for pow2: %g\tflops = %g (should be %g)\n', t_pow2, t_pow2/t_arith, flops_exp);
|
---|
75 | tic; for i=1:niter log(a); end; t_log=toc/niter;
|
---|
76 | fprintf('time for log: %g\tflops = %g (should be %g)\n', t_log, t_log/t_arith, flops_log);
|
---|
77 | tic; for i=1:niter log2(a); end; t_log=toc/niter;
|
---|
78 | fprintf('time for log2: %g\tflops = %g (should be %g)\n', t_log, t_log/t_arith, flops_log);
|
---|