
Eonwe
By: a guest on Apr 18th, 2009 | syntax:
Java | size: 2.75 KB | hits: 60 | expires: Never
public class ShiftTest {
private final static int MIN_VAL = 0;
private final static int SHIFT = 5;
private final static int DIVISOR = 1 << SHIFT;
public static long testShift(int maxInt) {
long sum = 0;
for (int i = MIN_VAL; i < maxInt; i++) {
sum += i >> SHIFT;
}
return sum;
}
public static long testDivide(int maxInt) {
long sum = 0;
for (int i = MIN_VAL; i < maxInt; i++) {
sum += i / DIVISOR;
}
return sum;
}
public static Pair<Long, Long> testDividing(int maxInt, int shift) {
long time1
= System.
currentTimeMillis();
long val1 = testShift(maxInt);
time1
= System.
currentTimeMillis() - time1
;
long time2
= System.
currentTimeMillis();
long val2 = testDivide(maxInt);
time2
= System.
currentTimeMillis() - time2
;
if (val1 != val2) {
}
return Pair.of(time1, time2);
}
public static void main
(String...
ignored) {
final int maxInt
= Integer.
MAX_VALUE;
final int divisor = 1 << 1;
Pair<Long, Long> result = testDividing(maxInt, divisor);
System.
out.
printf("Testing to %d, divisor %d%n", maxInt, divisor
);
System.
out.
printf("Shift: %d%nDivision: %d%n", result.
first, result.
second);
}
/**
* Small typed utility class for pairs.
* Has public field for the values of pair and
* those values cannot be null.
*
* @param <T>
* @param <Y>
*/
public static class Pair
<T,Y
> implements Cloneable{
public final T first;
public final Y second;
/**
*
* @param first non-null
* @param second non-null
*/
public Pair(T first, Y second){
if (first == null || second == null){
}
this.first = first;
this.second = second;
}
public Pair(Pair<T,Y> source){
first = source.first;
second = source.second;
}
public Pair<T,Y> clone(){
try{
@SuppressWarnings("unchecked")
Pair<T,Y> cloned = (Pair<T, Y>) super.clone();
return cloned;
getClass().getCanonicalName()+
"does not support clone "+
"even though it should");
}
}
public static <T,Y> Pair<T,Y> of(T first, Y second){
return new Pair<T,Y>(first, second);
}
public int hashCode(){
return first.hashCode() ^ second.hashCode();
}
return "{"+first.toString()+", "+second.toString()+"}";
}
public boolean equals
(Object o
){
if (o instanceof Pair)
{
Pair<?,?> other = (Pair<?, ?>) o;
return first.equals(other.first) && second.equals(other.second);
}
return false;
}
}
}