java-如何测试字符串数组搜索的速度?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java-如何测试字符串数组搜索的速度?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3163字,纯文字阅读大概需要5分钟。
内容图文
![java-如何测试字符串数组搜索的速度?](/upload/InfoBanner/zyjiaocheng/678/50790824204c49d580bb9f2381a3fa49.jpg)
我为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
解决方法:
JVM非常复杂,因此,如果要获得准确和真实的结果,则必须记住有关JIT的影响.这意味着您需要测试已经由JIT优化的代码,并且在方法计时期间不会被更改.因此,您必须编写microbenchmark-可以使用Caliper或JMH之类的库.在这种情况下,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);;
}
}
}
内容总结
以上是互联网集市为您收集整理的java-如何测试字符串数组搜索的速度?全部内容,希望文章能够帮你解决java-如何测试字符串数组搜索的速度?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。