Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //DEFINE DatePerfTest.java
- import java.time.DayOfWeek;
- import java.time.LocalDate;
- import java.util.Arrays;
- import java.util.List;
- public class DatePerfTest {
- public static abstract class BaseTest extends Test {
- private static final long serialVersionUID = -4273919037149657344L;
- protected final LocalDate startDate = LocalDate.of(2014, 9, 7);
- protected final LocalDate endDate = startDate.plusYears(100);
- protected final List<DayOfWeek> ignore = Arrays.asList(
- DayOfWeek.SUNDAY, DayOfWeek.WEDNESDAY, DayOfWeek.FRIDAY);
- }
- public static class TestA extends BaseTest {
- private static final long serialVersionUID = 7230009098369787163L;
- @Override
- public void execute() {
- DateCountingUtils.countDaysIgnoreWithLoop(startDate, endDate, ignore);
- }
- }
- public static class TestB extends BaseTest {
- private static final long serialVersionUID = 460788287988852763L;
- @Override
- public void execute() {
- DateCountingUtils.countDaysIgnoreWithStream(startDate, endDate, ignore);
- }
- }
- public static void main(String... args) {
- TestRunner testRunner = new TestRunner(new TestA(), new TestB());
- testRunner.run(10000);
- testRunner.printResults();
- // Sample output:
- // > test0: 1023882.44 ns
- // > test1: 1754700.63 ns
- }
- }
- //DEFINE DateCountingUtils.java
- import java.time.DayOfWeek;
- import java.time.LocalDate;
- import java.time.temporal.ChronoUnit;
- import java.util.List;
- import java.util.stream.Stream;
- public class DateCountingUtils {
- public static void dispDate(String label, LocalDate date) {
- System.out.printf("%s: %s -> %s%n", label, date, date.getDayOfWeek());
- }
- public static int countDays(final LocalDate start,
- final LocalDate end) {
- return (int) ChronoUnit.DAYS.between(start, end);
- }
- public static int countDaysIgnoreWithLoop(final LocalDate start,
- final LocalDate end, final List<DayOfWeek> ignore) {
- int count = 0;
- LocalDate curr = start.plusDays(0);
- while (curr.isBefore(end) /* || curr.isEqual(end) */) {
- if (!ignore.contains(curr.getDayOfWeek())) {
- count++;
- }
- curr = curr.plusDays(1);
- }
- return count;
- }
- public static int countDaysIgnoreWithStream(LocalDate start, LocalDate end,
- List<DayOfWeek> ignore) {
- return (int) Stream.iterate(start, d -> d.plusDays(1))
- .limit(start.until(end, ChronoUnit.DAYS))
- .filter(d -> !ignore.contains(d.getDayOfWeek())).count();
- }
- }
- //DEFINE TestRunner.java
- import java.util.Arrays;
- import java.util.List;
- public class TestRunner {
- private List<Test> tests;
- public List<Test> getTests() {
- return tests;
- }
- public void setTests(List<Test> tests) {
- this.tests = tests;
- }
- public TestRunner(List<Test> tests) {
- this.tests = tests;
- }
- public TestRunner(Test... tests) {
- this(Arrays.asList(tests));
- }
- public void run(int runCount) {
- for (Test test : tests) {
- test.run(runCount);
- }
- }
- public void printResults() {
- for (Test test : tests) {
- System.out.println(test);
- }
- }
- public void addTest(Test test) {
- tests.add(test);
- }
- }
- //DEFINE Test.java
- import java.io.Serializable;
- public abstract class Test implements Serializable {
- private static final long serialVersionUID = 73867217431980486L;
- public static int TEST_ID;
- private final String label;
- private float avgRunTime;
- public float getAvgRunTime() {
- return avgRunTime;
- }
- public Test() {
- this("test" + TEST_ID++);
- }
- public Test(String label) {
- this.label = label;
- }
- public abstract void execute();
- private final long timeIt() {
- long start = System.nanoTime();
- execute();
- return System.nanoTime() - start;
- }
- public final void run(int count) {
- long elapsedTime = 0;
- //System.out.print(label + ": {");
- for (int i = 0; i < count; i++) {
- long duration = timeIt();
- //System.out.printf("%d,",duration);
- elapsedTime += duration;
- }
- //System.out.println("}");
- avgRunTime = elapsedTime / (float) count;
- }
- @Override
- public String toString() {
- return String.format("%s: %.2f ns", label,
- avgRunTime);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement