Skip to content

Commit

Permalink
Slight performance enhancement. No measurable difference using server…
Browse files Browse the repository at this point in the history
… VM, however.

git-svn-id: https://joda-time.svn.sourceforge.net/svnroot/joda-time/trunk@603 1e1cfbb7-5c0e-0410-a2f0-f98d92ec03a1
  • Loading branch information
broneill committed Dec 12, 2004
1 parent f31d099 commit d1284b5
Showing 1 changed file with 28 additions and 8 deletions.
36 changes: 28 additions & 8 deletions JodaTime/src/java/org/joda/time/format/FormatUtils.java
Expand Up @@ -106,8 +106,13 @@ public static void appendPaddedInteger(StringBuffer buf, int value, int size) {
for (; size > 2; size--) {
buf.append('0');
}
buf.append((char)(value / 10 + '0'));
buf.append((char)(value % 10 + '0'));
// Calculate value div/mod by 10 without using two expensive
// division operations. (2 ^ 27) / 10 = 13421772. Add one to
// value to correct rounding error.
int d = ((value + 1) * 13421772) >> 27;
buf.append((char) (d + '0'));
// Append remainder by calculating (value - d * 10).
buf.append((char) (value - (d << 3) - (d << 1) + '0'));
} else {
int digits;
if (value < 1000) {
Expand Down Expand Up @@ -195,8 +200,13 @@ public static void writePaddedInteger(Writer out, int value, int size)
for (; size > 2; size--) {
out.write('0');
}
out.write(value / 10 + '0');
out.write(value % 10 + '0');
// Calculate value div/mod by 10 without using two expensive
// division operations. (2 ^ 27) / 10 = 13421772. Add one to
// value to correct rounding error.
int d = ((value + 1) * 13421772) >> 27;
out.write(d + '0');
// Append remainder by calculating (value - d * 10).
out.write(value - (d << 3) - (d << 1) + '0');
} else {
int digits;
if (value < 1000) {
Expand Down Expand Up @@ -273,8 +283,13 @@ public static void appendUnpaddedInteger(StringBuffer buf, int value) {
if (value < 10) {
buf.append((char)(value + '0'));
} else if (value < 100) {
buf.append((char)(value / 10 + '0'));
buf.append((char)(value % 10 + '0'));
// Calculate value div/mod by 10 without using two expensive
// division operations. (2 ^ 27) / 10 = 13421772. Add one to
// value to correct rounding error.
int d = ((value + 1) * 13421772) >> 27;
buf.append((char) (d + '0'));
// Append remainder by calculating (value - d * 10).
buf.append((char) (value - (d << 3) - (d << 1) + '0'));
} else {
buf.append(Integer.toString(value));
}
Expand Down Expand Up @@ -320,8 +335,13 @@ public static void writeUnpaddedInteger(Writer out, int value)
if (value < 10) {
out.write(value + '0');
} else if (value < 100) {
out.write(value / 10 + '0');
out.write(value % 10 + '0');
// Calculate value div/mod by 10 without using two expensive
// division operations. (2 ^ 27) / 10 = 13421772. Add one to
// value to correct rounding error.
int d = ((value + 1) * 13421772) >> 27;
out.write(d + '0');
// Append remainder by calculating (value - d * 10).
out.write(value - (d << 3) - (d << 1) + '0');
} else {
out.write(Integer.toString(value));
}
Expand Down

0 comments on commit d1284b5

Please sign in to comment.