-
Notifications
You must be signed in to change notification settings - Fork 3
Loop Benchmarks
Marcelo Sousa edited this page May 6, 2015
·
14 revisions
- ActivateComparator:
public int compare(Object o1, Object o2) {
if (o1 == null && o2 == null) {
return 0;
}
if (o1 == null) {
return -1;
}
if (o2 == null) {
return 1;
}
if (o1.equals(o2)) {
return 0;
}
Activate a1 = o1.getClass().getAnnotation(Activate.class);
Activate a2 = o2.getClass().getAnnotation(Activate.class);
if ((a1.before().length > 0 || a1.after().length > 0
|| a2.before().length > 0 || a2.after().length > 0)
&& o1.getClass().getInterfaces().length > 0
&& o1.getClass().getInterfaces()[0].isAnnotationPresent(SPI.class)) {
ExtensionLoader<?> extensionLoader = ExtensionLoader.getExtensionLoader(o1.getClass().getInterfaces()[0]);
if (a1.before().length > 0 || a1.after().length > 0) {
String n2 = extensionLoader.getExtensionName(o2.getClass());
for (String before : a1.before()) {
if (before.equals(n2)) {
return -1;
}
}
for (String after : a1.after()) {
if (after.equals(n2)) {
return 1;
}
}
}
if (a2.before().length > 0 || a2.after().length > 0) {
String n1 = extensionLoader.getExtensionName(o1.getClass());
for (String before : a2.before()) {
if (before.equals(n1)) {
return 1;
}
}
for (String after : a2.after()) {
if (after.equals(n1)) {
return -1;
}
}
}
}
int n1 = a1 == null ? 0 : a1.order();
int n2 = a2 == null ? 0 : a2.order();
return n1 > n2 ? 1 : -1;
}
-
AsciiString: contains a bunch of String comparators (case sensitive and insensitive) that use CharSequence.
-
Attributes:
public final int compare (final Attribute arg0, final Attribute arg1) {
return (int)(arg0.type - arg1.type);
}
public int compareTo (Attributes other) {
if (other == this)
return 0;
if (mask != other.mask)
return mask < other.mask ? -1 : 1;
sort();
other.sort();
for (int i = 0; i < attributes.size; i++) {
final int c = attributes.get(i).compareTo(other.attributes.get(i));
if (c != 0)
return c;
}
return 0;
}
- ByteBuffer:
public int compareTo (ByteBuffer otherBuffer) {
int compareRemaining = (remaining() < otherBuffer.remaining()) ? remaining() : otherBuffer.remaining();
int thisPos = position;
int otherPos = otherBuffer.position;
byte thisByte, otherByte;
while (compareRemaining > 0) {
thisByte = get(thisPos);
otherByte = otherBuffer.get(otherPos);
if (thisByte != otherByte) {
return thisByte < otherByte ? -1 : 1;
}
thisPos++;
otherPos++;
compareRemaining--;
}
return remaining() - otherBuffer.remaining();
}
- ByteBufUtil
public static int compare(ByteBuf bufferA, ByteBuf bufferB) {
final int aLen = bufferA.readableBytes();
final int bLen = bufferB.readableBytes();
final int minLength = Math.min(aLen, bLen);
final int uintCount = minLength >>> 2;
final int byteCount = minLength & 3;
int aIndex = bufferA.readerIndex();
int bIndex = bufferB.readerIndex();
if (bufferA.order() == bufferB.order()) {
for (int i = uintCount; i > 0; i --) {
long va = bufferA.getUnsignedInt(aIndex);
long vb = bufferB.getUnsignedInt(bIndex);
if (va > vb) {
return 1;
}
if (va < vb) {
return -1;
}
aIndex += 4;
bIndex += 4;
}
} else {
for (int i = uintCount; i > 0; i --) {
long va = bufferA.getUnsignedInt(aIndex);
long vb = swapInt(bufferB.getInt(bIndex)) & 0xFFFFFFFFL;
if (va > vb) {
return 1;
}
if (va < vb) {
return -1;
}
aIndex += 4;
bIndex += 4;
}
}
for (int i = byteCount; i > 0; i --) {
short va = bufferA.getUnsignedByte(aIndex);
short vb = bufferB.getUnsignedByte(bIndex);
if (va > vb) {
return 1;
}
if (va < vb) {
return -1;
}
aIndex ++;
bIndex ++;
}
return aLen - bLen;
}
- CaseIgnoringComparator
public int compare(CharSequence o1, CharSequence o2) {
int o1Length = o1.length();
int o2Length = o2.length();
int min = Math.min(o1Length, o2Length);
for (int i = 0; i < min; i++) {
char c1 = o1.charAt(i);
char c2 = o2.charAt(i);
if (c1 != c2) {
c1 = Character.toUpperCase(c1);
c2 = Character.toUpperCase(c2);
if (c1 != c2) {
c1 = Character.toLowerCase(c1);
c2 = Character.toLowerCase(c2);
if (c1 != c2) {
return c1 - c2;
}
}
}
}
return o1Length - o2Length;
}
- ChannelBuffers:
public static int compare(ChannelBuffer bufferA, ChannelBuffer bufferB) {
final int aLen = bufferA.readableBytes();
final int bLen = bufferB.readableBytes();
final int minLength = Math.min(aLen, bLen);
int aIndex = bufferA.readerIndex();
int bIndex = bufferB.readerIndex();
for (int i = minLength; i > 0; i--) {
byte va = bufferA.getByte(aIndex);
byte vb = bufferB.getByte(bIndex);
if (va > vb) {
return 1;
} else if (va < vb) {
return -1;
}
aIndex++;
bIndex++;
}
return aLen - bLen;
}
- CharBuffer
public int compareTo (CharBuffer otherBuffer) {
int compareRemaining = (remaining() < otherBuffer.remaining()) ? remaining() : otherBuffer.remaining();
int thisPos = position;
int otherPos = otherBuffer.position;
char thisByte, otherByte;
while (compareRemaining > 0) {
thisByte = get(thisPos);
otherByte = otherBuffer.get(otherPos);
if (thisByte != otherByte) {
return thisByte < otherByte ? -1 : 1;
}
thisPos++;
otherPos++;
compareRemaining--;
}
return remaining() - otherBuffer.remaining();
}
- ColumnsComparator
public int compare(Object obj1, Object obj2) {
Object[] column1 = (Object[])obj1;
Object[] column2 = (Object[])obj2;
String columnName1 = (String)column1[0];
String columnName2 = (String)column2[0];
int x = -1;
for (int i = 0; i < _columnNames.length; i++) {
if (_columnNames[i].equals(columnName1)) {
x = i;
break;
}
}
int y = -1;
for (int i = 0; i < _columnNames.length; i++) {
if (_columnNames[i].equals(columnName2)) {
y = i;
break;
}
}
if ((x == -1) && (y > -1)) {
return 1;
}
else if ((x > -1) && (y == -1)) {
return -1;
}
else if ((x > -1) && (y > -1)) {
if (x < y) {
return -1;
}
else if (x > y) {
return 1;
}
}
return 0;
}
- CompositeRequestCondition (this class does not implement Comparable or Comparator)
public int compareTo(CompositeRequestCondition other, HttpServletRequest request) {
if (isEmpty() && other.isEmpty()) {
return 0;
}
else if (isEmpty()) {
return 1;
}
else if (other.isEmpty()) {
return -1;
}
else {
assertNumberOfConditions(other);
for (int i = 0; i < getLength(); i++) {
int result = this.requestConditions[i].compareTo(other.requestConditions[i], request);
if (result != 0) {
return result;
}
}
return 0;
}
}
- CompoundComparator implements Comparator, Serializable
public int compare(T o1, T o2) {
Assert.state(this.comparators.size() > 0,
"No sort definitions have been added to this CompoundComparator to compare");
for (InvertibleComparator comparator : this.comparators) {
int result = comparator.compare(o1, o2);
if (result != 0) {
return result;
}
}
return 0;
}
- Correction implements Comparable
public int compareTo(Correction other) {
return compareTo(other.score, other.candidates);
}
int compareTo(double otherScore, Candidate[] otherCandidates) {
if (score == otherScore) {
int limit = Math.min(candidates.length, otherCandidates.length);
for (int i=0;i<limit;i++) {
int cmp = candidates[i].term.compareTo(otherCandidates[i].term);
if (cmp != 0) {
// Later (zzz) terms sort before (are weaker than) earlier (aaa) terms:
return -cmp;
}
}
return candidates.length - otherCandidates.length;
} else {
return Double.compare(score, otherScore);
}
}
- DestinationPatternsMessageCondition extends AbstractMessageCondition
public int compareTo(DestinationPatternsMessageCondition other, Message<?> message) {
String destination = (String) message.getHeaders().get(LOOKUP_DESTINATION_HEADER);
Comparator<String> patternComparator = this.pathMatcher.getPatternComparator(destination);
Iterator<String> iterator = patterns.iterator();
Iterator<String> iteratorOther = other.patterns.iterator();
while (iterator.hasNext() && iteratorOther.hasNext()) {
int result = patternComparator.compare(iterator.next(), iteratorOther.next());
if (result != 0) {
return result;
}
}
if (iterator.hasNext()) {
return -1;
}
else if (iteratorOther.hasNext()) {
return 1;
}
else {
return 0;
}
}
- abstract class DoubleBuffer extends Buffer implements Comparable
public int compareTo (DoubleBuffer otherBuffer) {
int compareRemaining = (remaining() < otherBuffer.remaining()) ? remaining() : otherBuffer.remaining();
int thisPos = position;
int otherPos = otherBuffer.position;
// BEGIN android-changed
double thisDouble, otherDouble;
while (compareRemaining > 0) {
thisDouble = get(thisPos);
otherDouble = otherBuffer.get(otherPos);
// checks for double and NaN inequality
if ((thisDouble != otherDouble) && ((thisDouble == thisDouble) || (otherDouble == otherDouble))) {
return thisDouble < otherDouble ? -1 : 1;
}
thisPos++;
otherPos++;
compareRemaining--;
}
// END android-changed
return remaining() - otherBuffer.remaining();
}
- ElementComparator implements Comparator
public int compare(Element el1, Element el2) {
String el1Name = el1.getName();
String el2Name = el2.getName();
if (!el1Name.equals(el2Name)) {
return el1Name.compareTo(el2Name);
}
String el1Text = el1.getTextTrim();
String el2Text = el2.getTextTrim();
if (!el1Text.equals(el2Text)) {
return el1Text.compareTo(el2Text);
}
List<Attribute> el1Attributes = el1.attributes();
List<Attribute> el2Attributes = el2.attributes();
if (el1Attributes.size() < el2Attributes.size()) {
return -1;
}
else if (el1Attributes.size() > el2Attributes.size()) {
return 1;
}
for (Attribute attr : el1Attributes) {
int value = _compare(
el2Attributes, attr, new AttributeComparator());
if (value != 0) {
return value;
}
}
List<Element> el1Elements = el1.elements();
List<Element> el2Elements = el2.elements();
if (el1Elements.size() < el2Elements.size()) {
return -1;
}
else if (el1Elements.size() > el2Elements.size()) {
return 1;
}
for (Element el : el1Elements) {
int value = _compare(el2Elements, el, new ElementComparator());
if (value != 0) {
return value;
}
}
return 0;
}
private int _compare(
List<Attribute> list, Attribute obj, Comparator<Attribute> comparator) {
int firstValue = -1;
for (int i = 0; i < list.size(); i++) {
Attribute o = list.get(i);
int value = comparator.compare(obj, o);
if (i == 0) {
firstValue = value;
}
if (value == 0) {
return 0;
}
}
return firstValue;
}
private int _compare(
List<Element> list, Element obj, Comparator<Element> comparator) {
int firstValue = -1;
for (int i = 0; i < list.size(); i++) {
Element o = list.get(i);
int value = comparator.compare(obj, o);
if (i == 0) {
firstValue = value;
}
if (value == 0) {
return 0;
}
}
return firstValue;
}
- abstract class FloatBuffer extends Buffer implements Comparable
public int compareTo (FloatBuffer otherBuffer) {
int compareRemaining = (remaining() < otherBuffer.remaining()) ? remaining() : otherBuffer.remaining();
int thisPos = position;
int otherPos = otherBuffer.position;
// BEGIN android-changed
float thisFloat, otherFloat;
while (compareRemaining > 0) {
thisFloat = get(thisPos);
otherFloat = otherBuffer.get(otherPos);
// checks for float and NaN inequality
if ((thisFloat != otherFloat) && ((thisFloat == thisFloat) || (otherFloat == otherFloat))) {
return thisFloat < otherFloat ? -1 : 1;
}
thisPos++;
otherPos++;
compareRemaining--;
}
// END android-changed
return remaining() - otherBuffer.remaining();
}
- abstract class IntBuffer extends Buffer implements Comparable
public int compareTo (IntBuffer otherBuffer) {
int compareRemaining = (remaining() < otherBuffer.remaining()) ? remaining() : otherBuffer.remaining();
int thisPos = position;
int otherPos = otherBuffer.position;
// BEGIN android-changed
int thisInt, otherInt;
while (compareRemaining > 0) {
thisInt = get(thisPos);
otherInt = otherBuffer.get(otherPos);
if (thisInt != otherInt) {
return thisInt < otherInt ? -1 : 1;
}
thisPos++;
otherPos++;
compareRemaining--;
}
// END android-changed
return remaining() - otherBuffer.remaining();
}
- abstract class LegacyCell implements Cell
/** Copied from {@link org.apache.lucene.util.BytesRef#compareTo(org.apache.lucene.util.BytesRef)}.
* This is to avoid creating a BytesRef. */
protected static int compare(byte[] aBytes, int aUpto, int a_length, byte[] bBytes, int bUpto, int b_length) {
final int aStop = aUpto + Math.min(a_length, b_length);
while(aUpto < aStop) {
int aByte = aBytes[aUpto++] & 0xff;
int bByte = bBytes[bUpto++] & 0xff;
int diff = aByte - bByte;
if (diff != 0) {
return diff;
}
}
// One is a prefix of the other, or, they are equal:
return a_length - b_length;
}
- abstract class LongBuffer extends Buffer implements Comparable
public int compareTo (LongBuffer otherBuffer) {
int compareRemaining = (remaining() < otherBuffer.remaining()) ? remaining() : otherBuffer.remaining();
int thisPos = position;
int otherPos = otherBuffer.position;
// BEGIN android-changed
long thisLong, otherLong;
while (compareRemaining > 0) {
thisLong = get(thisPos);
otherLong = otherBuffer.get(otherPos);
if (thisLong != otherLong) {
return thisLong < otherLong ? -1 : 1;
}
thisPos++;
otherPos++;
compareRemaining--;
}
// END android-changed
return remaining() - otherBuffer.remaining();
}
- MethodComparator implements Comparator
public int compare(Method method1, Method method2) {
String name1 = method1.getName();
String name2 = method2.getName();
int value = name1.compareTo(name2);
if (value != 0) {
return value;
}
Class<?>[] parameterTypes1 = method1.getParameterTypes();
Class<?>[] parameterTypes2 = method2.getParameterTypes();
int index = 0;
while ((index < parameterTypes1.length) &&
(index < parameterTypes2.length)) {
Class<?> parameterType1 = parameterTypes1[index];
Class<?> parameterType2 = parameterTypes2[index];
String parameterTypeName1 = parameterType1.getName();
String parameterTypeName2 = parameterType2.getName();
value = parameterTypeName1.compareTo(parameterTypeName2);
if (value != 0) {
return value;
}
index++;
}
if (index < (parameterTypes1.length -1)) {
return -1;
}
else {
return 1;
}
}
- MimeType implements Comparable, Serializable
public int compareTo(MimeType other) {
int comp = getType().compareToIgnoreCase(other.getType());
if (comp != 0) {
return comp;
}
comp = getSubtype().compareToIgnoreCase(other.getSubtype());
if (comp != 0) {
return comp;
}
comp = getParameters().size() - other.getParameters().size();
if (comp != 0) {
return comp;
}
TreeSet<String> thisAttributes = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
thisAttributes.addAll(getParameters().keySet());
TreeSet<String> otherAttributes = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
otherAttributes.addAll(other.getParameters().keySet());
Iterator<String> thisAttributesIterator = thisAttributes.iterator();
Iterator<String> otherAttributesIterator = otherAttributes.iterator();
while (thisAttributesIterator.hasNext()) {
String thisAttribute = thisAttributesIterator.next();
String otherAttribute = otherAttributesIterator.next();
comp = thisAttribute.compareToIgnoreCase(otherAttribute);
if (comp != 0) {
return comp;
}
String thisValue = getParameters().get(thisAttribute);
String otherValue = other.getParameters().get(otherAttribute);
if (otherValue == null) {
otherValue = "";
}
comp = thisValue.compareTo(otherValue);
if (comp != 0) {
return comp;
}
}
return 0;
}
- NaturalOrderStringComparator implements Comparator, Serializable
public int compare(String s1, String s2) {
if (s1 == null) {
s1 = StringPool.BLANK;
}
if (s2 == null) {
s2 = StringPool.BLANK;
}
int value = 0;
int i1 = 0;
int i2 = 0;
int length1 = s1.length();
int length2 = s2.length();
while ((i1 < length1) && (i2 < length2)) {
char c1 = s1.charAt(i1);
char c2 = s2.charAt(i2);
if (Validator.isDigit(c1) && Validator.isDigit(c2)) {
String leadingDigitsAsString1 = StringUtil.extractLeadingDigits(
s1.substring(i1));
String leadingDigitsAsString2 = StringUtil.extractLeadingDigits(
s2.substring(i2));
int leadingNumber1 = GetterUtil.getInteger(
leadingDigitsAsString1);
int leadingNumber2 = GetterUtil.getInteger(
leadingDigitsAsString2);
if (leadingNumber1 != leadingNumber2) {
value = leadingNumber1 - leadingNumber2;
break;
}
i1 += leadingDigitsAsString1.length();
i2 += leadingDigitsAsString2.length();
continue;
}
if (isCheckSpecialCharacters() && Validator.isAscii(c1) &&
Validator.isAscii(c2)) {
boolean isDigitOrLetter1 = _isDigitOrLetter(c1);
boolean isDigitOrLetter2 = _isDigitOrLetter(c2);
if (isDigitOrLetter1 ^ isDigitOrLetter2) {
if (isDigitOrLetter1) {
value = 1;
}
else {
value = -1;
}
break;
}
}
if (c1 == c2) {
i1++;
i2++;
continue;
}
if (_caseSensitive) {
value = c1 - c2;
break;
}
else {
char c1UpperCase = Character.toUpperCase(c1);
char c2UpperCase = Character.toUpperCase(c2);
if (c1UpperCase == c2UpperCase) {
i1++;
i2++;
continue;
}
value = c1UpperCase - c2UpperCase;
break;
}
}
if ((value == 0) && (length1 != length2)) {
if ((length1 == i1) && (length2 == i2)) {
value = length2 - length1;
}
else {
value = length1 - length2;
}
}
if (_ascending) {
return value;
}
else {
return -value;
}
}
- PatternsRequestCondition extends AbstractRequestCondition
public int compareTo(PatternsRequestCondition other, HttpServletRequest request) {
String lookupPath = this.pathHelper.getLookupPathForRequest(request);
Comparator<String> patternComparator = this.pathMatcher.getPatternComparator(lookupPath);
Iterator<String> iterator = this.patterns.iterator();
Iterator<String> iteratorOther = other.patterns.iterator();
while (iterator.hasNext() && iteratorOther.hasNext()) {
int result = patternComparator.compare(iterator.next(), iteratorOther.next());
if (result != 0) {
return result;
}
}
if (iterator.hasNext()) {
return -1;
}
else if (iteratorOther.hasNext()) {
return 1;
}
else {
return 0;
}
}
- ShortBuffer extends Buffer implements Comparable
public int compareTo (ShortBuffer otherBuffer) {
int compareRemaining = (remaining() < otherBuffer.remaining()) ? remaining() : otherBuffer.remaining();
int thisPos = position;
int otherPos = otherBuffer.position;
short thisByte, otherByte;
while (compareRemaining > 0) {
thisByte = get(thisPos);
otherByte = otherBuffer.get(otherPos);
if (thisByte != otherByte) {
return thisByte < otherByte ? -1 : 1;
}
thisPos++;
otherPos++;
compareRemaining--;
}
return remaining() - otherBuffer.remaining();
}
- UTF8SortedAsUnicodeComparator implements Comparator
public int compare(BytesReference a, BytesReference b) {
if (a.hasArray() && b.hasArray()) {
final byte[] aBytes = a.array();
int aUpto = a.arrayOffset();
final byte[] bBytes = b.array();
int bUpto = b.arrayOffset();
final int aStop = aUpto + Math.min(a.length(), b.length());
while (aUpto < aStop) {
int aByte = aBytes[aUpto++] & 0xff;
int bByte = bBytes[bUpto++] & 0xff;
int diff = aByte - bByte;
if (diff != 0) {
return diff;
}
}
// One is a prefix of the other, or, they are equal:
return a.length() - b.length();
} else {
final byte[] aBytes = a.toBytes();
int aUpto = 0;
final byte[] bBytes = b.toBytes();
int bUpto = 0;
final int aStop = aUpto + Math.min(a.length(), b.length());
while (aUpto < aStop) {
int aByte = aBytes[aUpto++] & 0xff;
int bByte = bBytes[bUpto++] & 0xff;
int diff = aByte - bByte;
if (diff != 0) {
return diff;
}
}
// One is a prefix of the other, or, they are equal:
return a.length() - b.length();
}
}
- VertexAttributes implements Iterable, Comparable
public int compareTo (VertexAttributes o) {
if (attributes.length != o.attributes.length) return attributes.length - o.attributes.length;
final long m1 = getMask();
final long m2 = o.getMask();
if (m1 != m2) return m1 < m2 ? -1 : 1;
for (int i = attributes.length - 1; i >= 0; --i) {
final VertexAttribute va0 = attributes[i];
final VertexAttribute va1 = o.attributes[i];
if (va0.usage != va1.usage) return va0.usage - va1.usage;
if (va0.unit != va1.unit) return va0.unit - va1.unit;
if (va0.numComponents != va1.numComponents) return va0.numComponents - va1.numComponents;
if (va0.normalized != va1.normalized) return va0.normalized ? 1 : -1;
if (va0.type != va1.type) return va0.type - va1.type;
}
return 0;
}