2 * sumtimes - runtimes evaluating sums & squares of large lists and mats
4 * Copyright (C) 2006 Ernest Bowen
6 * Calc is open software; you can redistribute it and/or modify it under
7 * the terms of the version 2.1 of the GNU Lesser General Public License
8 * as published by the Free Software Foundation.
10 * Calc is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
13 * Public License for more details.
15 * A copy of version 2.1 of the GNU Lesser General Public License is
16 * distributed with calc under the filename COPYING-LGPL. You should have
17 * received a copy with calc; if not, write to Free Software Foundation, Inc.
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * @(#) $Revision: 30.1 $
21 * @(#) $Id: sumtimes.cal,v 30.1 2007/03/16 11:09:54 chongo Exp $
22 * @(#) $Source: /usr/local/src/bin/calc/cal/RCS/sumtimes.cal,v $
24 * Under source code control: 2006/06/22 17:29
25 * File existed as early as: 2006
27 * Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/
31 global sumtimes_t0, sumtimes_t1, sumtimes_t2, sumtimes_t3;
32 global sumtimes_A, sumtimes_B;
35 define timematsum(N) {
40 for (n = 0; n < N; n++) sumtimes_A[n] = rand(N);
42 ptop = &sumtimes_A[n-1];
43 sumtimes_t0 = usertime();
44 for (s = n = 0; n < N; n++) s += sumtimes_A[n];
45 sumtimes_t1 = usertime();
46 for (s = 0, p = &sumtimes_A[0]; p <= ptop; p++) s += *p;
47 sumtimes_t2 = usertime();
48 s = matsum(sumtimes_A);
49 sumtimes_t3 = usertime();
51 print "Matrix sum runtimes";
52 printf('\tStandard "for" loop:\t\t%.4f\n', sumtimes_t1 - sumtimes_t0);
53 printf('\t"For" loop using pointers:\t\t%.4f\n', sumtimes_t2 - sumtimes_t1);
54 printf('\tUsing builtin "matsum":\t\t%.4f\n', sumtimes_t3 - sumtimes_t2);
57 define timelistsum(N) {
60 sumtimes_A = makelist(N);
61 for (n = 0; n < N; n++) sumtimes_A[n] = rand(N);
63 sumtimes_t0 = usertime();
64 for (s = n = 0; n < N; n++) s += sumtimes_A[n];
65 sumtimes_t1 = usertime();
67 sumtimes_t2 = usertime();
68 print "List sum runtimes";
69 printf('\tStandard "for" loop:\t\t%.4f\n', sumtimes_t1 - sumtimes_t0);
70 printf('\tUsing builtin "sum":\t\t%.4f\n', sumtimes_t2 - sumtimes_t1);
74 define timematsort(N) {
78 for (n = 0; n < N; n++) sumtimes_A[n] = rand(N);
79 sumtimes_t0 = usertime();
81 sumtimes_t1 = usertime();
82 printf('\tMatrix sort runtime:\t\t%.4f\n', sumtimes_t1 - sumtimes_t0);
86 define timelistsort(N) {
89 sumtimes_A = makelist(N);
90 for (n = 0; n < N; n++) sumtimes_A[n] = rand(N);
91 sumtimes_t0 = usertime();
93 sumtimes_t1 = usertime();
94 printf('\tList sort runtime:\t\t%.4f\n', sumtimes_t1 - sumtimes_t0);
97 define timematreverse(N) {
101 for (n = 0; n < N; n++) sumtimes_A[n] = rand(N);
102 sumtimes_t0 = usertime();
104 sumtimes_t1 = usertime();
105 printf('\tMatrix reverse runtime %.4f\n', sumtimes_t1 - sumtimes_t0);
108 define timelistreverse(N) {
111 sumtimes_A = makelist(N);
112 for (n = 0; n < N; n++) sumtimes_A[n] = rand(N);
113 sumtimes_t0 = usertime();
115 sumtimes_t1 = usertime();
116 printf('\tList reverse runtime:\t\t%.4f\n', sumtimes_t1 - sumtimes_t0);
119 define timematssq(N) {
124 for (n = 0; n < N; n++) sumtimes_A[n] = rand(N);
126 ptop = &sumtimes_A[n-1];
127 sumtimes_t0 = usertime();
128 for (s = n = 0; n < N; n++) s += sumtimes_A[n]^2;
129 sumtimes_t1 = usertime();
130 for (s = 0, p = &sumtimes_A[0]; p <= ptop; p++) s += (*p)^2;
131 sumtimes_t2 = usertime();
133 print "Matrix sum of squares runtimes";
134 printf('\tStandard "for" loop:\t\t%.4f\n', sumtimes_t1 - sumtimes_t0);
135 printf('\t"For" loop using pointers:\t\t%.4f\n', sumtimes_t2 - sumtimes_t1);
138 define timelistssq(N) {
141 sumtimes_A = makelist(N);
142 for (n = 0; n < N; n++) sumtimes_A[n] = rand(N);
144 sumtimes_t0 = usertime();
145 for (s = n = 0; n < N; n++) s += sumtimes_A[n]^2;
146 sumtimes_t1 = usertime();
148 sumtimes_t2 = usertime();
149 print "List sum of squares runtimes";
150 printf('\tStandard "for" loop:\t\t%.4f\n', sumtimes_t1 - sumtimes_t0);
151 printf('\tUsing builtin "ssq":\t\t%.4f\n', sumtimes_t2 - sumtimes_t1);
154 define timehmean(N, M = 10) {
157 sumtimes_A = makelist(N);
158 for (n = 0; n < N; n++) sumtimes_A[n] = rand(1, M);
160 sumtimes_t0 = usertime();
161 for (s = n = 0; n < N; n++) s += 1/sumtimes_A[n];
163 sumtimes_t1 = usertime();
164 v2 = hmean(sumtimes_A);
165 sumtimes_t2 = usertime();
167 print "List harmonic meanruntimes";
168 printf('\tStandard "for" loop:\t\t%.4f\n', sumtimes_t1 - sumtimes_t0);
169 printf('\tUsing builtin "hmean":\t\t%.4f\n', sumtimes_t2 - sumtimes_t1);
172 define doalltimes(N) {