简体   繁体   English

为什么 C++ STL 映射是慢 Java 的映射类?

[英]Why C++ STL map is slow Java' s map class?

My input file is 2GB and in this file every line is a word.我的输入文件是 2GB,在这个文件中每一行都是一个词。 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!我用 Java 和 C++ 做同样的任务,但结果令人惊讶:C++ 太慢了! My code are as follows:我的代码如下:

C++: 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输出:成本:257.62 秒

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输出:成本:124 秒

I delete the code inside the while, just read the file but do not do anything, the result is the same.我把while里面的代码删掉了,只是读取文件什么都不做,结果还是一样。 Java cost: 32s, C++ cost: 38s. Java 成本:32 秒,C++ 成本:38 秒。 This gap I can accept.这个差距我可以接受。 My environment is Ubuntu Linux 13.04 and C++ use -O2 optimation.我的环境是 Ubuntu Linux 13.04,C++ 使用 -O2 优化。 Why does the STL perform poorly?为什么 STL 表现不佳?

The C++std::map is an ordered data-structure usually implemented as a tree. C++std::map是一种有序的数据结构,通常以树的形式实现。 A fairer comparison would be betweenjava.util.HashMap and std::unordered_map orjava.util.TreeMap and std::map .更公平的比较是在java.util.HashMapstd::unordered_mapjava.util.TreeMapstd::map

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM