如何测试String数组搜索的速度?

时间:2014-01-21 22:09:14

标签: java

我为Java类编写了一个程序,我正在搜索String数组中的特定目标。程序从数组的开头到数组的末尾搜索目标,然后从数组的末尾搜索到数组的开头。我应该测试两个搜索的速度,看看哪个更快。我该怎么测试呢?

以下是该计划:

public class LinearStringSearch {

    // Array filled with random Strings
    String[] randomStrings = {"apple", "yellow", "fire", "wood", "zinc", 
            "ram", "mouse", "fish", "cheese", "dirt"};

    // Save target arguments for global access(console printing purposes)
    String target;  
    String target2; 

    /**
     * 
     * @param target - the item you want to retrieve from array
     * @param sa - the name of the array
     * @return i - the target, otherwise return error code: -1
     */
    int linearStringSearch(String target, String[] sa) {
          this.target = target;  // set class variable for print access
          for(int i = 0; i < sa.length; ++i) {
             System.out.println("Searching array position: " + i);
             if (sa[i].equals(target)) {
              // System.out.println("Target found! ");
               return i;
             }   
          }
          return -1;
    }

    /**
     * 
     * @param target - the item you want to retrieve from array
     * @param sa - the name of the array
     * @return i - the target, otherwise return error code: -1
     */
    int backwardLinearStringSearch(String target, String[] sa) {
        this.target2 = target;  // set class variable for print access
        for(int i = 9; i < sa.length; --i) {
            System.out.println("Searching array position: " + i);
            if (sa[i].equals(target)) {
                return i;
            }
        }
        return -1; // -1 means that target was not found
    }

     /*
      * The target string is searched from the beginning of the array to the end of the array, then
      * from the end of the array to the beginning of the array. 
      */
    public static void main(String[] args) {

        LinearStringSearch lss = new LinearStringSearch();

        // Search array from beginning to end
        System.out.println("Linear search: ");                          // Print title  
        int index = lss.linearStringSearch("mouse", lss.randomStrings); // Pass arguments
        System.out.println("The target " + "'" + lss.target + "'" +     // Print to console
        " found at array index: "+index);    

        // Search array from end to beginning
        System.out.println("\nBackwards linear search: ");                       // Print title
        int index2 = lss.backwardLinearStringSearch("mouse", lss.randomStrings); // Pass arguments
        System.out.println("The target " + "'" + lss.target2 + "'" +             // Print to console
        " found at array index: "+index2); 
    }   
}

这是输出:

Linear search: 
Searching array position: 0
Searching array position: 1
Searching array position: 2
Searching array position: 3
Searching array position: 4
Searching array position: 5
Searching array position: 6
The target 'mouse' found at array index: 6

Backwards linear search: 
Searching array position: 9
Searching array position: 8
Searching array position: 7
Searching array position: 6
The target 'mouse' found at array index: 6

2 个答案:

答案 0 :(得分:2)

JVM非常复杂,因此如果您想获得准确而真实的结果,您必须记住JIT的影响。这意味着您需要测试已经由JIT优化的代码,并且在方法计时期间不会更改。所以你必须写microbenchmark - 你可以使用像CaliperJMH这样的库。在Caliper的这种情况下,它看起来像这样:

public class MyBenchmark extends Benchmark {
    public void timeMyOperation(int reps) {
      for (int i = 0; i < reps; i++) {
        int index = lss.linearStringSearch("mouse", lss.randomStrings);;
      }
   }
}

答案 1 :(得分:2)

看看Java Performance TestingSystem.currentTimeMillis()或更好getCurrentThreadCpuTime()是您的朋友。如果时差太小,请考虑多次运行每个测试并比较所需的时间。