Merge pull request #3254 from egoebelbecker/master

BAEL-1374 -reuse search array
This commit is contained in:
Loredana Crusoveanu 2017-12-16 08:13:55 +02:00 committed by GitHub
commit 4ae33ebdf8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 23 additions and 42 deletions

View File

@ -10,78 +10,60 @@ import java.util.concurrent.TimeUnit;
@OutputTimeUnit(TimeUnit.MICROSECONDS) @OutputTimeUnit(TimeUnit.MICROSECONDS)
public class SearchArrayTest { public class SearchArrayTest {
@State(Scope.Benchmark)
public static class SearchData {
static int count = 1000;
static String[] strings = seedArray(1000);
}
@Benchmark @Benchmark
public void searchArrayLoop() { public void searchArrayLoop() {
for (int i = 0; i < SearchData.count; i++) {
int count = 1000; searchLoop(SearchData.strings, "T");
String[] strings = seedArray(count);
for (int i = 0; i < count; i++) {
searchLoop(strings, "T");
} }
} }
@Benchmark @Benchmark
public void searchArrayAllocNewList() { public void searchArrayAllocNewList() {
for (int i = 0; i < SearchData.count; i++) {
int count = 1000; searchList(SearchData.strings, "T");
String[] strings = seedArray(count);
for (int i = 0; i < count; i++) {
searchList(strings, "W");
} }
} }
@Benchmark @Benchmark
public void searchArrayAllocNewSet() { public void searchArrayAllocNewSet() {
for (int i = 0; i < SearchData.count; i++) {
int count = 1000; searchSet(SearchData.strings, "T");
String[] strings = seedArray(count);
for (int i = 0; i < count; i++) {
searchSet(strings, "S");
} }
} }
@Benchmark @Benchmark
public void searchArrayReuseList() { public void searchArrayReuseList() {
List<String> asList = Arrays.asList(SearchData.strings);
int count = 1000; for (int i = 0; i < SearchData.count; i++) {
String[] strings = seedArray(count); asList.contains("T");
List<String> asList = Arrays.asList(strings);
for (int i = 0; i < count; i++) {
asList.contains("W");
} }
} }
@Benchmark @Benchmark
public void searchArrayReuseSet() { public void searchArrayReuseSet() {
Set<String> asSet = new HashSet<>(Arrays.asList(SearchData.strings));
int count = 1000; for (int i = 0; i < SearchData.count; i++) {
String[] strings = seedArray(count); asSet.contains("T");
Set<String> asSet = new HashSet<>(Arrays.asList(strings));
for (int i = 0; i < count; i++) {
asSet.contains("S");
} }
} }
@Benchmark @Benchmark
public void searchArrayBinarySearch() { public void searchArrayBinarySearch() {
Arrays.sort(SearchData.strings);
int count = 1000; for (int i = 0; i < SearchData.count; i++) {
String[] strings = seedArray(count); Arrays.binarySearch(SearchData.strings, "T");
Arrays.sort(strings);
long startTime = System.nanoTime();
for (int i = 0; i < count; i++) {
Arrays.binarySearch(strings, "A");
} }
long duration = System.nanoTime() - startTime;
//System.out.println("Binary search: " + duration / 10000);
} }
private boolean searchList(String[] strings, String searchString) { private boolean searchList(String[] strings, String searchString) {
@ -101,8 +83,7 @@ public class SearchArrayTest {
return false; return false;
} }
private String[] seedArray(int length) { private static String[] seedArray(int length) {
String[] strings = new String[length]; String[] strings = new String[length];
Random random = new Random(); Random random = new Random();
for (int i = 0; i < length; i++) for (int i = 0; i < length; i++)