summaryrefslogtreecommitdiff
path: root/base/bench.c
blob: 385681f02f8361e679971ed6d4cf5968df97a3b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/* Copyright (C) 2001-2023 Artifex Software, Inc.
   All Rights Reserved.

   This software is provided AS-IS with no warranty, either express or
   implied.

   This software is distributed under license and may not be copied,
   modified or distributed except as expressly authorized under the terms
   of the license contained in the file LICENSE in this distribution.

   Refer to licensing information at http://www.artifex.com or contact
   Artifex Software, Inc.,  39 Mesa Street, Suite 108A, San Francisco,
   CA 94129, USA, for further information.
*/


/* Simple hardware benchmarking suite (C and PostScript) */
#include "stdio_.h"
#include <stdlib.h>

/* Patchup for GS externals */
FILE *gs_stdout;
FILE *gs_stderr;
const char gp_scratch_file_name_prefix[] = "gs_";
static void
capture_stdio(void)
{
    gs_stdout = stdout;
    gs_stderr = stderr;
}
#include "gsio.h"
#undef gs_stdout
#undef gs_stderr
#undef stdout
#define stdout gs_stdout
#undef stderr
#define stderr gs_stderr
FILE *
gp_open_scratch_file(const gs_memory_t *mem,
                     const char        *prefix,
                     char              *fname,
                     const char        *mode)
{
    return NULL;
}
void
gp_set_printer_binary(int prnfno, int binary)
{
}
void
gs_to_exit(int n)
{
}
#define eprintf_program_ident(f, pn, rn) (void)0
void
lprintf_file_and_line(FILE * f, const char *file, int line)
{
    fprintf(f, "%s(%d): ", file, line);
}

/*
 * Read the CPU time (in seconds since an implementation-defined epoch)
 * into ptm[0], and fraction (in nanoseconds) into ptm[1].
 */
#include "gp_unix.c"
#undef stdout
#define stdout gs_stdout
#undef stderr
#define stderr gs_stderr

/* Loop unrolling macros */
#define do10(x) x;x;x;x;x; x;x;x;x;x

/* Define the actual benchmarks. */
static int
iadd(int a, int n, char **msg)
{
    int b = 0, i;

    for (i = n / 20; --i >= 0;) {
        do10((b += a, b += i));
    }
    *msg = "integer adds";
    return b;
}
static int
imul(int a, int n, char **msg)
{
    int b = 1, i;

    for (i = n / 20; --i > 0;) {
        do10((b *= a, b *= i));
    }
    *msg = "integer multiplies";
    return b;
}
static int
idiv(int a, int n, char **msg)
{
    int b = 1, i;

    for (i = n / 20; --i > 0;) {
        b += 999999;
        do10((b /= a, b /= i));
    }
    *msg = "integer divides";
    return b;
}
static int
fadd(float a, int n, char **msg)
{
    float b = 0;
    int i;

    for (i = n / 10; --i >= 0;) {
        do10((b += a));
    }
    *msg = "floating adds";
    return b;
}
static int
fmul(float a, int n, char **msg)
{
    float b = 1;
    int i;

    for (i = n / 10; --i >= 0;) {
        do10((b *= a));
    }
    *msg = "floating multiplies";
    return b;
}
static int
fdiv(float a, int n, char **msg)
{
    float b = 1;
    int i;

    for (i = n / 10; --i >= 0;) {
        do10((b /= a));
    }
    *msg = "floating divides";
    return b;
}
static int
fconv(int a, int n, char **msg)
{
    int b[10];
    float f[10];
    int i;

    b[0] = a;
    for (i = n / 20; --i >= 0;)
        f[0] = b[0], f[1] = b[1], f[2] = b[2], f[3] = b[3], f[4] = b[4],
            f[5] = b[5], f[6] = b[6], f[7] = b[7], f[8] = b[8], f[9] = b[9],
            b[0] = f[1], b[1] = f[2], b[2] = f[3], b[3] = f[4], b[4] = f[5],
            b[5] = f[6], b[6] = f[7], b[7] = f[8], b[8] = f[9], b[9] = f[0];
    *msg = "float/int conversions";
    return b[0];
}
static int
mfast(int *m, int n, char **msg)
{
    int i;

    m[0] = n;
    for (i = n / 20; --i >= 0;)
        m[9] = m[8], m[8] = m[7], m[7] = m[6], m[6] = m[5], m[5] = m[4],
            m[4] = m[3], m[3] = m[2], m[2] = m[1], m[1] = m[0], m[0] = m[9];
    *msg = "fast memory accesses";
    return m[0];
}
static int
mslow(int *m, int n, char **msg)
{
    int *p;
    int i, k = 0;

    m[0] = n;
    for (i = n / 20; --i >= 0; k = (k + 397) & 0x3ffff)
        p = m + k,
            p[0] = p[100], p[20] = p[120], p[40] = p[140],
            p[60] = p[160], p[80] = p[180],
            p[200] = p[300], p[220] = p[320], p[240] = p[340],
            p[260] = p[360], p[280] = p[380];
    *msg = "slow memory accesses";
    return m[0];
}

