My input file is 2GB and in this file every line is a word. I need to write a program to do wordcount. I use Java and C++ do to the same task but result is surprising: C++ is too slow! My code are as follows:
C++:
int main() {
struct timespec ts, te;
double cost;
clock_gettime(CLOCK_REALTIME, &ts);
map<string, int> map;
ifstream fin("inputfile.txt");
string word;
while(getline(fin, word)) {
++map[word];
}
clock_gettime(CLOCK_REALTIME, &te);
cost = te.tv_sec - ts.tv_sec + (double)(te.tv_nsec-ts.tv_nsec)/NANO;
printf("cost: %-15.10f s\n", cost);
return 0;
}
Output: cost: 257.62 s
Java:
public static void main(String[] args) throws Exception {
long startTime = System.currentTimeMillis();
Map<String, Integer> map = new HashMap<String, Integer>();
FileReader reader = new FileReader("inputfile.txt");
BufferedReader br = new BufferedReader(reader);
String str = null;
while((str = br.readLine()) != null) {
Integer count = map.get(str);
map.put(str, count == null ? 1 : count + 1);
}
long endTime = System.currentTimeMillis();
System.out.println("cost : " + (endTime - startTime)/1000 + "s");
}
Output: cost: 124 s
I delete the code inside the while, just read the file but do not do anything, the result is the same. Java cost: 32s, C++ cost: 38s. This gap I can accept. My environment is Ubuntu Linux 13.04 and C++ use -O2 optimation. Why does the STL perform poorly?