1 /* 2 * main.h 3 * 4 * PWLib application header file for MapDictionary 5 * 6 * Copyright 2009 Derek J Smithies 7 * 8 * $Revision: 23418 $ 9 * $Author: rjongbloed $ 10 * $Date: 2009-09-10 19:13:48 -0500 (Thu, 10 Sep 2009) $ 11 */ 12 13 #ifndef _MapDictionary_MAIN_H 14 #define _MapDictionary_MAIN_H 15 16 #include <ptlib/pprocess.h> 17 18 #include <map> 19 20 /*! \mainpage map_dictionary 21 22 The purpose of this program is to find out which is faster: 23 \li STL based map 24 \li PTLib based dictionary 25 26 By default, this program creates the 200 instances of the structure Element. 27 Each instance of the Element class is given a random (and hopefully) unique key. 28 The key can exist either as a string, or as a number. 29 30 Each instance of the class Element has a key to the next instance. 31 32 After creation of an instance of the class Element, it is put into a 33 map (or PDictionary), keyed of the random key. 34 35 Then, the code looks through the map (or PDictionary) for each created 36 instance. Since the code knows the first key, it can get the key of 37 the next element via the current element. In this way, the code is 38 made to make N acceses to seemingly random parts of the map (or 39 PDictionary). 40 41 Results so far indicate that when the key is integer, STL map is 42 always faster - by a factor of four or more. 43 44 When the key is a PString, STL map is only faster when there are more 45 than 1000 elements in the map. 46 */ 47 48 /**This class is the core of the thing. It is placed in the structure 49 (map or dictionary) being tested. There are hundreds/thousands of 50 these created. Each instance holds the key to the next 51 element. Since the key to the next element is random, any code that 52 follows this ends up walking over the entire map/directory in a 53 random fashion */ 54 class Element : public PObject 55 { 56 PCLASSINFO(Element, PObject); 57 58 /*The string held in this class */ 59 PString contents; 60 61 /**the string key to the next element */ 62 PString nextKey; 63 64 /**the integer key to the next element */ 65 PINDEX nextIntKey; 66 67 /**the string key to get to this element */ 68 PString thisKey; 69 70 /**the integer key to get to this element */ 71 PINDEX thisIntKey; 72 }; 73 74 75 /**Defination of a STL based map, keyed of a PString */ 76 typedef std::map<PString, Element *> ElementMap; 77 78 /**Defination of a PTLib based dictionary, keyed of a PString */ 79 typedef PDictionary<PString, Element > ElementDict; 80 81 /**Defination of a STL based map, keyed of an integer */ 82 typedef std::map<PINDEX, Element *> ElementIntMap; 83 84 /**Defination of a PTLib based dictionary, keyed of an integer */ 85 typedef PDictionary<POrdinalKey, Element > ElementIntDict; 86 87 88 /**This is where all the activity happens. This class is launched on 89 program startup, and does timing runs on the map and dictionaries 90 to see which is faster */ 91 class MapDictionary : public PProcess 92 { 93 PCLASSINFO(MapDictionary, PProcess) 94 95 public: 96 /**Constructor */ 97 MapDictionary(); 98 99 /**Destructor */ 100 ~MapDictionary(); 101 102 /**Program execution starts here */ 103 void Main(); 104 105 /**Test the STL map, which uses a string index */ 106 void TestMap(); 107 108 /**Test the PTLib dictionary, which uses a string index */ 109 void TestDict(); 110 111 /**Test the STL map, which uses an integer index */ 112 void TestIntMap(); 113 114 /**Test the PTLib dictionary, which uses an integer index */ 115 void TestIntDict(); 116 117 118 protected: 119 120 /**The number of times we go over all the elements */ 121 PINDEX loops; 122 123 /**The number of elements to put in the map/dictionary */ 124 PINDEX size; 125 126 /**The string key to the first element in the map/dictionary */ 127 PString firstKey; 128 129 /**the integer key to the first element in the map/dictionary */ 130 PINDEX firstIntKey; 131 }; 132 133 134 135 136 #endif // _MapDictionary_MAIN_H 137 138 139 // End of File /////////////////////////////////////////////////////////////// 140