BAEL-1374 - search an array. Name changes.

This commit is contained in:
ericgoebelbecker 2017-12-01 18:02:13 -05:00
parent e4d301e81d
commit 89ab473651
1 changed files with 22 additions and 22 deletions

View File

@ -12,25 +12,25 @@ public class SearchArrayTest {
int count = 1000;
String[] array = seedArray(count);
String[] strings = seedArray(count);
long startTime = System.nanoTime();
for (int i = 0; i < count; i++) {
searchList(array, "A");
searchList(strings, "W");
}
long duration = System.nanoTime() - startTime;
System.out.println("SearchList: " + duration / 10000);
startTime = System.nanoTime();
for (int i = 0; i < count; i++) {
searchSet(array,"A");
searchSet(strings,"S");
}
duration = System.nanoTime() - startTime;
System.out.println("SearchSet: " + duration / 10000);
startTime = System.nanoTime();
for (int i = 0; i < count; i++) {
searchLoop(array, "A");
searchLoop(strings, "T");
}
duration = System.nanoTime() - startTime;
System.out.println("SearchLoop: " + duration / 10000);
@ -40,28 +40,28 @@ public class SearchArrayTest {
public void searchArrayReuseCollections() {
int count = 10000;
String[] array = seedArray(count);
String[] strings = seedArray(count);
List<String> asList = Arrays.asList(array);
Set<String> asSet = new HashSet<>(Arrays.asList(array));
List<String> asList = Arrays.asList(strings);
Set<String> asSet = new HashSet<>(Arrays.asList(strings));
long startTime = System.nanoTime();
for (int i = 0; i < count; i++) {
asList.contains("A");
asList.contains("W");
}
long duration = System.nanoTime() - startTime;
System.out.println("List: " + duration / 10000);
startTime = System.nanoTime();
for (int i = 0; i < count; i++) {
asSet.contains("A");
asSet.contains("S");
}
duration = System.nanoTime() - startTime;
System.out.println("Set: " + duration / 10000);
startTime = System.nanoTime();
for (int i = 0; i < count; i++) {
searchLoop(array, "A");
searchLoop(strings, "T");
}
duration = System.nanoTime() - startTime;
System.out.println("Loop: " + duration / 10000);
@ -73,33 +73,33 @@ public class SearchArrayTest {
public void searchArrayBinarySearch() {
int count = 10000;
String[] array = seedArray(count);
Arrays.sort(array);
String[] strings = seedArray(count);
Arrays.sort(strings);
long startTime = System.nanoTime();
for (int i = 0; i < count; i++) {
Arrays.binarySearch(array, "A");
Arrays.binarySearch(strings, "A");
}
long duration = System.nanoTime() - startTime;
System.out.println("Binary search: " + duration / 10000);
}
private boolean searchList(String[] arr, String targetValue) {
return Arrays.asList(arr).contains(targetValue);
private boolean searchList(String[] strings, String searchString) {
return Arrays.asList(strings).contains(searchString);
}
private boolean searchSet(String[] arr, String targetValue) {
Set<String> set = new HashSet<>(Arrays.asList(arr));
return set.contains(targetValue);
private boolean searchSet(String[] strings, String searchString) {
Set<String> set = new HashSet<>(Arrays.asList(strings));
return set.contains(searchString);
}
private boolean searchLoop(String[] arr, String targetValue) {
for (String s : arr) {
if (s.equals(targetValue))
private boolean searchLoop(String[] strings, String searchString) {
for (String s : strings) {
if (s.equals(searchString))
return true;
}
return true;
return false;
}
private String[] seedArray(int length) {