MercuryDPM  0.11
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Vec3DSpeedTest.cpp
Go to the documentation of this file.
1 //Copyright (c) 2013-2014, The MercuryDPM Developers Team. All rights reserved.
2 //For the list of developers, see <http://www.MercuryDPM.org/Team>.
3 //
4 //Redistribution and use in source and binary forms, with or without
5 //modification, are permitted provided that the following conditions are met:
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above copyright
9 // notice, this list of conditions and the following disclaimer in the
10 // documentation and/or other materials provided with the distribution.
11 // * Neither the name MercuryDPM nor the
12 // names of its contributors may be used to endorse or promote products
13 // derived from this software without specific prior written permission.
14 //
15 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 //ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 //WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 //DISCLAIMED. IN NO EVENT SHALL THE MERCURYDPM DEVELOPERS TEAM BE LIABLE FOR ANY
19 //DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 //(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 //LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 //ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 //(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 //SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 
26 #include<iostream>
27 #include "Math/Vector.h"
28 #include "Math/Matrix.h"
29 #include "MercuryTime.h"
30 
31 //not really useful functions as it does not return anything; simply for speed testing.
32 
34 {
35  return v.X;
36 }
37 
39 {
40  return v.X;
41 }
42 
44 {
45  return v->X;
46 }
47 
49 {
50  return v.X;
51 }
52 
54 {
55  return v.XX;
56 }
57 
59 {
60  return v.XX;
61 }
62 
64 {
65  return v->XX;
66 }
67 
69 {
70  return v.XX;
71 }
72 
73 void testVec3D(unsigned int n)
74 {
75  std::cout << "testing Vec3D performance" << std::endl;
76 
77  Vec3D v={1,0,0};
78  Mdouble x=0;
79 
80  Time time;
81  time.tic();
82  for (unsigned int i = 0; i < n; i++)
83  {
84  x+=passByReference(v);
85  }
86  std::cout << "passByReference clocks: " << time.toc()*CLOCKS_PER_SEC << std::endl;
87 
88  time.tic();
89  for (unsigned int i = 0; i < n; i++)
90  {
92  }
93  std::cout << "passByConstReference clocks: " << time.toc()*CLOCKS_PER_SEC << std::endl;
94 
95  Vec3D* p = &v;
96  time.tic();
97  for (unsigned int i = 0; i < n; i++)
98  {
99  x+=passByPointer(p);
100  }
101  std::cout << "passByPointer clocks: " << time.toc()*CLOCKS_PER_SEC << std::endl;
102 
103  time.tic();
104  for (unsigned int i = 0; i < n; i++)
105  {
106  x+=passByValue(v);
107  }
108  std::cout << "passByValue clocks: " << time.toc()*CLOCKS_PER_SEC << std::endl;
109  std::cout << x << std::endl;
110 }
111 
112 void testMatrix3D(unsigned int n)
113 {
114  std::cout << "testing Matrix3D performance" << std::endl;
115 
116  Matrix3D v={1,0,0,0,0,0,0,0,0};
117  Mdouble x=0;
118 
119  Time time;
120  time.tic();
121  for (unsigned int i = 0; i < n; i++)
122  {
123  x+=passByReference(v);
124  }
125  std::cout << "passByReference clocks: " << time.toc()*CLOCKS_PER_SEC << std::endl;
126 
127  time.tic();
128  for (unsigned int i = 0; i < n; i++)
129  {
130  x+=passByConstReference(v);
131  }
132  std::cout << "passByConstReference clocks: " << time.toc()*CLOCKS_PER_SEC << std::endl;
133 
134  Matrix3D* p = &v;
135  time.tic();
136  for (unsigned int i = 0; i < n; i++)
137  {
138  x+=passByPointer(p);
139  }
140  std::cout << "passByPointer clocks: " << time.toc()*CLOCKS_PER_SEC << std::endl;
141 
142  time.tic();
143  for (unsigned int i = 0; i < n; i++)
144  {
145  x+=passByValue(v);
146  }
147  std::cout << "passByValue clocks: " << time.toc()*CLOCKS_PER_SEC << std::endl;
148  std::cout << x << std::endl;
149 }
150 
151 int main()
152 {
153  unsigned int n = 1e8;
154  testVec3D(n);
155  testMatrix3D(n);
156  return 0;
157 }
Mdouble passByConstReference(const Vec3D &v)
Mdouble X
the vector components
Definition: Vector.h:52
Mdouble passByPointer(Vec3D *v)
Allows for timing the algorithms; accurate up to 0.01 sec.
Definition: MercuryTime.h:40
int main()
double Mdouble
Mdouble passByValue(Vec3D v)
void tic()
This is like a start button of a stopwatch. Assigns the variable start with the current number of clo...
Definition: MercuryTime.h:48
Mdouble passByReference(Vec3D &v)
Mdouble toc()
This is like a stop button of a stopwatch. Assigns the variable finish to the current value of ticks ...
Definition: MercuryTime.h:58
Mdouble XX
all nine matrix elements
Definition: Matrix.h:42
Implementation of a 3D matrix.
Definition: Matrix.h:36
Implementation of a 3D vector (by Vitaliy).
Definition: Vector.h:45
void testVec3D(unsigned int n)
void testMatrix3D(unsigned int n)