int
main(int argc, const char *argv[])
{
    int i;
    int *mem = malloc(1100000);

    capture_stdio();
    for (i = 0;; ++i) {
        long t0[2], t1[2];
        char *msg;
        int n;

        gp_get_usertime(t0);
        switch (i) {
            case 0:
                iadd(0, n = 10000000, &msg);
                break;
            case 1:
                imul(1, n = 1000000, &msg);
                break;
            case 2:
                idiv(1, n = 1000000, &msg);
                break;
            case 3:
                fadd(3.14, n = 10000000, &msg);
                break;
            case 4:
                fmul(1.0000001, n = 10000000, &msg);
                break;
            case 5:
                fdiv(1.0000001, n = 1000000, &msg);
                break;
            case 6:
                fconv(12345, n = 10000000, &msg);
                break;
            case 7:
                mfast(mem, n = 10000000, &msg);
                break;
            case 8:
                mslow(mem, n = 1000000, &msg);
                break;
            default:
                free(mem);
                exit(0);
        }
        gp_get_usertime(t1);
        fprintf(stdout, "Time for %9d %s = %g ms\n", n, msg,
                (t1[0] - t0[0]) * 1000.0 + (t1[1] - t0[1]) / 1000000.0);
        fflush(stdout);
    }
}

/*
   Output from SPARCstation 10, gcc -O bench.c gp_unix.c:

   Time for  10000000 integer adds = 113.502 ms
   Time for   1000000 integer multiplies = 467.965 ms
   Time for   1000000 integer divides = 594.328 ms
   Time for  10000000 floating adds = 641.21 ms
   Time for  10000000 floating multiplies = 643.357 ms
   Time for   1000000 floating divides = 131.995 ms
   Time for  10000000 float/int conversions = 602.061 ms
   Time for  10000000 fast memory accesses = 201.048 ms
   Time for   1000000 slow memory accesses = 552.606 ms

   Output from 486DX/25, wcl386 -oit bench.c gp_iwatc.c gp_msdos.c:

   Time for  10000000 integer adds = 490 ms
   Time for   1000000 integer multiplies = 770 ms
   Time for   1000000 integer divides = 1860 ms
   Time for  10000000 floating adds = 4070 ms
   Time for  10000000 floating multiplies = 4450 ms
   Time for   1000000 floating divides = 2470 ms
   Time for  10000000 float/int conversions = 25650 ms
   Time for  10000000 fast memory accesses = 990 ms
   Time for   1000000 slow memory accesses = 330 ms

 */

