Idle, compare the speed of several ways in which an array contains an element in Java, directly on the code
Talk was cheap, show you the code
Package Test.contain.lishaojie;
Import Java.util.Arrays;
Import Java.util.HashSet;
Import Java.util.Set;
public class Testcontain {
/**
* @param args
*/
public static void Main (string[] args) {
TODO auto-generated Method Stub
string[] arr = new string[] {"DD", "CC", "DD", "FF", "KK"};
String target = "A";
int times = 1000;//
Convert to list mode
Long startTime = System.currenttimemillis ();
for (int i = 0; I < times; i++) {
Bylist (arr, target);
}
Long endTime = System.currenttimemillis ();
Long Duration = Endtime-starttime;
SYSTEM.OUT.PRINTLN ("list mode:" + duration/1000000);
Convert to set mode
StartTime = System.currenttimemillis ();
for (int i = 0; I < times; i++) {
Byset (arr, target);
}
EndTime = System.currenttimemillis ();
Duration = Endtime-starttime;
SYSTEM.OUT.PRINTLN ("Set mode:" + duration/1000000);
Direct loop mode
StartTime = System.currenttimemillis ();
for (int i = 0; I < times; i++) {
Byforloop (arr, target);
}
EndTime = System.currenttimemillis ();
Duration = Endtime-starttime;
System.out.println ("Loop mode:" + duration/1000000);
Binary Method Search
StartTime = System.currenttimemillis ();
for (int i = 0; I < times; i++) {
Byarraysbinarysearch (arr, target);
}
EndTime = System.currenttimemillis ();
Duration = Endtime-starttime;
System.out.println ("Two-way Search:" + duration/1000000);
}
public static Boolean bylist (string[] arr, String targetvalue) {
return Arrays.aslist (arr). Contains (Targetvalue);
}
public static Boolean Byset (string[] arr, String targetvalue) {
set<string> set = new Hashset<string> (Arrays.aslist (arr));
Return Set.contains (Targetvalue);
}
public static Boolean Byforloop (string[] arr, String targetvalue) {
for (String S:arr) {
if (S.equals (Targetvalue))
return true;
}
return false;
}
public static Boolean Byarraysbinarysearch (string[] arr, String targetvalue) {
int a = Arrays.binarysearch (arr, targetvalue);
if (a > 0)
return true;
Else
return false;
}
}
The results of the operation are as follows:
List mode: 5
Set Mode: 22
Circulation Mode: 2
Dichotomy Search: 3
After a lot of data to test the cycle of the most efficient, followed by the dichotomy, and finally the list, and set because because the array is pressed into the collection type, the array elements are first traversed, and then use the collection class to do other operations. But the list way is obviously much faster than the Set method, which is why? Look directly at the code: first
@SafeVarargs
@SuppressWarnings ("VarArgs")
public static <T> list<t> aslist (T ... a) {
return new Arraylist<> (a);
}
The return is ArrayList so the set mode also takes one operation to convert ArrayList to set,
Public HashSet (collection<? extends e> c) {
Map = new Hashmap<> (Math.max ((int) (C.size ()/.75f) + 1, 16));
AddAll (c);
}
One of the AddAll methods:
public boolean addall (collection<? extends e> c) {
Boolean modified = false;
for (E e:c)
if (Add (e))
Modified = true;
return modified;
}
Once again the selection ring, so the efficiency is lower, binggo
Comparison of several ways in which an array contains an element is judged in Java