Created
November 23, 2015 09:10
-
-
Save relgames/ad4ce8251bf69ebd391a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.relgames; | |
import org.openjdk.jmh.annotations.Benchmark; | |
import org.openjdk.jmh.runner.Runner; | |
import org.openjdk.jmh.runner.RunnerException; | |
import org.openjdk.jmh.runner.options.Options; | |
import org.openjdk.jmh.runner.options.OptionsBuilder; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.Collections; | |
import java.util.Iterator; | |
import java.util.List; | |
public class MyBenchmark { | |
@Benchmark | |
public List<String> manualSort() { | |
List<String> result = new ArrayList<>(6); | |
List<String> list1 = Arrays.asList("1", "4", "7"); | |
List<String> list2 = Arrays.asList("1", "2", "6"); | |
Iterator<String> iterator1 = list1.iterator(); | |
Iterator<String> iterator2 = list2.iterator(); | |
String first = iterator1.next(); | |
String last = iterator2.next(); | |
for (int i = 0; i < list1.size() + list2.size(); i++) { | |
if ((last == null || (first != null && first.compareTo(last) < 0))) { | |
result.add(first); | |
first = iterator1.hasNext() ? iterator1.next() : null; | |
} else { | |
result.add(last); | |
last = iterator2.hasNext() ? iterator2.next() : null; | |
} | |
} | |
return result; | |
} | |
@Benchmark | |
public List<String> libSort() { | |
List<String> result = new ArrayList<>(6); | |
List<String> list1 = Arrays.asList("1","4","7"); | |
List<String> list2 = Arrays.asList("1", "2", "6"); | |
result.addAll(list1); | |
result.addAll(list2); | |
Collections.sort(result); | |
return result; | |
} | |
public static void main(String[] args) throws RunnerException { | |
Options opt = new OptionsBuilder() | |
.include(MyBenchmark.class.getSimpleName()) | |
.forks(1) | |
.build(); | |
new Runner(opt).run(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment