Added pure C++ timeing function
[SXSI/xpathcomp.git] / timeXMLTree.cpp
1 #include "XMLDocShredder.h"
2 #include "XMLTree.h"
3 #include "Utils.h"
4 #include <sys/time.h>
5 #include <time.h>
6
7 using std::cout;
8 using std::string;
9 using std::left;
10 using std::right;
11
12 static clock_t tFirstChild = 0;
13 static clock_t tNextSibling = 0;
14 static clock_t tTaggedDesc = 0;
15 static clock_t tTaggedFoll = 0;
16 static clock_t tParentNode = 0;
17 static clock_t tPrevNode = 0;
18 static clock_t tTag = 0;
19 static clock_t tMyText = 0;
20 static clock_t tPrevText = 0;
21 static clock_t tNextText = 0;
22 static clock_t tFullTraversal = 0;
23 static clock_t tJumpTraversal = 0;
24
25 static unsigned int cFirstChild = 0;
26 static unsigned int cNextSibling = 0;
27 static unsigned int cTaggedDesc = 0;
28 static unsigned int cTaggedFoll = 0;
29 static unsigned int cParentNode = 0;
30 static unsigned int cPrevNode = 0;
31 static unsigned int cTag = 0;
32 static unsigned int cMyText = 0;
33 static unsigned int cPrevText = 0;
34 static unsigned int cNextText = 0;
35 static unsigned int cFullTraversal = 0;
36 static unsigned int cJumpTraversal = 0;
37
38 static clock_t tmp;
39
40 static TagType target_tag = -1;
41
42 #define STARTTIMER()   (tmp= clock())
43 #define STOPTIMER(x)   do {  (t##x) = (t##x) + (clock() - tmp); (c##x)= (c##x)+1;  } while (0)
44 #define PRINTSTATS(x)  do {                                             \
45     std::cout.width(11);                                                \
46     std::cout << std::left << #x;                                       \
47     std::cout << " : ";                                                 \
48     std::cout.width(8);                                                 \
49     std::cout << std::right << c##x << " calls,";                       \
50     std::cout.width(8);                                                 \
51     std::cout << std::right << t##x << " cycles, total:";               \
52     std::cout.width(5);                                                 \
53     std::cout << std::right << ((t##x) *1000.00)  /CLOCKS_PER_SEC       \
54               << " ms, mean: ";                                         \
55     std::cout.width(5);                                                 \
56     std::cout << std::right                                             \
57               << (((t##x)* 1000.00)  /CLOCKS_PER_SEC) / c##x            \
58               << "\n";                                                  \
59   } while (0)
60
61
62 void traversal(XMLTree * tree, treeNode node,unsigned char* targettagname){
63   treeNode res1,res2;
64   TagType tag;
65   DocID id1,id2,id3;
66   const unsigned char * tagname;
67   if (node != NULLT){
68     STARTTIMER();
69     tag = tree->Tag(node);
70     STOPTIMER(Tag);
71     if (target_tag == -1){
72       tagname = tree->GetTagNameByRef(tag);
73       if (strcmp( (char*) tagname, (char*) targettagname) == 0)
74         target_tag = tag;
75     };
76     STARTTIMER();
77     res1 = tree->TaggedDesc(node,tag);
78     STOPTIMER(TaggedDesc);
79
80     STARTTIMER();
81     res1 = tree->TaggedFoll(node,tag);
82     STOPTIMER(TaggedFoll);
83
84     STARTTIMER();
85     id1 = tree->MyText(node);
86     STOPTIMER(MyText);
87
88     STARTTIMER();
89     id2 = tree->PrevText(node);
90     STOPTIMER(PrevText);
91
92     STARTTIMER();
93     id3 = tree->NextText(node);
94     STOPTIMER(NextText);
95     
96     id1 = max(id1, max(id2,id3));
97
98     STARTTIMER();
99     res1 = tree->ParentNode(id1);
100     STOPTIMER(ParentNode);
101
102     STARTTIMER();
103     res1 = tree->PrevNode(id1);
104     STOPTIMER(PrevNode);
105     
106     STARTTIMER();
107     res1 = tree->FirstChild(node);
108     STOPTIMER(FirstChild);
109
110     STARTTIMER();
111     res2 = tree->NextSibling(node);
112     STOPTIMER(NextSibling);
113     traversal(tree,res1,targettagname);
114     traversal(tree,res2,targettagname);
115     
116   };
117   
118 }
119
120 unsigned int time_traversal(XMLTree *tree,treeNode node,unsigned int count){
121   TagType tag;
122   if (node != NULLT) {
123     cFullTraversal++;
124     tag = tree->Tag(node);
125     if (tag == target_tag)
126       count = count + 1;
127     return time_traversal(tree,tree->NextSibling(node), 
128                           time_traversal(tree,tree->FirstChild(node),count));
129
130   }
131   else 
132     return count;
133 }
134
135
136 unsigned int time_jump(XMLTree* tree, treeNode node,unsigned int count,treeNode root){
137   TagType tag;
138   if (node != NULLT) {
139     cJumpTraversal++;
140     tag = tree->Tag(node);
141     if (tag == target_tag)
142       count = count + 1;
143     return time_jump(tree,
144                           tree->TaggedFollBelow(node,target_tag,root), 
145                           time_jump(tree,
146                                     tree->TaggedDesc(node,target_tag),
147                                     count,
148                                     node),
149                      root);
150     
151   }
152   else 
153     return count;
154 }
155
156
157
158
159
160 int main(int argc, char ** argv){
161   unsigned int count1,count2;
162   unsigned char * tagname = (unsigned char *) "keyword";
163
164   if (argc != 2){
165     std::cout << "Usage : " << argv[0] << " filename (without .srx)\n";
166     return 1;
167   };
168
169   // The samplerate is not taken into account for loading anymore
170   XMLTree * tree = XMLTree::Load((unsigned char*) argv[1],64);
171   
172   traversal(tree,tree->Root(),tagname);
173
174   STARTTIMER();
175   count1 = time_traversal(tree,tree->Root(),0);
176   STOPTIMER(FullTraversal);
177
178   count2 = time_jump(tree,tree->Root(),0,tree->Root());
179   STOPTIMER(JumpTraversal);
180   
181   PRINTSTATS(FirstChild);
182   PRINTSTATS(NextSibling);
183   PRINTSTATS(Tag);
184   PRINTSTATS(TaggedDesc);
185   PRINTSTATS(TaggedFoll);
186   PRINTSTATS(PrevText);
187   PRINTSTATS(MyText);
188   PRINTSTATS(NextText);
189   PRINTSTATS(ParentNode);
190   PRINTSTATS(PrevNode);
191   std::cout << "\n";
192   std::cout << "Full traversal found " << count1 << " " << tagname << "  nodes\n";
193   PRINTSTATS(FullTraversal);
194   std::cout << "\n";
195   std::cout << "Jump traversal found " << count2 << " " << tagname << "  nodes\n";
196   PRINTSTATS(JumpTraversal);
197   
198   
199   return 0;
200 }