source: trunk/test/lol-bench.cpp @ 880

Last change on this file since 880 was 880, checked in by sam, 9 years ago

test: add matrix code to the benchmark program.

  • Property svn:keywords set to Id
File size: 6.5 KB
Line 
1//
2// Lol Engine - Benchmark program
3//
4// Copyright: (c) 2005-2011 Sam Hocevar <sam@hocevar.net>
5//   This program is free software; you can redistribute it and/or
6//   modify it under the terms of the Do What The Fuck You Want To
7//   Public License, Version 2, as published by Sam Hocevar. See
8//   http://sam.zoy.org/projects/COPYING.WTFPL for more details.
9//
10
11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#include <cstdio>
16
17#include "core.h"
18#include "loldebug.h"
19
20using namespace std;
21using namespace lol;
22
23static size_t const MATRIX_TABLE_SIZE = 64 * 1024;
24static size_t const MATRIX_RUNS = 100;
25
26static size_t const HALF_TABLE_SIZE = 1024 * 1024;
27static size_t const HALF_RUNS = 50;
28
29static void bench_matrix(int mode);
30static void bench_half(int mode);
31
32int main(int argc, char **argv)
33{
34    Log::Info("----------------------------\n");
35    Log::Info(" Float matrices [-2.0, 2.0]\n");
36    Log::Info("----------------------------\n");
37    bench_matrix(1);
38
39    Log::Info("-------------------------------------\n");
40    Log::Info(" Half precision floats (random bits)\n");
41    Log::Info("-------------------------------------\n");
42    bench_half(1);
43
44    Log::Info("-----------------------------------\n");
45    Log::Info(" Half precision floats [-2.0, 2.0]\n");
46    Log::Info("-----------------------------------\n");
47    bench_half(2);
48
49    return EXIT_SUCCESS;
50}
51
52static void bench_matrix(int mode)
53{
54    float result[5] = { 0.0f };
55    Timer timer;
56
57    /* Set up tables */
58    mat4 *pm = new mat4[MATRIX_TABLE_SIZE + 1];
59    float *pf = new float[MATRIX_TABLE_SIZE];
60
61    for (size_t run = 0; run < MATRIX_RUNS; run++)
62    {
63        switch (mode)
64        {
65        case 1:
66            for (size_t i = 0; i < MATRIX_TABLE_SIZE; i++)
67                for (int j = 0; j < 4; j++)
68                    for (int k = 0; k < 4; k++)
69                        pm[i][j][k] = RandF(-2.0f, 2.0f);
70            break;
71        }
72
73        /* Copy matrices */
74        timer.GetMs();
75        for (size_t i = 0; i < MATRIX_TABLE_SIZE; i++)
76            pm[i] = pm[i + 1];
77        result[0] += timer.GetMs();
78
79        /* Determinant */
80        timer.GetMs();
81        for (size_t i = 0; i < MATRIX_TABLE_SIZE; i++)
82            pf[i] = pm[i].det();
83        result[1] += timer.GetMs();
84
85        /* Multiply matrices */
86        timer.GetMs();
87        for (size_t i = 0; i < MATRIX_TABLE_SIZE; i++)
88            pm[i] *= pm[i + 1];
89        result[2] += timer.GetMs();
90
91        /* Add matrices */
92        timer.GetMs();
93        for (size_t i = 0; i < MATRIX_TABLE_SIZE; i++)
94            pm[i] += pm[i + 1];
95        result[3] += timer.GetMs();
96
97        /* Invert matrix */
98        timer.GetMs();
99        for (size_t i = 0; i < MATRIX_TABLE_SIZE; i++)
100            pm[i] = pm[i].invert();
101        result[4] += timer.GetMs();
102    }
103
104    delete[] pm;
105    delete[] pf;
106
107    for (size_t i = 0; i < sizeof(result) / sizeof(*result); i++)
108        result[i] *= 1000000.0f / (MATRIX_TABLE_SIZE * MATRIX_RUNS);
109
110    Log::Info("                          ns/elem\n");
111    Log::Info("mat4 = mat4              %7.3f\n", result[0]);
112    Log::Info("float = mat4.det()       %7.3f\n", result[1]);
113    Log::Info("mat4 *= mat4             %7.3f\n", result[2]);
114    Log::Info("mat4 += mat4             %7.3f\n", result[3]);
115    Log::Info("mat4 = mat4.invert()     %7.3f\n", result[4]);
116}
117
118static void bench_half(int mode)
119{
120    float result[10] = { 0.0f };
121    Timer timer;
122
123    /* Set up tables */
124    float *pf = new float[HALF_TABLE_SIZE + 1];
125    half *ph = new half[HALF_TABLE_SIZE + 1];
126
127    for (size_t run = 0; run < HALF_RUNS; run++)
128    {
129        switch (mode)
130        {
131        case 1:
132            for (size_t i = 0; i < HALF_TABLE_SIZE + 1; i++)
133                ph[i] = half::makebits(rand());
134            break;
135        case 2:
136            for (size_t i = 0; i < HALF_TABLE_SIZE + 1; i++)
137                ph[i] = RandF(-2.0f, 2.0f);
138            break;
139        }
140#if defined __EXCEPTIONS
141#error lol
142#endif
143
144        /* Copy float */
145        timer.GetMs();
146        for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
147            pf[i] = pf[i + 1];
148        result[0] += timer.GetMs();
149
150        /* Convert half to float (array) */
151        timer.GetMs();
152        half::convert(pf, ph, HALF_TABLE_SIZE);
153        result[1] += timer.GetMs();
154
155        /* Convert half to float (fast) */
156        timer.GetMs();
157        for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
158            pf[i] = (float)ph[i];
159        result[2] += timer.GetMs();
160
161        /* Add a half to every float */
162        timer.GetMs();
163        for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
164            pf[i] += ph[i];
165        result[3] += timer.GetMs();
166
167        /* Copy half */
168        timer.GetMs();
169        for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
170            ph[i] = ph[i + 1];
171        result[4] += timer.GetMs();
172
173        /* Change sign of every half */
174        timer.GetMs();
175        for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
176            ph[i] = -ph[i];
177        result[5] += timer.GetMs();
178
179        /* Convert float to half (array) */
180        timer.GetMs();
181        half::convert(ph, pf, HALF_TABLE_SIZE);
182        result[6] += timer.GetMs();
183
184        /* Convert float to half (fast) */
185        timer.GetMs();
186        for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
187            ph[i] = (half)pf[i];
188        result[7] += timer.GetMs();
189
190        /* Convert float to half (accurate) */
191        timer.GetMs();
192        for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
193            ph[i] = half::makeaccurate(pf[i]);
194        result[8] += timer.GetMs();
195
196        /* Add a float to every half */
197        timer.GetMs();
198        for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
199            ph[i] += pf[i];
200        result[9] += timer.GetMs();
201    }
202
203    delete[] pf;
204    delete[] ph;
205
206    for (size_t i = 0; i < sizeof(result) / sizeof(*result); i++)
207        result[i] *= 1000000.0f / (HALF_TABLE_SIZE * HALF_RUNS);
208
209    Log::Info("                          ns/elem\n");
210    Log::Info("float = float            %7.3f\n", result[0]);
211    Log::Info("float = half (array)     %7.3f\n", result[1]);
212    Log::Info("float = half (fast)      %7.3f\n", result[2]);
213    Log::Info("float += half            %7.3f\n", result[3]);
214    Log::Info("half = half              %7.3f\n", result[4]);
215    Log::Info("half = -half             %7.3f\n", result[5]);
216    Log::Info("half = float (array)     %7.3f\n", result[6]);
217    Log::Info("half = float (fast)      %7.3f\n", result[7]);
218    Log::Info("half = float (accurate)  %7.3f\n", result[8]);
219    Log::Info("half += float            %7.3f\n", result[9]);
220}
221
Note: See TracBrowser for help on using the repository browser.