ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-11-12 03:03:43  阅读:161  来源: 互联网

标签:performance hashmap spelling java


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

因此,还可以从文本文件中读取英语单词列表,然后将其存储在列表中.当我单独运行此代码时,它的运行时间不到一秒钟.当我运行用于将1,000,000条Tweet的每个单词存储在tweet文件中(无需检查拼写)的代码时,它将每个单词及其频率存储在HashMap< String,Integer>中.在20-30秒左右.

但是,当我添加一行以检查该单词的拼写是否正确时,这会导致运行时间的增加,达到荒谬的程度,以至于我几乎可以在看完电影之前看完​​电影.

调用的简单方面是SpelledCorrectly(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()三次.

标签:performance,hashmap,spelling,java
来源: https://codeday.me/bug/20191112/2023981.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有