Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.36 KB | None | 0 0
  1. package test;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7. import java.util.Random;
  8. import java.util.stream.IntStream;
  9.  
  10. public class Log2vsApproxLog2 {
  11.  
  12. public static void main(String[] args) {
  13. final int TOTAL_REPETITIONS = 1000;
  14.  
  15. final int RUNS = 10;
  16. final int REPEATS_PER_RUN = 100000;
  17.  
  18. final int WARMUPS = 5;
  19. final int ARRAY_SIZE = 100000000;
  20.  
  21. Map<String, List<Long>> totalStats = new HashMap<>();
  22. for(int mode=0; mode<2; mode++) {
  23. String key = mode%2 == 0 ? "log2" : "approxLog2";
  24. totalStats.put(key, new ArrayList<Long>());
  25. }
  26.  
  27. IntStream.range(0, TOTAL_REPETITIONS + WARMUPS).sequential().forEach(repetition -> {
  28. Random r = new Random(System.currentTimeMillis());
  29.  
  30. Timer timer = new Timer();
  31. double[] randoms = new double[ARRAY_SIZE];
  32. for(int i=0; i<randoms.length; i++)
  33. randoms[i] = r.nextDouble();
  34. System.out.println(String.format("\ngenerating %d random double values took %d ms", randoms.length, timer.durationAsMilliS()));
  35.  
  36. Map<String, List<Long>> runStats = new HashMap<>();
  37. Timer nt;
  38.  
  39. for(int mode=0; mode<2; mode++) {
  40. String key = (mode + repetition) %2 == 0 ? "log2" : "approxLog2";
  41. if(!runStats.containsKey(key))
  42. runStats.put(key, new ArrayList<Long>());
  43.  
  44. for(int run = 0; run < RUNS + WARMUPS; run++) {
  45. if(key.equals("log2")) {
  46. nt = new Timer();
  47. for(int i=0; i<REPEATS_PER_RUN; i++)
  48. for(double d: randoms)
  49. log2(d);
  50. long time = nt.durationAsNanoS();
  51. if(run >= WARMUPS)
  52. runStats.get(key).add(time);
  53. } else {
  54. nt = new Timer();
  55. for(int i=0; i<REPEATS_PER_RUN; i++)
  56. for(double d: randoms)
  57. approxLog2(d);
  58. long time = nt.durationAsNanoS();
  59. if(run >= WARMUPS)
  60. runStats.get(key).add(time);
  61. }
  62. }
  63. }
  64.  
  65. if(repetition >= WARMUPS) {
  66. System.out.println("Stats: " + runStats);
  67. for(String key: runStats.keySet()) {
  68. long average = (long)runStats.get(key).stream().mapToLong((x) -> x).summaryStatistics().getAverage();
  69. totalStats.get(key).add(average);
  70. System.out.println("Average time for " + key + " -> " + average + " ns");
  71. }
  72. } else {
  73. System.out.println("warmup round");
  74. }
  75.  
  76. });
  77.  
  78. System.out.println("\n\nFINAL RESULTS FOR " + TOTAL_REPETITIONS + " REPETITIONS");
  79. for(String key: totalStats.keySet())
  80. System.out.println(String.format("Total Stats time for %s -> %s", key, totalStats.get(key).stream().mapToLong((x) -> x).summaryStatistics()));
  81.  
  82. }
  83.  
  84.  
  85. static final double lg2 = Math.log(2);
  86.  
  87. public static double log2(double n) {
  88. return (Math.log(n) / lg2);
  89. }
  90.  
  91. public static double approxLog2(double value) {
  92. final long valueBits = Double.doubleToRawLongBits(value);
  93. final long exponent = ((valueBits & 0x7ff0_0000_0000_0000L) >>> 52) - 1024;
  94. final double m = Double.longBitsToDouble((valueBits & 0x800fffffffffffffL) | 0x3ff0000000000000L);
  95. return (m * (2 - (1.0 / 3) * m) + exponent - (2.0 / 3.0));
  96. }
  97.  
  98.  
  99. static class Timer {
  100. final static long KILO = 1000L;
  101.  
  102. private long start;
  103.  
  104. public Timer() {
  105. start = System.nanoTime();
  106. }
  107.  
  108. public long durationAsNanoS() {
  109. return System.nanoTime() - start;
  110. }
  111.  
  112. public long durationAsMicroS() {
  113. return durationAsNanoS()/KILO;
  114. }
  115.  
  116. public long durationAsMilliS() {
  117. return durationAsMicroS()/KILO;
  118. }
  119. }
  120.  
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement