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
|
/* OpenSceneGraph example, osgunittests.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "performance.h"
#include <osg/Timer>
#include <iostream>
#include <osg/NodeVisitor>
#include <osg/ref_ptr>
#include <osg/MatrixTransform>
#include <osg/Group>
struct Benchmark
{
Benchmark()
{
calibrate();
_beginTick = _timer.tick();
_endTick = _timer.tick();
}
void calibrate(unsigned int numLoops = 100000)
{
osg::Timer_t beginTick = _timer.tick();
for(unsigned int i=0;i<numLoops;++i)
{
begin();
end();
}
osg::Timer_t endTick = _timer.tick();
_averageDelay = _timer.delta_s(beginTick,endTick)/(double)numLoops;
}
inline void begin()
{
_beginTick = _timer.tick();
}
inline void end()
{
_endTick = _timer.tick();
}
inline double time()
{
double t = _timer.delta_s(_beginTick,_endTick) - _averageDelay;
return t<0.0 ? 0.0 : t;
}
inline void output(const char* str, double numIterations=1.0)
{
std::cout<<str<<"\t";
double s = time()/numIterations;
if (s>=1.0) std::cout<<s<<" s"<<std::endl;
else if (s>=0.001) std::cout<<s*1000.0<<" ms (10 ^ -3)"<<std::endl;
else if (s>=0.000001) std::cout<<s*1000000.0<<" ns (10 ^ -6)"<<std::endl;
else std::cout<<s*1000000000.0<<" ps (10 ^ -9)"<<std::endl;
}
osg::Timer _timer;
osg::Timer_t _beginTick;
osg::Timer_t _endTick;
double _averageDelay;
};
#define RUN(A,B,D) { A.begin(); for(unsigned int i=0;i<D;++i) B; A.end(); A.output(#B,D); }
static int v = 0;
#define OPERATION { v=v+1; }
inline void inline_increment() { OPERATION }
void function_increment() { OPERATION }
typedef void ( * IncrementProc) ();
IncrementProc s_functionIncrement = &function_increment;
inline void functionPointer_increment() { s_functionIncrement(); }
struct InlineMethod;
struct Method;
struct VirtualMethod;
struct VirtualMethod2;
struct Visitor
{
virtual void apply(InlineMethod& m);
virtual void apply(Method& m);
virtual void apply(VirtualMethod& m);
virtual void apply(VirtualMethod2& m);
virtual ~Visitor() {}
};
struct InlineMethod
{
void method() { OPERATION }
virtual void accept(Visitor& visitor) { visitor.apply(*this); }
virtual ~InlineMethod() {}
};
struct Method
{
virtual void accept(Visitor& visitor) { visitor.apply(*this); }
void method();
virtual ~Method() {}
};
void Method::method() { OPERATION }
struct VirtualMethod
{
virtual void accept(Visitor& visitor) { visitor.apply(*this); }
virtual void method();
virtual ~VirtualMethod() {}
};
void VirtualMethod::method() { OPERATION }
struct VirtualMethod2 : public VirtualMethod
{
VirtualMethod2() { }
virtual void accept(Visitor& visitor) { visitor.apply(*this); }
virtual void method();
virtual ~VirtualMethod2() { }
char a[100];
};
void VirtualMethod2::method() { OPERATION }
void Visitor::apply(Method& m) { m.method(); }
void Visitor::apply(VirtualMethod& m) { m.method(); }
void Visitor::apply(InlineMethod& m) { m.method(); }
void Visitor::apply(VirtualMethod2& m) { m.method(); }
struct CustomVisitor
{
virtual void apply(InlineMethod& m) { m.method(); }
virtual void apply(Method& m) { m.method(); }
virtual void apply(VirtualMethod& m) { m.method(); }
virtual void apply(VirtualMethod2& m) { m.method(); }
virtual ~CustomVisitor() {}
};
class CustomNodeVisitor : public osg::NodeVisitor
{
public:
void apply(osg::Node&) { }
void apply(osg::Group&) { }
void apply(osg::Transform&) { }
};
void runPerformanceTests()
{
Benchmark benchmark;
unsigned int iterations = 10000000;
RUN(benchmark, {} , iterations)
v = 0;
RUN(benchmark, OPERATION , iterations)
RUN(benchmark, functionPointer_increment() , iterations)
RUN(benchmark, inline_increment() , iterations)
RUN(benchmark, function_increment() , iterations)
VirtualMethod2 m4;
RUN(benchmark, m4.method() , iterations)
InlineMethod m1;
RUN(benchmark, m1.method() , iterations)
Method m2;
RUN(benchmark, m2.method() , iterations)
VirtualMethod m3;
RUN(benchmark, m3.method() , iterations)
RUN(benchmark, m3.method() , iterations)
Visitor visitor;
RUN(benchmark, m4.accept(visitor), iterations)
RUN(benchmark, m1.accept(visitor), iterations)
RUN(benchmark, m2.accept(visitor), iterations)
RUN(benchmark, m3.accept(visitor), iterations)
RUN(benchmark, m4.accept(visitor), iterations)
VirtualMethod* vm4 = &m4;
RUN(benchmark, (dynamic_cast<VirtualMethod2*>(vm4))->method(), iterations)
RUN(benchmark, (static_cast<VirtualMethod2*>(vm4))->method(), iterations)
RUN(benchmark, { VirtualMethod mm; mm.method(); }, iterations)
RUN(benchmark, { VirtualMethod2 mm; mm.method(); }, iterations)
osg::ref_ptr<osg::Group> group = new osg::Group;
osg::ref_ptr<osg::MatrixTransform> mt = new osg::MatrixTransform;
osg::Node* m = mt.get();
CustomNodeVisitor cnv;
RUN(benchmark, { osg::MatrixTransform* mtl = dynamic_cast<osg::MatrixTransform*>(m); if (mtl) cnv.apply(*mtl); }, 1000)
RUN(benchmark, { m->accept(cnv); }, 10000)
}
|