DGtal 1.3.0
Loading...
Searching...
No Matches
Macros | Functions
testLabelledMap-benchmark.cpp File Reference
#include <cstdio>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <forward_list>
#include <boost/version.hpp>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_smallint.hpp>
#include <boost/random/uniform_01.hpp>
#include <boost/random/geometric_distribution.hpp>
#include <boost/random/variate_generator.hpp>
#include "DGtal/base/Common.h"
#include "DGtal/base/LabelledMap.h"

Go to the source code of this file.

Macros

#define BOOST_MAJOR_VERSION   (BOOST_VERSION / 100000)
 
#define BOOST_MINOR_VERSION   (( BOOST_VERSION / 100) % 1000)
 
#define BOOST_SUBMINOR_VERSION   (BOOST_VERSION % 100)
 

Functions

template<typename MapLXY , unsigned int L, unsigned int X, unsigned int Y>
unsigned int generateData (MapLXY &m, double proba_no_label, double proba_label)
 
template<typename MapLXY , unsigned int L, unsigned int X, unsigned int Y>
double sumAllData (MapLXY &m)
 
template<typename MapLXY , unsigned int L, unsigned int X, unsigned int Y>
double sumOneData (MapLXY &m, unsigned int l)
 
template<typename MapLXY , unsigned int L, unsigned int X, unsigned int Y>
unsigned int locateThreeData (MapLXY &m, unsigned int l1, unsigned int l2, unsigned int l3)
 
template<typename MapLXY , unsigned int L, unsigned int X, unsigned int Y>
unsigned int eraseOneData (MapLXY &m, unsigned int l)
 
int main ()
 

Detailed Description

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Author
Jacques-Olivier Lachaud (jacqu.nosp@m.es-o.nosp@m.livie.nosp@m.r.la.nosp@m.chaud.nosp@m.@uni.nosp@m.v-sav.nosp@m.oie..nosp@m.fr ) Laboratory of Mathematics (CNRS, UMR 5807), University of Savoie, France
Date
2012/07/02

This file is part of the DGtal library

Definition in file testLabelledMap-benchmark.cpp.

Macro Definition Documentation

◆ BOOST_MAJOR_VERSION

#define BOOST_MAJOR_VERSION   (BOOST_VERSION / 100000)

Definition at line 50 of file testLabelledMap-benchmark.cpp.

◆ BOOST_MINOR_VERSION

#define BOOST_MINOR_VERSION   (( BOOST_VERSION / 100) % 1000)

Definition at line 51 of file testLabelledMap-benchmark.cpp.

◆ BOOST_SUBMINOR_VERSION

#define BOOST_SUBMINOR_VERSION   (BOOST_VERSION % 100)

Definition at line 52 of file testLabelledMap-benchmark.cpp.

Function Documentation

◆ eraseOneData()

template<typename MapLXY , unsigned int L, unsigned int X, unsigned int Y>
unsigned int eraseOneData ( MapLXY &  m,
unsigned int  l 
)

Definition at line 608 of file testLabelledMap-benchmark.cpp.

609{
610 unsigned int nb = 0;
611 for ( unsigned int y = 0; y < Y; ++y )
612 for ( unsigned int x = 0; x < X; ++x )
613 {
614 nb += m.erase( l, x, y );
615 }
616 std::cerr << "- " << nb << " values deleted." << endl;
617 return nb;
618}

◆ generateData()

template<typename MapLXY , unsigned int L, unsigned int X, unsigned int Y>
unsigned int generateData ( MapLXY &  m,
double  proba_no_label,
double  proba_label 
)

Definition at line 522 of file testLabelledMap-benchmark.cpp.

