java - 将一种数据结构与另一种数据结构进行比较,导致运行时间超过 50 分钟

标签 java performance hashmap spelling

我正在编写读取文本文件(每行一条推文)的代码,然后遍历每条推文,将其与英语单词列表进行比较,以查看该单词是否拼写错误。

所以英语单词列表也是从文本文件中读取的,然后将其存储在列表中。当我单独为此运行代码时,它会在不到一秒的时间内运行。当我运行用于将每个单词存储在 1,000,000 条推文的推文文件中的代码(不检查拼写)时,它将每个单词及其频率存储在 HashMap<String, Integer> 中。在大约 20-30 秒内。

但是,当我添加一行代码来检查单词是否拼写正确时,它会导致运行时间大幅增加,以至于我几乎可以在电影运行完毕之前观看它。

调用的简单方面 isSpelledCorrectly(X) (它只调用 list.contains(x) ,最坏情况下的运行时间为 O(n)),但它导致代码从 30 秒运行时间变为 50 分钟运行时间似乎相当令人困惑?

代码:

拼写:

static List<String> spellCheck = new ArrayList<String>();

public AssignTwo() throws IOException{
    spellCheck = initCorrectSpelling("C:\\Users\\Gregs\\InfoRetrieval\\src\\english-words");
}

public static List<String> initCorrectSpelling(String filename) throws IOException { //store correct spelling of words in list
    Scanner scanner = new Scanner(new FileInputStream(filename));
    try{
        while(scanner.hasNextLine()){
            String next = scanner.nextLine();
            spellCheck.add(next);
        }
    }
    finally{
        scanner.close();
    }
    return spellCheck;
}

public static boolean isSpelledCorrectly(String word){  //check if any given word is spelled correctly by seeing if it is 
    boolean output = false;                             //contained within the spellCheck list
    if(spellCheck.contains(word)) output = true;                    
    return output;
}

存储推文的代码:

public static HashMap<String, Integer> misSpell;
public AssignOne() throws IOException {         //read in file from path, test functions
    index("C:\\Users\\Gregs\\InfoRetrieval\\src\\tweets");
}

public static void index(String filename)  throws IOException {
    misSpell = new HashMap<String, Integer>();
    Scanner scanner = new Scanner(new FileInputStream(filename));
    try{
        while(scanner.hasNextLine()){
            String line = scanner.nextLine();       
            String[] lineArr = line.split(" ");
            for(int i=3; i<lineArr.length; i++){
                int count=1;
                lineArr[i] = lineArr[i].replaceAll("[^a-zA-Z0-9]", "");
                //if(!AssignTwo.isSpelledCorrectly(lineArr[i].toLowerCase())){  //with this line commented out, runtime <30sec, with line >50mins
                    if(misSpell.containsKey(lineArr[i].toLowerCase())){             
                        count = 1 + misSpell.get(lineArr[i].toLowerCase());             
                    }
                    misSpell.put(lineArr[i].toLowerCase(), count);
                //}
            }
        }
    } 
    finally{
        scanner.close();
    }
}

关于在哪里改进代码或如何使比较更有效的任何建议?对于正确拼写的单词,是否有更快的数据结构?

最佳答案

List.contains() 是 O(N),N 是字典中的单词数。

使用 HashSet,其中 contains() 是 O(1)。

使用缓冲阅读器也会加快速度。并且避免对每个单词调用 toLowerCase() 三次。

关于java - 将一种数据结构与另一种数据结构进行比较,导致运行时间超过 50 分钟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40817767/

相关文章:

performance - Webgl:低 fps 和约 72% 的空闲时间

Java - 迭代 HashMap 替换 ArrayList

java - 从 java 执行 MYSQL 命令时出现错误。相同的命令在 Linux 终端上运行良好。可能是什么原因?

java - 如何在 JPA 存储库方法中使用部分组合键来获取数据?

linux - Linux 中的时间命令显示的用户和系统时间过高

c++ - 处理结构数组 : all in one place vs splitting vs arrays within structs 的正确方法

java - 覆盖 ArrayList<String> 中的 HashMap 中的值

hashmap - Hashmap 和 Hashtable 理论上有什么区别?

java - 以编程方式访问 'res/raw' 或 Assets 文件夹中的 PDF 文件以使用给定方法进行解析

java - 为什么我的 ImageButtons 不能为我的滑动益智游戏随机播放?