pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

Java pastebin - collaborative debugging tool View Help


Posted by Eonwe on Sat 18 Apr 07:59
report abuse | download | new post

  1. public class ShiftTest {
  2.  
  3.         private final static int MIN_VAL = 0;
  4.         private final static int SHIFT = 5;
  5.         private final static int DIVISOR = 1 << SHIFT;
  6.  
  7.         public static long testShift(int maxInt) {
  8.                 long sum = 0;
  9.                 for (int i = MIN_VAL; i < maxInt; i++) {
  10.                         sum += i >> SHIFT;
  11.                 }
  12.                 return sum;
  13.         }
  14.  
  15.         public static long testDivide(int maxInt) {
  16.                 long sum = 0;
  17.                 for (int i = MIN_VAL; i < maxInt; i++) {
  18.                         sum += i / DIVISOR;
  19.                 }
  20.                 return sum;
  21.         }
  22.  
  23.         public static Pair<Long, Long> testDividing(int maxInt, int shift) {
  24.  
  25.                 long time1 = System.currentTimeMillis();
  26.                 long val1 = testShift(maxInt);
  27.                 time1 = System.currentTimeMillis() - time1;
  28.                 long time2 = System.currentTimeMillis();
  29.                 long val2 = testDivide(maxInt);
  30.                 time2 = System.currentTimeMillis() - time2;
  31.  
  32.                 if (val1 != val2) {
  33.                         throw new RuntimeException("val1 != val2: " + val1 + " != " + val2);
  34.                 }
  35.                 return Pair.of(time1, time2);
  36.         }
  37.  
  38.         public static void main(String... ignored) {
  39.                 final int maxInt = Integer.MAX_VALUE;
  40.                 final int divisor = 1 << 1;
  41.                 Pair<Long, Long> result = testDividing(maxInt, divisor);
  42.                 System.out.printf("Testing to %d, divisor %d%n", maxInt, divisor);
  43.                 System.out.printf("Shift: %d%nDivision: %d%n", result.first, result.second);
  44.         }
  45.        
  46.         /**
  47.          * Small typed utility class for pairs.
  48.          * Has public field for the values of pair and
  49.          * those values cannot be null.
  50.          *
  51.          * @param <T>
  52.          * @param <Y>
  53.          */
  54.         public static class Pair<T,Y> implements Cloneable{
  55.  
  56.                 public final T first;
  57.                 public final Y second;
  58.                
  59.                 /**
  60.                  *
  61.                  * @param first non-null
  62.                  * @param second non-null
  63.                  */
  64.                 public Pair(T first, Y second){
  65.                         if (first == null || second == null){
  66.                                 throw new IllegalArgumentException("Arguments cannot be null");
  67.                         }
  68.                         this.first = first;
  69.                         this.second = second;
  70.                 }
  71.                
  72.                 public Pair(Pair<T,Y> source){
  73.                         first = source.first;
  74.                         second = source.second;
  75.                 }
  76.                
  77.                 public Pair<T,Y> clone(){
  78.                         try{
  79.                     @SuppressWarnings("unchecked")
  80.                                 Pair<T,Y> cloned = (Pair<T, Y>) super.clone();
  81.                                 return cloned;
  82.                                
  83.                         }catch (CloneNotSupportedException e){
  84.                                 throw new IllegalStateException(
  85.                                                 getClass().getCanonicalName()+
  86.                                                 "does not support clone "+
  87.                                                 "even though it should");
  88.                         }
  89.                 }
  90.                
  91.                 public static <T,Y> Pair<T,Y> of(T first, Y second){
  92.                         return new Pair<T,Y>(first, second);
  93.                 }
  94.                
  95.                 public int hashCode(){
  96.                         return first.hashCode() ^ second.hashCode();
  97.                 }
  98.  
  99.                 public String toString(){
  100.                         return "{"+first.toString()+", "+second.toString()+"}";
  101.                 }
  102.                
  103.                 public boolean equals(Object o){
  104.                         if (o instanceof Pair)
  105.                         {
  106.                                 Pair<?,?> other = (Pair<?, ?>) o;
  107.                                 return first.equals(other.first) && second.equals(other.second);
  108.                         }
  109.                         return false;
  110.                 }
  111.                
  112.         }
  113. }

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with @@


Remember me so that I can delete my post