blob: 953513b89bf25ad334f35eed777452e78bc1b642 [file] [log] [blame]
/*
* Copyright (C) 2009 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package benchmarks.regression;
import com.google.caliper.Param;
import com.google.caliper.Runner;
import com.google.caliper.SimpleBenchmark;
/**
* Tests various Character methods, intended for testing multiple
* implementations against each other.
*/
public class CharacterBenchmark extends SimpleBenchmark {
@Param private CharacterSet characterSet;
@Param private Overload overload;
private char[] chars;
@Override protected void setUp() throws Exception {
this.chars = characterSet.chars;
}
public enum Overload { CHAR, INT }
@Override public double nanosToUnits(double nanos) {
return nanos / 65536;
}
public enum CharacterSet {
ASCII(128),
UNICODE(65536);
final char[] chars;
CharacterSet(int size) {
this.chars = new char[65536];
for (int i = 0; i < 65536; ++i) {
chars[i] = (char) (i % size);
}
}
}
// A fake benchmark to give us a baseline.
public boolean timeIsSpace(int reps) {
boolean dummy = false;
if (overload == Overload.CHAR) {
for (int i = 0; i < reps; ++i) {
for (int ch = 0; ch < 65536; ++ch) {
dummy ^= ((char) ch == ' ');
}
}
} else {
for (int i = 0; i < reps; ++i) {
for (int ch = 0; ch < 65536; ++ch) {
dummy ^= (ch == ' ');
}
}
}
return dummy;
}
public void timeDigit(int reps) {
if (overload == Overload.CHAR) {
for (int i = 0; i < reps; ++i) {
for (int ch = 0; ch < 65536; ++ch) {
Character.digit(chars[ch], 10);
}
}
} else {
for (int i = 0; i < reps; ++i) {
for (int ch = 0; ch < 65536; ++ch) {
Character.digit((int) chars[ch], 10);
}
}
}
}
public void timeGetNumericValue(int reps) {
if (overload == Overload.CHAR) {
for (int i = 0; i < reps; ++i) {
for (int ch = 0; ch < 65536; ++ch) {
Character.getNumericValue(chars[ch]);
}
}
} else {
for (int i = 0; i < reps; ++i) {
for (int ch = 0; ch < 65536; ++ch) {
Character.getNumericValue((int) chars[ch]);
}
}
}
}
public void timeIsDigit(int reps) {
if (overload == Overload.CHAR) {
for (int i = 0; i < reps; ++i) {
for (int ch = 0; ch < 65536; ++ch) {
Character.isDigit(chars[ch]);
}
}
} else {
for (int i = 0; i < reps; ++i) {
for (int ch = 0; ch < 65536; ++ch) {
Character.isDigit((int) chars[ch]);
}
}
}
}
public void timeIsIdentifierIgnorable(int reps) {
if (overload == Overload.CHAR) {
for (int i = 0; i < reps; ++i) {
for (int ch = 0; ch < 65536; ++ch) {
Character.isIdentifierIgnorable(chars[ch]);
}
}
} else {
for (int i = 0; i < reps; ++i) {
for (int ch = 0; ch < 65536; ++ch) {
Character.isIdentifierIgnorable((int) chars[ch]);
}
}
}
}
public void timeIsJavaIdentifierPart(int reps) {
if (overload == Overload.CHAR) {
for (int i = 0; i < reps; ++i) {
for (int ch = 0; ch < 65536; ++ch) {
Character.isJavaIdentifierPart(chars[ch]);
}
}
} else {
for (int i = 0; i < reps; ++i) {
for (int ch = 0; ch < 65536; ++ch) {
Character.isJavaIdentifierPart((int) chars[ch]);
}
}
}
}
public void timeIsJavaIdentifierStart(int reps) {
if (overload == Overload.CHAR) {
for (int i = 0; i < reps; ++i) {
for (int ch = 0; ch < 65536; ++ch) {
Character.isJavaIdentifierStart(chars[ch]);
}
}
} else {
for (int i = 0; i < reps; ++i) {
for (int ch = 0; ch < 65536; ++ch) {
Character.isJavaIdentifierStart((int) chars[ch]);
}
}
}
}
public void timeIsLetter(int reps) {
if (overload == Overload.CHAR) {
for (int i = 0; i < reps; ++i) {
for (int ch = 0; ch < 65536; ++ch) {
Character.isLetter(chars[ch]);
}
}
} else {
for (int i = 0; i < reps; ++i) {
for (int ch = 0; ch < 65536; ++ch) {
Character.isLetter((int) chars[ch]);
}
}
}
}
public void timeIsLetterOrDigit(int reps) {
if (overload == Overload.CHAR) {
for (int i = 0; i < reps; ++i) {
for (int ch = 0; ch < 65536; ++ch) {
Character.isLetterOrDigit(chars[ch]);
}
}
} else {
for (int i = 0; i < reps; ++i) {
for (int ch = 0; ch < 65536; ++ch) {
Character.isLetterOrDigit((int) chars[ch]);
}
}
}
}
public void timeIsLowerCase(int reps) {
if (overload == Overload.CHAR) {
for (int i = 0; i < reps; ++i) {
for (int ch = 0; ch < 65536; ++ch) {
Character.isLowerCase(chars[ch]);
}
}
} else {
for (int i = 0; i < reps; ++i) {
for (int ch = 0; ch < 65536; ++ch) {
Character.isLowerCase((int) chars[ch]);
}
}
}
}
public void timeIsSpaceChar(int reps) {
if (overload == Overload.CHAR) {
for (int i = 0; i < reps; ++i) {
for (int ch = 0; ch < 65536; ++ch) {
Character.isSpaceChar(chars[ch]);
}
}
} else {
for (int i = 0; i < reps; ++i) {
for (int ch = 0; ch < 65536; ++ch) {
Character.isSpaceChar((int) chars[ch]);
}
}
}
}
public void timeIsUpperCase(int reps) {
if (overload == Overload.CHAR) {
for (int i = 0; i < reps; ++i) {
for (int ch = 0; ch < 65536; ++ch) {
Character.isUpperCase(chars[ch]);
}
}
} else {
for (int i = 0; i < reps; ++i) {
for (int ch = 0; ch < 65536; ++ch) {
Character.isUpperCase((int) chars[ch]);
}
}
}
}
public void timeIsWhitespace(int reps) {
if (overload == Overload.CHAR) {
for (int i = 0; i < reps; ++i) {
for (int ch = 0; ch < 65536; ++ch) {
Character.isWhitespace(chars[ch]);
}
}
} else {
for (int i = 0; i < reps; ++i) {
for (int ch = 0; ch < 65536; ++ch) {
Character.isWhitespace((int) chars[ch]);
}
}
}
}
public void timeToLowerCase(int reps) {
if (overload == Overload.CHAR) {
for (int i = 0; i < reps; ++i) {
for (int ch = 0; ch < 65536; ++ch) {
Character.toLowerCase(chars[ch]);
}
}
} else {
for (int i = 0; i < reps; ++i) {
for (int ch = 0; ch < 65536; ++ch) {
Character.toLowerCase((int) chars[ch]);
}
}
}
}
public void timeToUpperCase(int reps) {
if (overload == Overload.CHAR) {
for (int i = 0; i < reps; ++i) {
for (int ch = 0; ch < 65536; ++ch) {
Character.toUpperCase(chars[ch]);
}
}
} else {
for (int i = 0; i < reps; ++i) {
for (int ch = 0; ch < 65536; ++ch) {
Character.toUpperCase((int) chars[ch]);
}
}
}
}
}