您的位置:首页 > 其它

使用不同方法查找数组中某个特定值(对象),并计算时间(个人练习)

2017-04-15 13:52 555 查看
设计了几种遍历方式,来看那种最快,先来看结果,后面附有实现方式,后续写代码可根据需求选择哪种方式

数组长度小于1000时,使用自定义Loop最快; 数组长度很大时,使用Arrays.binarySearch()速度最快.

第一种数组长度10000,  总评价:Arrays.binarySearch()速度最快,其次是自定义Loop(不稳定),List速度不如Set

执行第一次:

使用Loop查找花费的时间为183040

使用Arrays.binarySearch()查找花费的时间为36693

使用UseList查找花费的时间为187733
使用UseSet查找花费的时间为150187

执行第二次:

使用Loop查找花费的时间为93013

使用Arrays.binaryS
4000
earch()查找花费的时间为35840

使用UseList查找花费的时间为283307

使用UseSet查找花费的时间为272640

第二种数组长度1000000,总评价:Arrays.binarySearch()速度最快,Loop最慢,List速度不如Set

执行第一次;

使用Loop查找花费的时间为4436905

使用Arrays.binarySearch()查找花费的时间为33707

使用UseList查找花费的时间为193280

使用UseSet查找花费的时间为149760

执行第二次:

使用Loop查找花费的时间为4009812

使用Arrays.binarySearch()查找花费的时间为34133

使用UseList查找花费的时间为185600

使用UseSet查找花费的时间为144640

第三种数组长度1000, 总评价: Loop最快,其次是Arrays.binarySearch(); List速度不如Set

执行第一次:

使用Loop查找花费的时间为20907

使用Arrays.binarySearch()查找花费的时间为49494

使用UseList查找花费的时间为200533

使用UseSet查找花费的时间为247040

执行第二次:

使用Loop查找花费的时间为27734

使用Arrays.binarySearch()查找花费的时间为53334

使用UseList查找花费的时间为303360

使用UseSet查找花费的时间为269227

附上测试代码:

package test1;

//在数组中查找某个特定值,如何查找最快?测试看看。。
//如何在数组中找出某个特定值,并计算执行时间
public class TestQuery1 {
public static void main(String[] args) {
//定义一个含有10000个数字的数组
int[] arr = new int[1000];
for(int i = 0; i < arr.length; i++){
arr[i] = (int) (Math.random()*1000);
}

//假设查询数字666
int targetValue = 666;

//使用Loop查找时间
UseLoop useLoop = new UseLoop(arr, targetValue);
useLoop.spendTime("Loop");

//使用Arrays.binarySearch()方法查询
UseArraysBinary useAB = new UseArraysBinary(arr, targetValue);
useAB.spendTime("Arrays.binarySearch()");

//使用集合List查询
UseList useList = new UseList(arr, targetValue);
useList.spendTime("UseList");

//使用集合set查询
UseSet useSet = new UseSet(arr, targetValue);
useSet.spendTime("UseSet");
}
}


Spend Time抽象类,计算花费时间:
package test1;

public abstract class SpendTime {

protected int[] arr;
protected int targetValue;

public abstract int search();

public void spendTime(String name){
long start = System.nanoTime();
search();
long end = System.nanoTime();
long time = end - start;
System.out.println("使用"+name+"查找花费的时间为"+time);
}
}使用Loop:
package test1;

public class UseLoop extends SpendTime{
//使用Loop查找数组是否包含某个值

public UseLoop(int[] arr, int targetValue){
this.arr = arr;
this.targetValue = targetValue;
}

public int search() {
for(int i =0; i < arr.length; i++){
if (arr[i]== targetValue){
return i;
}
}
return -1;
}
}


使用Arrays.binarySearch()
package test1;

import java.util.Arrays;

public class UseArraysBinary extends SpendTime{
public UseArraysBinary(int[] arr, int targetValue){
this.arr = arr;
this.targetValue = targetValue;
}
//使用Arrays.binarySearch查询数组中是否含有特定值
@Override
public int search() {
return Arrays.binarySearch(arr, targetValue);
}
}使用List
package test1;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class UseList extends SpendTime{

public UseList(int[] arr, int targetValue){
this.arr = arr;
this.targetValue = targetValue;
}

@Override
public int search() {
List list = Arrays.asList(arr);
if(list.contains(targetValue))
return 1;
return -1;
}
}使用Set
package test1;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class UseSet extends SpendTime{
public UseSet(int[] arr, int targetValue){
this.arr = arr;
this.targetValue = targetValue;
}
@Override
public int search() {
Set set = new HashSet(Arrays.asList(arr));
if(set.contains(targetValue))
return 1;
return -1;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