523{
524 boost::mt19937 rng; // produces randomness out of thin air
525 rng.seed( 0 );
526 boost::uniform_smallint<> diceL(0, L-1);
527 boost::uniform_01<> diceDouble;
528 boost::geometric_distribution<> nbLabelsDist( proba_label ); // Y
529 boost::variate_generator
530 <boost::mt19937&,
531 boost::geometric_distribution<> > diceNbLabels( rng, nbLabelsDist);
532 // E(Y) = (1-p)/p, Var(Y) = (1-p)/p^2
533 std::cerr << "E(Y)=" << ( (1-proba_label)/proba_label )
534 << " Var(Y)=" << ( (1-proba_label)/(proba_label*proba_label) )
535 << std::endl;
536 unsigned int total = 0;
537 for ( unsigned int y = 0; y < Y; ++y )
538 for ( unsigned int x = 0; x < X; ++x )
539 {
540 if ( diceDouble( rng ) >= proba_no_label )
541 {
542 unsigned int nb = diceNbLabels();
543 for ( unsigned int i = 0; i < nb; ++i )
544 {
545 unsigned int l = diceL( rng );
546 double v = diceDouble( rng );
547 m.setValue( v, l, x, y );
548 }
549 total += nb;
550 }
551 }
552 std::cerr << "- " << total << " insertions." << endl;
553 return total;
554}

◆ locateThreeData()

template<typename MapLXY , unsigned int L, unsigned int X, unsigned int Y>
unsigned int locateThreeData ( MapLXY &  m,
unsigned int  l1,
unsigned int  l2,
unsigned int  l3 
)

Definition at line 591 of file testLabelledMap-benchmark.cpp.

592{
593 unsigned int loc3 = 0;
594 for ( unsigned int y = 0; y < Y; ++y )
595 for ( unsigned int x = 0; x < X; ++x )
596 {
597 if ( ( m.hasLabel( l1, x, y ) )
598 && ( m.hasLabel( l2, x, y ) )
599 && ( m.hasLabel( l3, x, y ) ) )
600 ++loc3;
601 }
602 std::cerr << "- " << loc3 << " places with " << l1 << ", " << l2 << " ," << l3 << endl;
603 return loc3;
604}

◆ main()

int main ( void  )

Probability that there is no data at this location.

If there is a possibility to have a data, this probability is used to define a geometric distribution that defines the number of data (ie valid labels) at this place. The smaller, the higher is the expectation. 0.5 means E(X) = 1.

Definition at line 620 of file testLabelledMap-benchmark.cpp.

