使用不同方法查找数组中某个特定值(对象),并计算时间(个人练习)
来源:互联网 发布:两个蓝牙单片机通信 编辑:程序博客网 时间:2024/06/16 00:42
设计了几种遍历方式,来看那种最快,先来看结果,后面附有实现方式,后续写代码可根据需求选择哪种方式
数组长度小于1000时,使用自定义Loop最快; 数组长度很大时,使用Arrays.binarySearch()速度最快.
第一种数组长度10000, 总评价:Arrays.binarySearch()速度最快,其次是自定义Loop(不稳定),List速度不如Set
执行第一次:
使用Loop查找花费的时间为183040使用Arrays.binarySearch()查找花费的时间为36693
使用UseList查找花费的时间为187733
使用UseSet查找花费的时间为150187
执行第二次:
使用Loop查找花费的时间为93013
使用Arrays.binarySearch()查找花费的时间为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);}//假设查询数字666int 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查询数组中是否含有特定值@Overridepublic 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;}@Overridepublic 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;}@Overridepublic int search() {Set set = new HashSet(Arrays.asList(arr));if(set.contains(targetValue))return 1;return -1;}}
- 使用不同方法查找数组中某个特定值(对象),并计算时间(个人练习)
- 使用不同方法查找数组中某个特定值,并计算时间(改正二分法查找错误和数组转化集合错误)
- 查找字符串中某个特定的单词出现的次数(练习)
- 当某个类的prototype对象 中有数组时,通过该类的对象 并使用push方法就可以改变原型对象中数组的值
- 在数组(序列、有序集合)中查找特定数组
- 从有序数组中查找某个值(二分)
- js删除数组中某个特定的对象
- 在特定数组中查找最小整数(生成id的o(n)方法)
- 个人练习数据结构之--------------关于线性数据的有序数组以及之上的二分法查找、不同排序方法的学习
- 在数组中寻找某个特定整型值的存储位置,并还回一个指向该位置的指针
- 使用Python处理目录(二):计算某个目录下特定文件类型中文件总行数
- 枚举 数组中查找某个值
- 有关在某个数组中查找一个值的算法(Python实现)
- 二分搜索典型应用(一)从有序数组中查找某个值
- java几种不同方法查询数组中包括某个值时所用的速度
- 查找有序二维数组中是否包含特定值
- 数组排序(按对象中某个属性进行排序)
- 面试题:如何在一千万个不重复整数(电话号码)中查找某个特定数 位运算 bitmap
- leetcode-515. Find Largest Value in Each Tree Row
- 恢复旋转排序数组
- vue.js 初体验
- 学习淘淘商城第二十二课(KindEditor富文本编辑器的使用)
- Eclipse中设置作者日期等信息
- 使用不同方法查找数组中某个特定值(对象),并计算时间(个人练习)
- linux启动级别
- 浅谈JavaScript实现关键字文本高亮显示
- flask笔记:12:flask与nodejs通讯跨域问题解决
- 在线购物系统-实验1
- Android如何在命令模式下使用adb命令
- C#.NET学习心得(一)
- A + B Problem II
- vs visual studio 2017 片段行为 在标识符后键入 ?-Tab 时包含片段 智能提示(IntelliSense)