Advertisement
MrPolywhirl

Java 8 Date Performance Test

Sep 12th, 2014
634
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.08 KB | None | 0 0
  1. //DEFINE DatePerfTest.java
  2.  
  3. import java.time.DayOfWeek;
  4. import java.time.LocalDate;
  5. import java.util.Arrays;
  6. import java.util.List;
  7.  
  8. public class DatePerfTest {
  9.     public static abstract class BaseTest extends Test {
  10.         private static final long serialVersionUID = -4273919037149657344L;
  11.  
  12.         protected final LocalDate startDate = LocalDate.of(2014, 9, 7);
  13.         protected final LocalDate endDate = startDate.plusYears(100);
  14.         protected final List<DayOfWeek> ignore = Arrays.asList(
  15.                 DayOfWeek.SUNDAY, DayOfWeek.WEDNESDAY, DayOfWeek.FRIDAY);
  16.     }
  17.  
  18.     public static class TestA extends BaseTest {
  19.         private static final long serialVersionUID = 7230009098369787163L;
  20.  
  21.         @Override
  22.         public void execute() {
  23.             DateCountingUtils.countDaysIgnoreWithLoop(startDate, endDate, ignore);
  24.         }
  25.     }
  26.  
  27.     public static class TestB extends BaseTest {
  28.         private static final long serialVersionUID = 460788287988852763L;
  29.  
  30.         @Override
  31.         public void execute() {
  32.             DateCountingUtils.countDaysIgnoreWithStream(startDate, endDate, ignore);
  33.         }
  34.     }
  35.  
  36.     public static void main(String... args) {
  37.         TestRunner testRunner = new TestRunner(new TestA(), new TestB());
  38.  
  39.         testRunner.run(10000);
  40.         testRunner.printResults();
  41.  
  42.         // Sample output:
  43.         // > test0: 1023882.44 ns
  44.         // > test1: 1754700.63 ns
  45.     }
  46. }
  47.  
  48. //DEFINE DateCountingUtils.java
  49.  
  50. import java.time.DayOfWeek;
  51. import java.time.LocalDate;
  52. import java.time.temporal.ChronoUnit;
  53. import java.util.List;
  54. import java.util.stream.Stream;
  55.  
  56. public class DateCountingUtils {
  57.     public static void dispDate(String label, LocalDate date) {
  58.         System.out.printf("%s: %s -> %s%n", label, date, date.getDayOfWeek());
  59.     }
  60.  
  61.     public static int countDays(final LocalDate start,
  62.             final LocalDate end) {
  63.         return (int) ChronoUnit.DAYS.between(start, end);
  64.     }
  65.  
  66.     public static int countDaysIgnoreWithLoop(final LocalDate start,
  67.             final LocalDate end, final List<DayOfWeek> ignore) {
  68.         int count = 0;
  69.         LocalDate curr = start.plusDays(0);
  70.  
  71.         while (curr.isBefore(end) /* || curr.isEqual(end) */) {
  72.             if (!ignore.contains(curr.getDayOfWeek())) {
  73.                 count++;
  74.             }
  75.  
  76.             curr = curr.plusDays(1);
  77.         }
  78.  
  79.         return count;
  80.     }
  81.  
  82.     public static int countDaysIgnoreWithStream(LocalDate start, LocalDate end,
  83.             List<DayOfWeek> ignore) {
  84.         return (int) Stream.iterate(start, d -> d.plusDays(1))
  85.                 .limit(start.until(end, ChronoUnit.DAYS))
  86.                 .filter(d -> !ignore.contains(d.getDayOfWeek())).count();
  87.     }
  88. }
  89.  
  90. //DEFINE TestRunner.java
  91.  
  92. import java.util.Arrays;
  93. import java.util.List;
  94.  
  95. public class TestRunner {
  96.  
  97.     private List<Test> tests;
  98.  
  99.     public List<Test> getTests() {
  100.         return tests;
  101.     }
  102.  
  103.     public void setTests(List<Test> tests) {
  104.         this.tests = tests;
  105.     }
  106.  
  107.     public TestRunner(List<Test> tests) {
  108.         this.tests = tests;
  109.     }
  110.  
  111.     public TestRunner(Test... tests) {
  112.         this(Arrays.asList(tests));
  113.     }
  114.  
  115.     public void run(int runCount) {
  116.         for (Test test : tests) {
  117.             test.run(runCount);
  118.         }
  119.     }
  120.  
  121.     public void printResults() {
  122.         for (Test test : tests) {
  123.             System.out.println(test);
  124.         }
  125.     }
  126.  
  127.     public void addTest(Test test) {
  128.         tests.add(test);
  129.     }
  130. }
  131.  
  132. //DEFINE Test.java
  133.  
  134. import java.io.Serializable;
  135.  
  136. public abstract class Test implements Serializable {
  137.     private static final long serialVersionUID = 73867217431980486L;
  138.  
  139.     public static int TEST_ID;
  140.     private final String label;
  141.     private float avgRunTime;
  142.  
  143.     public float getAvgRunTime() {
  144.         return avgRunTime;
  145.     }
  146.  
  147.     public Test() {
  148.         this("test" + TEST_ID++);
  149.     }
  150.  
  151.     public Test(String label) {
  152.         this.label = label;
  153.     }
  154.  
  155.     public abstract void execute();
  156.  
  157.     private final long timeIt() {
  158.         long start = System.nanoTime();
  159.         execute();
  160.         return System.nanoTime() - start;
  161.     }
  162.  
  163.     public final void run(int count) {
  164.         long elapsedTime = 0;
  165.         //System.out.print(label + ": {");
  166.  
  167.         for (int i = 0; i < count; i++) {
  168.             long duration = timeIt();
  169.  
  170.             //System.out.printf("%d,",duration);
  171.             elapsedTime += duration;
  172.         }
  173.  
  174.         //System.out.println("}");
  175.         avgRunTime = elapsedTime / (float) count;
  176.     }
  177.  
  178.     @Override
  179.     public String toString() {
  180.         return String.format("%s: %.2f ns", label,
  181.                 avgRunTime);
  182.     }
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement