java - 在进行基准测试时,ReadTime 在 HashMap 查找中意味着什么?

标签 java performance hashmap performance-testing

我正在对 HashMap 上的插入和读取时间进行一些性能测试,只是为了好玩,看看与其他数据结构相比,HashMap 的性能如何。

我有一个文本文件,其中包含 100 万个英语单词,其频率采用这种格式 -

hello 100
world 5000
good 2000
bad 9000
...

现在我正在逐行读取此文件并将其存储在 HashMap 中,以便我能够使用以下代码测量插入性能。

Map<String, String> wordTest = new HashMap<String, String>();

try {
    fis = new FileInputStream(FILE_LOCATION);
    reader = new BufferedReader(new InputStreamReader(fis));

    long startTime = System.nanoTime();
    String line = reader.readLine();
    while (line != null) {
    // split the string on whitespace
    String[] splitString = line.split("\\s+");
    // now put it in HashMap as key value  pair
    wordTest.put(splitString[0].toLowerCase().trim(), splitString[1].trim());

    line = reader.readLine();
    }
    long endTime = System.nanoTime() - startTime;
    System.out.println("Insertion Time: " +TimeUnit.MILLISECONDS.convert(endTime, TimeUnit.NANOSECONDS));
}

现在我还想在 HashMap 中测量读取性能。我知道如何从 HashMap 获取值,但不确定读取时间这意味着什么?意思是从HashMap中查找一个字符串需要多少时间?

基本上,在看了这个链接后我很困惑 - https://github.com/jpountz/tries/wiki/Benchmark 。在此链接中,他们有 ReadTime 但不确定它是什么意思?

所以我的问题是我的问题,如果我需要计算ReadTime一般意味着什么?我应该对从 HashMap 或其他东西进行单个字符串查找花费的时间进行基准测试吗?

或者一般来说,如果我想从 HashMap 测量 ReadTime 我应该怎么做?

最佳答案

注意:我从来没有建议您它会给您带来完美的基准测试结果。 它只是在 HashMap 中读写随机值的示例代码。

    String atoz = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQURSTUVWXYZ0123456789";

    Map<String, String> wordTest = new HashMap<String, String>();

    //write logic

    long startTime = System.currentTimeMillis();
    for (int i = 0; i < 24223400; i++) {
        int begin1 = 1 + (int) (Math.random() * ((atoz.length() - 1) + 1));
        int end1 = begin1 + (int) (Math.random() * ((atoz.length() - begin1) + 1));
        int begin2 = 1 + (int) (Math.random() * ((atoz.length() - 1) + 1));
        int end2 = begin2 + (int) (Math.random() * ((atoz.length() - begin2) + 1));

        wordTest.put(atoz.substring(begin1, end1), atoz.substring(begin2, end2));
    }
    long endTime = System.currentTimeMillis();
    System.out.println("Time taken:" + (endTime - startTime) + " ms to insert "
            + wordTest.size() + " records.");


    // Read logic  

    String atoz1 = "ABCDEFGHIJKLMNOPQURSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";

    int counter=0;
    long startTime1 = System.currentTimeMillis();
    for (int i = 0; i < 24223400; i++) {
        int begin1 = 1 + (int) (Math.random() * ((atoz1.length() - 1) + 1));
        int end1 = begin1 + (int) (Math.random() * ((atoz1.length() - begin1) + 1));

        if(wordTest.get(atoz1.substring(begin1, end1))==null){
            counter++;
        }
    }
    long endTime1 = System.currentTimeMillis();
    System.out.println("Time taken:" + (endTime1 - startTime1) + " ms to read " + 24223400
            + " records." + " Success hit:"+counter);

输出:

    Time taken:4440 ms to insert 1953 records.
    Time taken:2839 ms to read 24223400 records. Success hit:8743257

注意:所有键排列都是在 1953 条记录之后完成的,因此请尝试使用其他随机字符串和逻辑。

关于java - 在进行基准测试时,ReadTime 在 HashMap 查找中意味着什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23039414/

相关文章:

java - 将键值合并为另一个具有 java 中重复项的映射的新值

Java TreeMap put vs HashMap put,自定义对象作为键

ajax - 在我的 ASP.NET MVC 应用程序中将 JSON 数据检索到 jqGrid 时,我应该使用 POST 还是 GET?

c# - WebClient 下载速度非常慢

java - HashMap 键的顺序究竟何时受到影响,

java - 如何在 Olingo OData V4 java API 中创建枚举实体类型

performance - 什么是 L1 Cache Reference 或 Main Memory Reference

java - 如何用 JAXB 将空值表示为空元素?

java - 在 Android 中添加到外部数据库

反序列化 protobuf 消息时出现 java.lang.NoClassDefFoundError