/*
   The rest of this file contains a similar benchmark in PostScript.

   %!
   /timer               % <str> <N> <proc> timer
   { bind 2 copy usertime mark 4 2 roll repeat cleartomark usertime exch sub
   % Stack: str N proc dt
   exch pop
   (Time for ) print exch =only ( ) print exch =only (: ) print
   =only ( ms
   ) print flush
   } def

   (x 20 integer adds) 5000 { 0
   0 add 0 add 0 add 0 add 0 add 0 add 0 add 0 add 0 add 0 add
   0 add 0 add 0 add 0 add 0 add 0 add 0 add 0 add 0 add 0 add
   pop } timer

   (x 20 integer multiplies) 5000 { 1
   3 mul 3 mul 3 mul 3 mul 3 mul 3 mul 3 mul 3 mul 3 mul 3 mul
   3 mul 3 mul 3 mul 3 mul 3 mul 3 mul 3 mul 3 mul 3 mul 3 mul
   pop } timer

   (x 20 integer divides) 5000 { 1000000000
   3 idiv 3 idiv 3 idiv 3 idiv 3 idiv 3 idiv 3 idiv 3 idiv 3 idiv 3 idiv
   3 idiv 3 idiv 3 idiv 3 idiv 3 idiv 3 idiv 3 idiv 3 idiv 3 idiv 3 idiv
   pop } timer

   (x 20 floating adds) 5000 { 0.0
   1.0 add 1.0 add 1.0 add 1.0 add 1.0 add 1.0 add 1.0 add 1.0 add 1.0 add 1.0 add
   1.0 add 1.0 add 1.0 add 1.0 add 1.0 add 1.0 add 1.0 add 1.0 add 1.0 add 1.0 add
   pop } timer

   (x 20 floating multiplies) 5000 { 1.0
   2.3 mul 2.3 mul 2.3 mul 2.3 mul 2.3 mul 2.3 mul 2.3 mul 2.3 mul 2.3 mul 2.3 mul
   2.3 mul 2.3 mul 2.3 mul 2.3 mul 2.3 mul 2.3 mul 2.3 mul 2.3 mul 2.3 mul 2.3 mul
   pop } timer

   (x 20 floating divides) 5000 { 1.0
   2.3 div 2.3 div 2.3 div 2.3 div 2.3 div 2.3 div 2.3 div 2.3 div 2.3 div 2.3 div
   2.3 div 2.3 div 2.3 div 2.3 div 2.3 div 2.3 div 2.3 div 2.3 div 2.3 div 2.3 div
   pop } timer

   (x 20 float/int conversions) 5000 { 12345.0
   cvi cvr cvi cvr cvi cvr cvi cvr cvi cvr
   cvi cvr cvi cvr cvi cvr cvi cvr cvi cvr
   pop } timer

   /S 2048 string def
   (x 10000(byte) fast memory accesses) 1000 {
   //S 1024 1000 getinterval //S copy pop
   //S 1024 1000 getinterval //S copy pop
   //S 1024 1000 getinterval //S copy pop
   //S 1024 1000 getinterval //S copy pop
   //S 1024 1000 getinterval //S copy pop
   } timer

   /A [ 500 { 2048 string } repeat ] def
   (x 500 x 2000(byte) slower memory accesses) 10 {
   0 1 499 {
   //A exch get dup 1024 1000 getinterval exch copy pop
   } for
   } timer

   /Times-Roman findfont 36 scalefont setfont
   currentcacheparams pop pop 0 1 index setcacheparams
   /D [4 0 0 4 0 0] 1440 1440 <00 ff> makeimagedevice def
   D setdevice
   72 72 translate
   gsave 15 rotate
   0 0 moveto (A) show
   (x 10 (A) show (cache)) 100 {
   0 0 moveto
   (A) show (A) show (A) show (A) show (A) show
   (A) show (A) show (A) show (A) show (A) show
   } timer grestore

   0 setcachelimit
   gsave 10 rotate
   (x 10 (A) show (no cache)) 10 {
   0 0 moveto
   (A) show (A) show (A) show (A) show (A) show
   (A) show (A) show (A) show (A) show (A) show
   } timer grestore

   quit

   Results for SUN Sparc 2 (rated at 25 MIPS according to manual)

   ./gs
   Now in gs_init.ps
   TextAlphaBits defined GraphicsAlphaBits defined
   Aladdin Ghostscript 3.50 (1995-9-24)
   (c) 1995 Aladdin Enterprises, Menlo Park, CA.  All rights reserved. This
   software comes with NO WARRANTY: see the file PUBLIC for details. Leaving
   gs_init.ps
   GS>(ben1.c) run
   Time for 5000 x 20 integer adds: 171 ms
   Time for 5000 x 20 integer multiplies: 504 ms
   Time for 5000 x 20 integer divides: 334 ms
   Time for 5000 x 20 floating adds: 148 ms
   Time for 5000 x 20 floating multiplies: 165 ms
   Time for 5000 x 20 floating divides: 194 ms
   Time for 5000 x 20 float/int conversions: 121 ms
   Time for 1000 x 10000(byte) fast memory accesses: 112 ms
   Time for 10 x 500 x 2000(byte) slower memory accesses: 236 ms
   Loading NimbusRomanNo9L-Regular font from
   [...]/n021003l.gsf... 1739080 414724 2564864 1251073 0
   done. Time for 100 x 10 (A) show (cache): 144 ms
   Time for 10 x 10 (A) show (no cache): 538 ms

   Output from SPARCstation 10, gs 3.60 compiled with gcc -g -O -DDEBUG:

   gsnd bench.c
   Aladdin Ghostscript 3.60 (1995-10-23)