如何优化 java 代码 - 运行 时间

How to optimize java code - run time

我有以下代码花费了我的时间 运行。关于如何优化它以使其变得更好更快的任何建议?

                for (int tIndex = 0; tIndex < numTopics; tIndex++) {
                    double beta0 = sumTopicWordCount[tIndex] + betaSum;
                    int m0 = 0;
                    double expectWT = 1;
                    // getting the number of total words (or word w) in sentence i
                    List<String> sentenceStat = new ArrayList<String>();
                    for(int wIndex=0 ; wIndex<sentence.size() ; wIndex++){
                        sentenceStat.add(id2WordVocabulary.get(document.get(sIndex).get(wIndex)));
                    }
                    Set<String> unique = new HashSet<String>(sentenceStat);
                    for(String key : unique){
                        int cnt = Collections.frequency(sentenceStat, key);
                        double betaw = topicWordCount[tIndex][word2IdVocabulary.get(key)] + beta;
                        for (int m = 0; m < cnt; m++) {
                            expectWT *= (betaw + m) / (beta0 + m0);
                            m0++;
                        }
                    }
                    multiPros[tIndex] = (docTopicCount[sIndex][tIndex] + alpha) * expectWT;
                }

问题在于您在循环中重复扫描数据:Collections.frequency 一遍又一遍地扫描整个列表。

您不仅可以列出独特的元素,还可以一次性计算它们。我假设下面 Java 5-7;在 Java 8 中它会被缩短并且可能更快。

   Map<String, Integer> unique = new HashMap<String, Integer>();
   for(String s: sentenceStat) {
       Integer cnt = unique.get(s);
       if (cnt == null) {
           unique.put(s, 1);
       } else {
           unique.put(s, cnt + 1);
       }
   }
   for(Map.Entry<String, Integer> key : unique.entrySet()){
       String key = entry.getKey();
       int cnt = entry.getValue();
       double betaw = topicWordCount[tIndex][word2IdVocabulary.get(key)] + beta;
       for (int m = 0; m < cnt; m++) {
           expectWT *= (betaw + m) / (beta0 + m0);
           m0++;
       }
   }