Small fixes
[SXSI/TextCollection.git] / timeTextCollection.cpp
1 // Test driver for text collection
2 #include <iostream>
3 #include <cstdlib>
4 #include <string>
5 using std::cout;
6 using std::endl;
7 using std::cin;
8 using std::string;
9 #include <sys/time.h>
10 #include <time.h>
11
12 static struct timeval t1;
13 static struct timeval t2;
14
15 #define STARTTIMER() (gettimeofday(&t1,NULL))
16 #define STOPTIMER()  (gettimeofday(&t2,NULL))
17 #define GETTIME()    (((t2.tv_sec  - t1.tv_sec) * 1000000.0 + (t2.tv_usec  - t1.tv_usec))/1000.0)
18                       
19
20
21 #include "TextCollectionBuilder.h"
22 using SXSI::TextCollection;
23 using SXSI::TextCollectionBuilder;
24
25 int main(int argc, char**argv)
26 {
27   string str;
28   string buffer;
29   unsigned int max_str = 0;
30   unsigned int num_str = 0;
31   double time;
32
33   string words[] =  { "abcd", "abc", "mirrors", "attires", "mature",
34                       "rescue", "such", "embrace", "shipping", "ae",
35                       "preventions", "ab", "fe", "w" };
36   
37   
38   
39   TextCollectionBuilder *tcb = new TextCollectionBuilder(64);
40
41
42   STARTTIMER();
43   std::cerr << "Filling collection\n";
44   // read only 100000 strings 
45   while (not(cin.eof()) && num_str < 100000 ){
46       getline(cin,str); // Read line by line.
47       if (str.compare("----------") == 0){
48         tcb->InsertText((unsigned char*) buffer.c_str());
49
50         if (num_str % 10000 == 0){
51           STOPTIMER();
52           time = GETTIME();
53           std::cerr << "Added " << num_str << " strings in "
54                     << time  << " ms\n";
55           STARTTIMER();
56         };
57
58         num_str++;
59         if (max_str < buffer.size())
60           max_str = buffer.size();
61         buffer.clear();
62
63       }
64       else 
65         buffer.append(str);
66   };
67   std::cerr << "Calling MakeStatic()\n";
68   
69   TextCollection *tc = tcb->InitTextCollection();
70
71   std::cerr << "Statistics: " << num_str << " strings, " << max_str << " = max length\n";
72   int count;
73   bool is;
74   TextCollection::document_result res;
75   for (unsigned int i = 0; i < (sizeof(words)/sizeof(char*)) ; i++){
76   
77     STARTTIMER();
78     is = tc->IsContains((unsigned char*) words[i].c_str());
79     STOPTIMER();
80     time = GETTIME();
81
82     std::cerr << is << ", " << time << ", ";
83
84
85     STARTTIMER();
86     count = tc->Count((unsigned char*) words[i].c_str());
87     STOPTIMER();
88     time = GETTIME();
89
90     std::cerr << count << ", " << time << ", ";
91
92
93     STARTTIMER();
94     count = tc->CountContains((unsigned char*) words[i].c_str());
95     STOPTIMER();
96     time = GETTIME();
97
98     std::cerr << count << ", " << time << ", ";
99     
100
101     STARTTIMER();
102     res = tc->Contains((unsigned char*) words[i].c_str());
103     STOPTIMER();
104     time = GETTIME();
105     
106     std::cerr << time << "\n";
107     
108     
109   };
110
111   return 0;
112 }