621{
622 typedef double Value;
623 static const unsigned int X = 100;
624 static const unsigned int Y = 100;
625 static const unsigned int L = 16;
626 typedef DGtal::uint8_t Word;
627 static const unsigned int N = 1;
628 static const unsigned int M = 5;
630 static const double PROBA_NO_LABEL = 0.75;
635 static const double PROBA_LABEL = 0.5;
636
637 typedef ArrayLXY<Value, L, X, Y> MyArrayLXY;
638 typedef ArrayXYOfMap<Value, L, X, Y> MyArrayXYOfMap;
639 typedef ArrayXYOfList<Value, L, X, Y> MyArrayXYOfList;
640 typedef ArrayXYOfLabelledMap<Value, L, X, Y, Word, N, M > MyArrayXYOfLabelledMap;
641
642 //----------------------------------------------------------------------
643 trace.beginBlock ( "---------- ArrayLXY ---------------" );
644 trace.beginBlock ( "Generating ArrayLXY" );
645 MyArrayLXY* arrayLXY = new MyArrayLXY( -1.0 );
646 generateData< MyArrayLXY, L, X, Y> ( *arrayLXY, PROBA_NO_LABEL, PROBA_LABEL );
647 trace.endBlock();
648
649 trace.beginBlock ( "Memory usage in ArrayLXY" );
650 std::cerr << arrayLXY->area() << " bytes." << std::endl;
651 trace.endBlock();
652
653 trace.beginBlock ( "Sum all values ArrayLXY" );
654 sumAllData< MyArrayLXY, L, X, Y> ( *arrayLXY );
655 trace.endBlock();
656
657 trace.beginBlock ( "Sum label 0 values ArrayLXY" );
658 sumOneData< MyArrayLXY, L, X, Y> ( *arrayLXY, 0 );
659 trace.endBlock();
660
661 trace.beginBlock ( "Sum label 15 values ArrayLXY" );
662 sumOneData< MyArrayLXY, L, X, Y> ( *arrayLXY, 15 );
663 trace.endBlock();
664
665 trace.beginBlock ( "Locate places (3, 7, 8) in ArrayLXY" );
666 locateThreeData< MyArrayLXY, L, X, Y> ( *arrayLXY, 3, 7, 8 );
667 trace.endBlock();
668
669 trace.beginBlock ( "Erase label 9 in ArrayLXY" );
670 eraseOneData< MyArrayLXY, L, X, Y> ( *arrayLXY, 9 );
671 trace.endBlock();
672
673 trace.beginBlock ( "Delete ArrayLXY" );
674 delete arrayLXY;
675 trace.endBlock();
676 trace.endBlock();
677
678 //----------------------------------------------------------------------
679 trace.beginBlock ( "---------- ArrayXYOfMap ---------------" );
680 trace.beginBlock ( "Generating ArrayXYOfMap" );
681 MyArrayXYOfMap* arrayXYOfMap = new MyArrayXYOfMap();
682 generateData< MyArrayXYOfMap, L, X, Y> ( *arrayXYOfMap, PROBA_NO_LABEL, PROBA_LABEL );
683 trace.endBlock();
684
685 trace.beginBlock ( "Memory usage in ArrayXYOfMap" );
686 std::cerr << arrayXYOfMap->area() << " bytes." << std::endl;
687 trace.endBlock();
688
689 trace.beginBlock ( "Sum all values ArrayXYOfMap" );
690 sumAllData< MyArrayXYOfMap, L, X, Y> ( *arrayXYOfMap );
691 trace.endBlock();
692
693 trace.beginBlock ( "Sum label 0 values ArrayXYOfMap" );
694 sumOneData< MyArrayXYOfMap, L, X, Y> ( *arrayXYOfMap, 0 );
695 trace.endBlock();
696
697 trace.beginBlock ( "Sum label 15 values ArrayXYOfMap" );
698 sumOneData< MyArrayXYOfMap, L, X, Y> ( *arrayXYOfMap, 15 );
699 trace.endBlock();
700
701 trace.beginBlock ( "Locate places (3, 7, 8) in ArrayXYOfMap" );
702 locateThreeData< MyArrayXYOfMap, L, X, Y> ( *arrayXYOfMap, 3, 7, 8 );
703 trace.endBlock();
704
705 trace.beginBlock ( "Erase label 9 in ArrayXYOfMap" );
706 eraseOneData< MyArrayXYOfMap, L, X, Y> ( *arrayXYOfMap, 9 );
707 trace.endBlock();
708
709 trace.beginBlock ( "Delete ArrayXYOfMap" );
710 delete arrayXYOfMap;
711 trace.endBlock();
712 trace.endBlock();
713
714 //----------------------------------------------------------------------
715 trace.beginBlock ( "---------- ArrayXYOfList ---------------" );
716 trace.beginBlock ( "Generating ArrayXYOfList" );
717 MyArrayXYOfList* arrayXYOfList = new MyArrayXYOfList();
718 generateData< MyArrayXYOfList, L, X, Y> ( *arrayXYOfList, PROBA_NO_LABEL, PROBA_LABEL );
719 trace.endBlock();
720
721 trace.beginBlock ( "Memory usage in ArrayXYOfList" );
722 std::cerr << arrayXYOfList->area() << " bytes." << std::endl;
723 trace.endBlock();
724
725 trace.beginBlock ( "Sum all values ArrayXYOfList" );
726 sumAllData< MyArrayXYOfList, L, X, Y> ( *arrayXYOfList );
727 trace.endBlock();
728
729 trace.beginBlock ( "Sum label 0 values ArrayXYOfList" );
730 sumOneData< MyArrayXYOfList, L, X, Y> ( *arrayXYOfList, 0 );
731 trace.endBlock();
732
733 trace.beginBlock ( "Sum label 15 values ArrayXYOfList" );
734 sumOneData< MyArrayXYOfList, L, X, Y> ( *arrayXYOfList, 15 );
735 trace.endBlock();
736
737 trace.beginBlock ( "Locate places (3, 7, 8) in ArrayXYOfList" );
738 locateThreeData< MyArrayXYOfList, L, X, Y> ( *arrayXYOfList, 3, 7, 8 );
739 trace.endBlock();
740
741 trace.beginBlock ( "Erase label 9 in ArrayXYOfList" );
742 eraseOneData< MyArrayXYOfList, L, X, Y> ( *arrayXYOfList, 9 );
743 trace.endBlock();
744
745 trace.beginBlock ( "Delete ArrayXYOfList" );
746 delete arrayXYOfList;
747 trace.endBlock();
748 trace.endBlock();
749
750 //----------------------------------------------------------------------
751 trace.beginBlock ( "---------- ArrayXYOfLabelledMap ---------------" );
752 trace.beginBlock ( "Generating ArrayXYOfLabelledMap" );
753 MyArrayXYOfLabelledMap* arrayXYOfLabelledMap = new MyArrayXYOfLabelledMap;
754 generateData< MyArrayXYOfLabelledMap, L, X, Y > ( *arrayXYOfLabelledMap, PROBA_NO_LABEL, PROBA_LABEL );
755 trace.endBlock();
756
757 trace.beginBlock ( "Memory usage in ArrayXYOfLabelledMap" );
758 std::cerr << arrayXYOfLabelledMap->area() << " bytes." << std::endl;
759 trace.endBlock();
760
761 trace.beginBlock ( "Sum all values ArrayXYOfLabelledMap" );
762 sumAllData< MyArrayXYOfLabelledMap, L, X, Y> ( *arrayXYOfLabelledMap );
763 trace.endBlock();
764
765 trace.beginBlock ( "Sum label 0 values ArrayXYOfLabelledMap" );
766 sumOneData< MyArrayXYOfLabelledMap, L, X, Y> ( *arrayXYOfLabelledMap, 0 );
767 trace.endBlock();
768
769 trace.beginBlock ( "Sum label 15 values ArrayXYOfLabelledMap" );
770 sumOneData< MyArrayXYOfLabelledMap, L, X, Y> ( *arrayXYOfLabelledMap, 15 );
771 trace.endBlock();
772
773 trace.beginBlock ( "Locate places (3, 7, 8) in ArrayXYOfLabelledMap" );
774 locateThreeData< MyArrayXYOfLabelledMap, L, X, Y> ( *arrayXYOfLabelledMap, 3, 7, 8 );
775 trace.endBlock();
776
777 trace.beginBlock ( "Erase label 9 in ArrayXYOfLabelledMap" );
778 eraseOneData< MyArrayXYOfLabelledMap, L, X, Y> ( *arrayXYOfLabelledMap, 9 );
779 trace.endBlock();
780
781 trace.beginBlock ( "Delete ArrayXYOfLabelledMap" );
782 delete arrayXYOfLabelledMap;
783 trace.endBlock();
784 trace.endBlock();
785
786 return 0;
787}
void beginBlock(const std::string &keyword="")
double endBlock()
boost::uint8_t uint8_t
unsigned 8-bit integer.
Definition: BasicTypes.h:59
Trace trace
Definition: Common.h:154

