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