DGtal  1.2.0
testInHalfPlane-benchmark.cpp
Go to the documentation of this file.
1 
29 #include <iostream>
30 #include <cstdlib>
31 #include <ctime>
32 
33 #include "DGtal/base/Common.h"
34 
35 #include "DGtal/kernel/PointVector.h"
36 
37 #include "DGtal/geometry/tools/determinant/C2x2DetComputer.h"
38 #include "DGtal/geometry/tools/determinant/Simple2x2DetComputer.h"
39 #include "DGtal/geometry/tools/determinant/SimpleIncremental2x2DetComputer.h"
40 #include "DGtal/geometry/tools/determinant/AvnaimEtAl2x2DetSignComputer.h"
41 #include "DGtal/geometry/tools/determinant/Filtered2x2DetComputer.h"
42 
43 #include "DGtal/geometry/tools/determinant/COrientationFunctor2.h"
44 #include "DGtal/geometry/tools/determinant/InHalfPlaneBy2x2DetComputer.h"
45 #include "DGtal/geometry/tools/determinant/InHalfPlaneBySimple3x3Matrix.h"
47 
48 using namespace std;
49 using namespace DGtal;
50 
51 
53 // Random Functions
55 
60 {
61  return static_cast<DGtal::int32_t>(rand() % 32768);
62 }
63 
69 {
70  return static_cast<DGtal::int32_t>(rand() % 2048) +
71  ( randomInt15() * 2048 );
72 }
73 
79 {
80  return randomInt15() +
81  ( randomInt15() * 32768 );
82 }
83 
89 {
90  return
91  static_cast<DGtal::int64_t>(rand() % 128) +
92  ( static_cast<DGtal::int64_t>(randomInt15()) +
93  ( static_cast<DGtal::int64_t>(randomInt30()) )
94  * 32768 )
95  * 128;
96 }
97 
102 double randomDouble52 ()
103 {
104  return
105  static_cast<double>(rand() % 128) +
106  ( static_cast<double>(randomInt15()) +
107  ( static_cast<double>(randomInt30()) )
108  * 32768 )
109  * 128;
110 }
111 
117 {
118  return
119  static_cast<DGtal::int64_t>(rand() % 4) +
120  ( static_cast<DGtal::int64_t>(randomInt30()) +
121  ( static_cast<DGtal::int64_t>(randomInt30()) )
122  * 1073741824 )
123  * 4;
124 }
125 
126 
127 #ifdef WITH_BIGINTEGER
133 {
134  return
135  static_cast<DGtal::BigInteger>(rand() % 4) +
136  ( static_cast<DGtal::BigInteger>(randomInt30()) +
137  ( static_cast<DGtal::BigInteger>(randomInt30()) )
138  * 1073741824 )
139  * 4;
140 }
141 #endif
142 
148 {
150  return ((rand() % 2) ? x : -x);
151 }
152 
158 {
160  return ((rand() % 2) ? x : -x);
161 }
162 
168 {
170  return ((rand() % 2) ? x : -x);
171 }
172 
178 {
180  return ((rand() % 2) ? x : -x);
181 }
182 
188 {
189  double x = randomDouble52();
190  return ((rand() % 2) ? x : -x);
191 }
192 
198 {
200  return ((rand() % 2) ? x : -x);
201 }
202 
203 #ifdef WITH_BIGINTEGER
209 {
211  if (rand() % 2)
212  return x;
213  else
214  return -x;
215 }
216 #endif
217 
219 // Tests Functions
221 
230 template<typename OrientationFunctor, typename RandomFunctor>
231 bool randomTest(OrientationFunctor f, RandomFunctor gen, const DGtal::int32_t n = 1000000)
232 {
234 
235  typedef typename OrientationFunctor::Point Point;
236  Point P, Q, R;
237 
238  clock_t timeBegin, timeEnd;
239  timeBegin = clock();
240 
241  for (DGtal::int32_t i = 0; (i < n); ++i)
242  {
243  P[0] = gen();
244  P[1] = gen();
245  Q[0] = gen();
246  Q[1] = gen();
247  f.init(P, Q);
248  R[0] = gen();
249  R[1] = gen();
250  f( R );
251  }
252 
253  timeEnd = clock();
254  long double time, CPUTime;
255  time = ((double)timeEnd-(double)timeBegin);
256  CPUTime = time/((double)CLOCKS_PER_SEC);
257  std::cout << CPUTime << " ";
258 
259  return true;
260 }
261 
272 template<typename OrientationFunctor, typename RandomFunctor>
273 bool nullSameVectorsTest(OrientationFunctor f, RandomFunctor gen, const DGtal::int32_t n = 1000000)
274 {
276 
277  typedef typename OrientationFunctor::Point Point;
278  Point P, Q;
279 
280  clock_t timeBegin, timeEnd;
281  timeBegin = clock();
282 
283  for (DGtal::int32_t i = 0; (i < n); ++i)
284  {
285  P[0] = gen();
286  P[1] = gen();
287  Q[0] = gen();
288  Q[1] = gen();
289  f.init(P, Q);
290  f( Q );
291  }
292 
293  timeEnd = clock();
294  long double time, CPUTime;
295  time = ((double)timeEnd-(double)timeBegin);
296  CPUTime = time/((double)CLOCKS_PER_SEC);
297  std::cout << CPUTime << " ";
298 
299  return true;
300 }
301 
312 template<typename OrientationFunctor, typename RandomFunctor>
313 bool nullZeroVectorTest(OrientationFunctor f, RandomFunctor gen, const DGtal::int32_t n = 1000000)
314 {
316 
317  typedef typename OrientationFunctor::Point Point;
318  Point P, Q;
319 
320  clock_t timeBegin, timeEnd;
321  timeBegin = clock();
322 
323  for (DGtal::int32_t i = 0; (i < n); ++i)
324  {
325  P[0] = gen();
326  P[1] = gen();
327  Q[0] = gen();
328  Q[1] = gen();
329  f.init(P, Q);
330  f( P );
331  }
332 
333  timeEnd = clock();
334  long double time, CPUTime;
335  time = ((double)timeEnd-(double)timeBegin);
336  CPUTime = time/((double)CLOCKS_PER_SEC);
337  std::cout << CPUTime << " ";
338 
339  return true;
340 }
341 
353 template<typename OrientationFunctor, typename RandomFunctor>
354 bool nullTest(OrientationFunctor f, RandomFunctor gen, const DGtal::int32_t n = 1000000)
355 {
357 
358  typedef typename OrientationFunctor::Point Point;
359  Point P, Q, R;
360  P = Point(0,0);
361 
362  typedef typename Point::Coordinate Coordinate;
363  Coordinate k, l, u0, u1;
364 
365  clock_t timeBegin, timeEnd;
366  timeBegin = clock();
367 
368  for (DGtal::int32_t i = 0; (i < n); ++i)
369  {
370  k = gen();
371  l = gen();
372  u0 = gen();
373  u1 = gen();
374  Q[0] = k*u0;
375  Q[1] = k*u1;
376  R[0] = l*u0;
377  R[1] = l*u1;
378  f.init(P, Q);
379  f( R );
380  }
381 
382  timeEnd = clock();
383  long double time, CPUTime;
384  time = ((double)timeEnd-(double)timeBegin);
385  CPUTime = time/((double)CLOCKS_PER_SEC);
386  std::cout << CPUTime << " ";
387 
388  return true;
389 }
390 
403 template<typename OrientationFunctor, typename RandomFunctor>
404 bool quasiNullTest(OrientationFunctor f, RandomFunctor gen, const DGtal::int32_t n = 1000000)
405 {
407 
408  typedef typename OrientationFunctor::Point Point;
409  Point P, Q, R;
410  P = Point(0,0);
411 
412  typedef typename Point::Coordinate Coordinate;
413  Coordinate k, l, u0, u1;
414 
415  clock_t timeBegin, timeEnd;
416  timeBegin = clock();
417 
418  for (DGtal::int32_t i = 0; (i < n); ++i)
419  {
420  k = gen();
421  l = gen();
422  u0 = gen();
423  u1 = gen();
424  Q[0] = k*u0;
425  Q[0] += (rand()%5)-2;
426  Q[1] = k*u1;
427  Q[1] += (rand()%5)-2;
428  R[0] = l*u0;
429  R[0] += (rand()%5)-2;
430  R[1] = l*u1;
431  R[1] += (rand()%5)-2;
432  f.init(P, Q);
433  f( R );
434  }
435 
436  timeEnd = clock();
437  long double time, CPUTime;
438  time = ((double)timeEnd-(double)timeBegin);
439  CPUTime = time/((double)CLOCKS_PER_SEC);
440  std::cout << CPUTime << " ";
441 
442  return true;
443 }
444 
457 template<typename OrientationFunctor>
458 bool incTest(OrientationFunctor f, const DGtal::int32_t n = 1000000)
459 {
461 
462  typedef typename OrientationFunctor::Point Point;
463  Point P, Q, R;
464 
465  typedef typename Point::Coordinate Coordinate;
466  Coordinate dx, dy;
467 
468  Coordinate max = 1073741824;
469  P = Point(0,0);
470  Q = Point(max, max);
471  R = Point(max/2, max/2);
472  f.init(P, Q);
473 
474  clock_t timeBegin, timeEnd;
475  timeBegin = clock();
476 
477  for (DGtal::int32_t i = 0; (i < n); ++i)
478  {
479  dx = (rand() % 3) - 1;
480  dy = (rand() % 3) - 1;
481  R[0] += dx;
482  ASSERT( R[0] >= -max );
483  ASSERT( R[0] < max );
484  R[1] += dy;
485  ASSERT( R[0] >= -max );
486  ASSERT( R[0] < max );
487  f( R );
488  }
489 
490  timeEnd = clock();
491  long double time, CPUTime;
492  time = ((double)timeEnd-(double)timeBegin);
493  CPUTime = time/((double)CLOCKS_PER_SEC);
494  std::cout << CPUTime << " ";
495 
496  return true;
497 }
498 
505 {
507 
508  long seed = time(NULL);
509 
510  std::cout << "# incremental input " << std::endl;
511  std::cout << "# running times in s. for 1 million tries" << std::endl;
512 
513  {
514  srand(seed);
515  std::cout << "2x2-int32-int64 ";
518  incTest( F() );
519  std::cout << std::endl;
520  }
521  {
522  srand(seed);
523  std::cout << "2x2-inc-int32-int64 ";
526  incTest( F() );
527  std::cout << std::endl;
528  }
529  {
530  srand(seed);
531  std::cout << "2x2-avnaim++-int32-double ";
532  typedef AvnaimEtAl2x2DetSignComputer<double> DetComputer;
533  typedef Filtered2x2DetComputer<DetComputer> FDetComputer;
535  incTest( F() );
536  std::cout << std::endl;
537  }
538 #ifdef WITH_BIGINTEGER
539  {
540  srand(seed);
541  std::cout << "2x2-int32-BigInt ";
544  incTest( F() );
545  std::cout << std::endl;
546  }
547  {
548  srand(seed);
549  std::cout << "2x2-inc-int32-BigInt ";
552  incTest( F() );
553  std::cout << std::endl;
554  }
555 #endif
556  return true;
557 }
558 
565 {
567 
568  std::cout << "# random integers within [-2^30 ; 2^30[" << std::endl;
569  std::cout << "# running times in s. for 1 million tries" << std::endl;
570  std::cout << "# columns: random, null1, null2, null3, quasi-null " << std::endl;
571  std::cout << "# NB. double has " << std::numeric_limits<double>::digits << " bits in the mantissa " << std::endl;
572 
573  long seed = time(NULL);
574 
575  {
576  srand(seed);
577  std::cout << "3x3-int32-int64 ";
582  nullTest( F(), signedRandomInt15 );
584  std::cout << std::endl;
585  }
586 #ifdef WITH_BIGINTEGER
587  {
588  srand(seed);
589  std::cout << "3x3-int32-BigInt ";
594  nullTest( F(), signedRandomInt15 );
596  std::cout << std::endl;
597 }
598 #endif
599  {
600  srand(seed);
601  std::cout << "2x2-int32-int64 ";
607  nullTest( F(), signedRandomInt15 );
609  std::cout << std::endl;
610  }
611 #ifdef WITH_BIGINTEGER
612  {
613  srand(seed);
614  std::cout << "2x2-int32-BigInt ";
620  nullTest( F(), signedRandomInt15 );
622  std::cout << std::endl;
623  }
624 #endif
625  {
626  srand(seed);
627  std::cout << "2x2-inc-int32-int64 ";
633  nullTest( F(), signedRandomInt15 );
635  std::cout << std::endl;
636  }
637 #ifdef WITH_BIGINTEGER
638  {
639  srand(seed);
640  std::cout << "2x2-inc-int32-BigInt ";
646  nullTest( F(), signedRandomInt15 );
648  std::cout << std::endl;
649  }
650 #endif
651  {
652  srand(seed);
653  std::cout << "2x2-avnaim-int32-int32 ";
659  nullTest( F(), signedRandomInt15 );
661  std::cout << std::endl;
662  }
663  {
664  srand(seed);
665  std::cout << "2x2-avnaim-int32-double ";
666  typedef AvnaimEtAl2x2DetSignComputer<double> DetComputer;
671  nullTest( F(), signedRandomInt15 );
673  std::cout << std::endl;
674  }
675  {
676  srand(seed);
677  std::cout << "2x2-avnaim++-int32-double ";
678  typedef AvnaimEtAl2x2DetSignComputer<double> DetComputer;
679  typedef Filtered2x2DetComputer<DetComputer> FDetComputer;
684  nullTest( F(), signedRandomInt15 );
686  std::cout << std::endl;
687  }
688 
689  return true;
690 }
691 
698 {
699  std::cout << "# random integers within [-2^52 ; 2^52[" << std::endl;
700  std::cout << "# running times in s. for 1 million tries" << std::endl;
701  std::cout << "# columns: random, null1, null2, null3, quasi-null " << std::endl;
702  std::cout << "# NB. double has " << std::numeric_limits<double>::digits << " bits in the mantissa " << std::endl;
703 
704  long seed = time(NULL);
705 
706 #ifdef WITH_BIGINTEGER
707  {
708  srand(seed);
709  std::cout << "3x3-double-BigInt ";
710  typedef PointVector<2, double> Point;
715  nullTest( F(), signedRandomInt26 );
717  std::cout << std::endl;
718  }
719  {
720  srand(seed);
721  std::cout << "2x2-double-BigInt ";
722  typedef PointVector<2, double> Point;
728  nullTest( F(), signedRandomInt26 );
730  std::cout << std::endl;
731  }
732  {
733  srand(seed);
734  std::cout << "2x2-inc-double-BigInt ";
735  typedef PointVector<2, double> Point;
741  nullTest( F(), signedRandomInt26 );
743  std::cout << std::endl;
744  }
745 #endif
746  {
747  srand(seed);
748  std::cout << "2x2-avnaim-int64-int64 ";
755  nullTest( F(), signedRandomInt26 );
757  std::cout << std::endl;
758  }
759  {
760  srand(seed);
761  std::cout << "2x2-avnaim-double-int64 ";
762  typedef PointVector<2, double> Point;
768  nullTest( F(), signedRandomInt26 );
770  std::cout << std::endl;
771  }
772  {
773  srand(seed);
774  std::cout << "2x2-avnaim-int64-double ";
776  typedef AvnaimEtAl2x2DetSignComputer<double> DetComputer;
781  nullTest( F(), signedRandomInt26 );
783  std::cout << std::endl;
784  }
785  {
786  srand(seed);
787  std::cout << "2x2-avnaim-double-double ";
788  typedef PointVector<2, double> Point;
789  typedef AvnaimEtAl2x2DetSignComputer<double> DetComputer;
794  nullTest( F(), signedRandomInt26 );
796  std::cout << std::endl;
797  }
798  {
799  srand(seed);
800  std::cout << "2x2-avnaim++-int64-double ";
802  typedef AvnaimEtAl2x2DetSignComputer<double> DetComputer;
803  typedef Filtered2x2DetComputer<DetComputer> FDetComputer;
808  nullTest( F(), signedRandomInt26 );
810  std::cout << std::endl;
811  }
812  {
813  srand(seed);
814  std::cout << "2x2-avnaim++-double-double ";
815  typedef PointVector<2, double> Point;
816  typedef AvnaimEtAl2x2DetSignComputer<double> DetComputer;
817  typedef Filtered2x2DetComputer<DetComputer> FDetComputer;
822  nullTest( F(), signedRandomInt26 );
824  std::cout << std::endl;
825  }
826 
827  return true;
828 }
829 
836 {
837  std::cout << "# random integers within [-2^62 ; 2^62[" << std::endl;
838  std::cout << "# running times in s. for 1 million tries" << std::endl;
839  std::cout << "# columns: random, null1, null2, null3, quasi-null " << std::endl;
840  std::cout << "# NB. long double has " << std::numeric_limits<long double>::digits << " bits in the mantissa " << std::endl;
841 
842  long seed = time(NULL);
843 
844 #ifdef WITH_BIGINTEGER
845  {
846  srand(seed);
847  std::cout << "3x3-BigInt-BigInt ";
853  nullTest( F(), signedRandomInt30 );
855  std::cout << std::endl;
856  }
857  {
858  srand(seed);
859  std::cout << "2x2-BigInt-BigInt ";
866  nullTest( F(), signedRandomInt30 );
868  std::cout << std::endl;
869  }
870  {
871  srand(seed);
872  std::cout << "2x2-inc-BigInt-BigInt ";
879  nullTest( F(), signedRandomInt30 );
881  std::cout << std::endl;
882  }
883 #endif
884  {
885  srand(seed);
886  std::cout << "2x2-avnaim-int64-int64 ";
893  nullTest( F(), signedRandomInt30 );
895  std::cout << std::endl;
896  }
897  {
898  srand(seed);
899  std::cout << "2x2-avnaim++-int64-longdouble ";
901  typedef AvnaimEtAl2x2DetSignComputer<long double> DetComputer;
902  typedef Filtered2x2DetComputer<DetComputer> FDetComputer;
907  nullTest( F(), signedRandomInt30 );
909  std::cout << std::endl;
910  }
911 
912  return true;
913 }
914 
916 // Standard services - public :
917 
918 int main( int argc, char** argv )
919 {
920  trace.beginBlock ( "Testing class InHalfPlane-benchmark" );
921  trace.info() << "Args:";
922  for ( int i = 0; i < argc; ++i )
923  trace.info() << " " << argv[ i ];
924  trace.info() << endl;
925 
927  randomTest30All();
928  randomTest52All();
929  randomTest62All();
930 
931  bool res = true;
932  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
933  trace.endBlock();
934  return res ? 0 : 1;
935 }
936 // //
Aim: Class that provides a way of computing the sign of the determinant of a 2x2 matrix from its four...
Aim: Class that provides a way of computing the sign of the determinant of a 2x2 matrix from its four...
Aim: Class that implements an orientation functor, ie. it provides a way to compute the orientation o...
Aim: Class that implements an orientation functor, ie. it provides a way to compute the orientation o...
Aim: Implements basic operations that will be used in Point and Vector classes.
Definition: PointVector.h:593
Aim: Small class useful to compute the determinant of a 2x2 matrix from its four coefficients,...
Aim: Small class useful to compute, in an incremental way, the determinant of a 2x2 matrix from its f...
void beginBlock(const std::string &keyword="")
std::ostream & emphase()
std::ostream & info()
double endBlock()
DGtal is the top-level namespace which contains all DGtal functions and types.
boost::int64_t int64_t
signed 94-bit integer.
Definition: BasicTypes.h:74
Trace trace
Definition: Common.h:154
boost::int32_t int32_t
signed 32-bit integer.
Definition: BasicTypes.h:72
mpz_class BigInteger
Multi-precision integer with GMP implementation.
Definition: BasicTypes.h:79
Aim: This concept is a refinement of COrientationFunctor, useful for simple algebraic curves that can...
int max(int a, int b)
MyPointD Point
Definition: testClone2.cpp:383
srand(0)
double randomDouble52()
DGtal::int32_t randomInt15()
bool nullZeroVectorTest(OrientationFunctor f, RandomFunctor gen, const DGtal::int32_t n=1000000)
DGtal::BigInteger signedRandomBigInt62()
DGtal::int32_t randomInt30()
DGtal::int64_t signedRandomInt52()
DGtal::int32_t randomInt26()
int main(int argc, char **argv)
DGtal::int64_t signedRandomInt62()
bool randomTest30All()
bool incTest(OrientationFunctor f, const DGtal::int32_t n=1000000)
DGtal::int64_t randomInt52()
bool incTestComparison()
DGtal::int32_t signedRandomInt30()
DGtal::int32_t signedRandomInt15()
bool randomTest(OrientationFunctor f, RandomFunctor gen, const DGtal::int32_t n=1000000)
double signedRandomDouble52()
bool quasiNullTest(OrientationFunctor f, RandomFunctor gen, const DGtal::int32_t n=1000000)
DGtal::int64_t randomInt62()
bool randomTest62All()
bool nullSameVectorsTest(OrientationFunctor f, RandomFunctor gen, const DGtal::int32_t n=1000000)
bool randomTest52All()
DGtal::int32_t signedRandomInt26()
bool nullTest(OrientationFunctor f, RandomFunctor gen, const DGtal::int32_t n=1000000)
DGtal::BigInteger randomBigInt62()