References DGtal::Trace::beginBlock(), DGtal::Trace::endBlock(), and DGtal::trace.

◆ sumAllData()

template<typename MapLXY , unsigned int L, unsigned int X, unsigned int Y>
double sumAllData ( MapLXY &  m)

Definition at line 559 of file testLabelledMap-benchmark.cpp.

560{
561 double sum = 0.0;
562 std::vector<unsigned int> labels;
563 for ( unsigned int y = 0; y < Y; ++y )
564 for ( unsigned int x = 0; x < X; ++x )
565 {
566 m.getLabels( labels, x, y );
567 for ( unsigned int i = 0; i < labels.size(); ++i )
568 sum += m.value( labels[ i ], x, y );
569 }
570 std::cerr << "- sum = " << sum << "." << endl;
571 return sum;
572}

◆ sumOneData()

template<typename MapLXY , unsigned int L, unsigned int X, unsigned int Y>
double sumOneData ( MapLXY &  m,
unsigned int  l 
)

Definition at line 576 of file testLabelledMap-benchmark.cpp.

577{
578 double sum = 0.0;
579 for ( unsigned int y = 0; y < Y; ++y )
580 for ( unsigned int x = 0; x < X; ++x )
581 {
582 if ( m.hasLabel( l, x, y ) )
583 sum += m.value( l, x, y );
584 }
585 std::cerr << "- sum = " << sum << "." << endl;
586 return sum;
587}