Modified the timing 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 #include <stdio.h>
12
13 static struct timeval t1;
14 static struct timeval t2;
15
16 #define STARTTIMER() (gettimeofday(&t1,NULL))
17 #define STOPTIMER()  (gettimeofday(&t2,NULL))
18 #define GETTIME()    (((t2.tv_sec  - t1.tv_sec) * 1000000.0 + (t2.tv_usec  - t1.tv_usec))/1000.0)
19                       
20
21
22 #include "TextCollectionBuilder.h"
23 using SXSI::TextCollection;
24 using SXSI::TextCollectionBuilder;
25
26
27 int main(int argc, char**argv)
28 {
29   string str;
30   string buffer;
31   unsigned int text_size = 0;
32   unsigned int max_str = 0;
33   unsigned int num_str = 0;
34   double time;
35
36   string words[] =  { "Bakst", 
37                       "ruminants", "morphine", "AUSTRALIA","molecule" ,"brain", "human", "blood","from", "with", " in", "the", "of",
38                       "a",
39                       "\n" };
40   unsigned int NWORDS = 15;
41
42   TextCollectionBuilder *tcb64 = new TextCollectionBuilder(64);
43   TextCollectionBuilder *tcb2 = new TextCollectionBuilder(5);
44
45   STARTTIMER();
46   std::cerr << "Filling collection\n";
47
48
49   while (not(cin.eof()) ){
50     getline(cin,str); // Read line by line.    
51     str.append("\n");
52     
53     if (str.compare("----------\n") == 0 ){
54       tcb64->InsertText((unsigned char*) buffer.c_str());
55       tcb2->InsertText((unsigned char*) buffer.c_str());
56
57       if (num_str % 10000 == 0){
58         STOPTIMER();
59         time = GETTIME();
60         std::cerr << "Added " << num_str << " strings in "
61                   << time  << " ms\n";
62         std::cerr.flush();
63         //STARTTIMER();
64       };
65       
66       num_str++;
67       if (max_str < buffer.size())
68         max_str = buffer.size();
69       text_size += buffer.size();
70       buffer.clear();
71     }     
72     else 
73       buffer.append(str);
74   };
75   std::cerr << "Number of bytes inserted : " << text_size << "\n";
76   std::cerr << "Calling InitTextCollection() for sf=64: ";
77   STARTTIMER();
78   TextCollection *tc64 = tcb64->InitTextCollection();
79   STOPTIMER();
80   time = GETTIME();
81   std::cerr << time << "ms\n";
82   std::cerr << "Calling InitTextCollection() for sf=5: ";
83   STARTTIMER();
84   TextCollection *tc2 = tcb2->InitTextCollection();
85   STOPTIMER();
86   time = GETTIME();
87   std::cerr << time << "ms\n";
88   FILE* file;
89
90   file = fopen("index_64.tc","w+");
91   std::cerr << "Saving to index_64.tc ";
92   STARTTIMER();
93   tc64->Save(file);
94   STOPTIMER();   
95   time = GETTIME();
96   std::cerr << time << "ms\n";
97   fclose(file);
98
99   file = fopen("index_05.tc","w+");
100   std::cerr << "Saving to index_05.tc ";
101   STARTTIMER();
102   tc2->Save(file);
103   STOPTIMER();   
104   time = GETTIME();
105   std::cerr << time << "ms\n";
106   fclose(file);
107   
108
109   std::cerr << "Statistics: " << num_str << " strings, " << max_str << " = max length\n";
110   int count;
111   bool is;
112   TextCollection::document_result res;
113   TextCollection *tc;
114   tc = tc64;
115   std::cerr << "Sampling rate 64\n";
116   for (unsigned int num = 0; num < 2; num ++){
117     
118     for (unsigned int i = 0; i < NWORDS ; i++){
119       
120       std::cerr << "\"" << words[i] << ": "; 
121       STARTTIMER();
122       is = tc->IsContains((unsigned char*) words[i].c_str());
123       STOPTIMER();
124       time = GETTIME();
125       
126       std::cerr << is << ", " << time << ", ";
127       
128       
129       STARTTIMER();
130       count = tc->Count((unsigned char*) words[i].c_str());
131       STOPTIMER();
132       time = GETTIME();
133       
134       std::cerr << count << ", " << time << ", ";
135       
136       
137       STARTTIMER();
138       count = tc->CountContains((unsigned char*) words[i].c_str());
139       STOPTIMER();
140       time = GETTIME();
141       
142       std::cerr << count << ", " << time << ", ";
143     
144       
145       STARTTIMER();
146       res = tc->Contains((unsigned char*) words[i].c_str());
147       STOPTIMER();
148       time = GETTIME();
149       
150       std::cerr << time << "\n";
151       
152       
153     };
154     tc = tc2;
155     std::cerr << "---------------------------\n";
156     std::cerr << "Sampling rate 5\n";
157   };
158   return 0;
159 }