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://www.wtfpl.net/ 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 | |
---|
19 | using namespace std; |
---|
20 | using namespace lol; |
---|
21 | |
---|
22 | static size_t const MATRIX_TABLE_SIZE = 64 * 1024; |
---|
23 | static size_t const MATRIX_RUNS = 100; |
---|
24 | |
---|
25 | void bench_matrix(int mode) |
---|
26 | { |
---|
27 | float result[5] = { 0.0f }; |
---|
28 | Timer timer; |
---|
29 | |
---|
30 | /* Set up tables */ |
---|
31 | mat4 *pm = new mat4[MATRIX_TABLE_SIZE + 1]; |
---|
32 | float *pf = new float[MATRIX_TABLE_SIZE]; |
---|
33 | |
---|
34 | for (size_t run = 0; run < MATRIX_RUNS; run++) |
---|
35 | { |
---|
36 | switch (mode) |
---|
37 | { |
---|
38 | case 1: |
---|
39 | for (size_t i = 0; i < MATRIX_TABLE_SIZE; i++) |
---|
40 | for (int j = 0; j < 4; j++) |
---|
41 | for (int k = 0; k < 4; k++) |
---|
42 | pm[i][j][k] = rand(-2.0f, 2.0f); |
---|
43 | break; |
---|
44 | } |
---|
45 | |
---|
46 | /* Copy matrices */ |
---|
47 | timer.Get(); |
---|
48 | for (size_t i = 0; i < MATRIX_TABLE_SIZE; i++) |
---|
49 | pm[i] = pm[i + 1]; |
---|
50 | result[0] += timer.Get(); |
---|
51 | |
---|
52 | /* Determinant */ |
---|
53 | timer.Get(); |
---|
54 | for (size_t i = 0; i < MATRIX_TABLE_SIZE; i++) |
---|
55 | pf[i] = determinant(pm[i]); |
---|
56 | result[1] += timer.Get(); |
---|
57 | |
---|
58 | /* Multiply matrices */ |
---|
59 | timer.Get(); |
---|
60 | for (size_t i = 0; i < MATRIX_TABLE_SIZE; i++) |
---|
61 | pm[i] *= pm[i + 1]; |
---|
62 | result[2] += timer.Get(); |
---|
63 | |
---|
64 | /* Add matrices */ |
---|
65 | timer.Get(); |
---|
66 | for (size_t i = 0; i < MATRIX_TABLE_SIZE; i++) |
---|
67 | pm[i] += pm[i + 1]; |
---|
68 | result[3] += timer.Get(); |
---|
69 | |
---|
70 | /* Invert matrix */ |
---|
71 | timer.Get(); |
---|
72 | for (size_t i = 0; i < MATRIX_TABLE_SIZE; i++) |
---|
73 | pm[i] = inverse(pm[i]); |
---|
74 | result[4] += timer.Get(); |
---|
75 | } |
---|
76 | |
---|
77 | delete[] pm; |
---|
78 | delete[] pf; |
---|
79 | |
---|
80 | for (size_t i = 0; i < sizeof(result) / sizeof(*result); i++) |
---|
81 | result[i] *= 1e9f / (MATRIX_TABLE_SIZE * MATRIX_RUNS); |
---|
82 | |
---|
83 | Log::Info(" ns/elem\n"); |
---|
84 | Log::Info("mat4 = mat4 %7.3f\n", result[0]); |
---|
85 | Log::Info("float = mat4.det() %7.3f\n", result[1]); |
---|
86 | Log::Info("mat4 *= mat4 %7.3f\n", result[2]); |
---|
87 | Log::Info("mat4 += mat4 %7.3f\n", result[3]); |
---|
88 | Log::Info("mat4 = mat4.invert() %7.3f\n", result[4]); |
---|
89 | } |
---|
90 | |
---|