Fixed MakeStatic()
[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 "TextCollection.h"
22 using SXSI::TextCollection;
23
24 int main(int argc, char**argv)
25 {
26   string str;
27   string buffer;
28   unsigned int max_str = 0;
29   unsigned int num_str = 0;
30   double time;
31
32   string words[] =  { "abcd", "abc", "mirrors", "attires", "mature",
33                       "rescue", "such", "embrace", "shipping", "ae",
34                       "preventions", "ab", "fe", "w" };
35   
36   
37   
38   TextCollection *csa = TextCollection::InitTextCollection(64);
39
40
41   STARTTIMER();
42   std::cerr << "Filling collection\n";
43   // read only 100000 strings 
44   while (not(cin.eof()) && num_str < 100000 ){
45       getline(cin,str); // Read line by line.
46       if (str.compare("----------") == 0){
47         csa->InsertText((unsigned char*) buffer.c_str());
48
49         if (num_str % 10000 == 0){
50           STOPTIMER();
51           time = GETTIME();
52           std::cerr << "Added " << num_str << " strings in "
53                     << time  << " ms\n";
54           STARTTIMER();
55         };
56
57         num_str++;
58         if (max_str < buffer.size())
59           max_str = buffer.size();
60         buffer.clear();
61
62       }
63       else 
64         buffer.append(str);
65   };
66   std::cerr << "Calling MakeStatic()\n";
67   
68   csa->MakeStatic();
69
70   std::cerr << "Statistics: " << num_str << " strings, " << max_str << " = max length\n";
71   int count;
72   bool is;
73   TextCollection::document_result res;
74   for (unsigned int i = 0; i < (sizeof(words)/sizeof(char*)) ; i++){
75   
76     STARTTIMER();
77     is = csa->IsContains((unsigned char*) words[i].c_str());
78     STOPTIMER();
79     time = GETTIME();
80
81     std::cerr << is << ", " << time << ", ";
82
83
84     STARTTIMER();
85     count = csa->Count((unsigned char*) words[i].c_str());
86     STOPTIMER();
87     time = GETTIME();
88
89     std::cerr << count << ", " << time << ", ";
90
91
92     STARTTIMER();
93     count = csa->CountContains((unsigned char*) words[i].c_str());
94     STOPTIMER();
95     time = GETTIME();
96
97     std::cerr << count << ", " << time << ", ";
98     
99
100     STARTTIMER();
101     res = csa->Contains((unsigned char*) words[i].c_str());
102     STOPTIMER();
103     time = GETTIME();
104     
105     std::cerr << time << "\n";
106     
107     
108   };
109
110   return 0;
111 }