You are viewing a plain text version of this content. The canonical link for it is here.
Posted to fop-dev@xmlgraphics.apache.org by bu...@apache.org on 2007/11/22 18:06:31 UTC
DO NOT REPLY [Bug 43940] New: - Faster method for double formatting
DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG�
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://issues.apache.org/bugzilla/show_bug.cgi?id=43940>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND�
INSERTED IN THE BUG DATABASE.
http://issues.apache.org/bugzilla/show_bug.cgi?id=43940
Summary: Faster method for double formatting
Product: Fop
Version: 1.0dev
Platform: All
OS/Version: All
Status: NEW
Severity: minor
Priority: P2
Component: general
AssignedTo: fop-dev@xmlgraphics.apache.org
ReportedBy: jeremias@apache.org
We format a lot of double values to strings for PDF and PostScript output. We
currently use DecimalFormat in a problematic way (DecimalFormat is not
thread-safe). It also doesn't quite cover the requirements I have for the method
in that particular context. Furthermore, this has shown as a minor hotspot in
profiling sessions.
The task is to write an implementation of the following method:
static void formatDouble(double value, int decimals, int precision, StringBuffer
target)
value: the double value
decimals: the number of decimal places behind the decimal point
precision: the maximum precision for small values. If the value is between -1
and +1, the precision needs to be increased up to the number of decimal places
indicated by this parameter. This is used in contexts where transformation
matrices scale the content and a higher precision might be required.
target: the formatted characters are appended to the given StringBuffer. A
StringBuffer is chosen for efficiency so the number of String concatenations can
be reduced if possible. A wrapper method returning a String is easily written
around this method.
The goal of this method is to have a compact representation of a double value
while adressing accuracy requirements in the usage context.
the decimal point is . (".", PERIOD) (this is not dependent on the user's
locale)
A few examples (consider decimals being 4 and precision being 8):
0.0 should be rendered as "0"
0.1 should be rendered as "0.1"
1234.1 should be rendered as "1234.1"
1234.1234567 should be rendered as "1234.1235" (note the trailing 5! Rounding!)
1234.00001 should be rendered as "1234"
0.00001 should be rendered as "0.00001" (here you see the effect of the
"precision" parameter.
0.00000001 should be rendered as "0.00000001".
0.000000001 should be rendered as "0".
Acceptance criteria:
- The method has to prove to be faster than new DecimalFormat("0.######", new
DecimalFormatSymbols(Locale.US)).format(value) on Sun Java 1.4, 1.5 an 6.0.
- The method must be thread-safe.
- The method must have a javadoc comment.
- It must be accompanied by a JUnit TestCase testing all aspects of the method.
- It should not depend on any additional library not already in the dependency
list of Apache XML Graphics Commons (its destination in the end).
Any volunteers? The prize: eternal glory and a big "thank you" from me! ;-)
Otherwise, this is just a reminder for myself.
--
Configure bugmail: http://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.
DO NOT REPLY [Bug 43940] - Faster method for double formatting
Posted by bu...@apache.org.
DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG�
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://issues.apache.org/bugzilla/show_bug.cgi?id=43940>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND�
INSERTED IN THE BUG DATABASE.
http://issues.apache.org/bugzilla/show_bug.cgi?id=43940
------- Additional Comments From a_l.delmelle@pandora.be 2007-11-24 05:16 -------
(In reply to comment #1)
Slight correction
> /* append integer part*/
> target.append(intPart);
Should be:
target.append(Math.abs(intPart));
--
Configure bugmail: http://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.
DO NOT REPLY [Bug 43940] - Faster method for double formatting
Posted by bu...@apache.org.
DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG�
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://issues.apache.org/bugzilla/show_bug.cgi?id=43940>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND�
INSERTED IN THE BUG DATABASE.
http://issues.apache.org/bugzilla/show_bug.cgi?id=43940
------- Additional Comments From a_l.delmelle@pandora.be 2007-11-24 04:31 -------
Performed no timings or comparisons yet, but the result is OK for the mentioned cases.
How about:
public static void formatDouble(
double source,
int decimals,
int precision,
StringBuffer target) {
boolean isPositive = (source >= 0);
long intPart = (long) (isPositive ? Math.floor(source) : Math.ceil(source));
int scale = (intPart >= 1) ? decimals : precision;
long fracPart = (long) Math.abs(Math.round((source - intPart) * Math.pow(10, scale)));
if (intPart != 0 || fracPart != 0) {
/* non-zero value */
if (!isPositive) {
/* negative value, insert sign */
target.append('-');
}
/* append integer part*/
target.append(intPart);
if (fracPart != 0) {
/* append fractional part */
target.append('.');
/* insert leading zeroes */
while (fracPart < Math.pow(10, --scale)) {
target.append('0');
}
target.append(fracPart);
/* remove trailing zeroes */
for (int i = target.length(); --i >= 0 && target.charAt(i) == '0'; ) {
target.deleteCharAt(i);
}
}
} else {
target.append('0');
}
}
--
Configure bugmail: http://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.
DO NOT REPLY [Bug 43940] - Faster method for double formatting
Posted by bu...@apache.org.
DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG�
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://issues.apache.org/bugzilla/show_bug.cgi?id=43940>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND�
INSERTED IN THE BUG DATABASE.
http://issues.apache.org/bugzilla/show_bug.cgi?id=43940
------- Additional Comments From a_l.delmelle@pandora.be 2007-11-24 05:42 -------
... and one more correction ;-)
The scale should also depend on whether Math.abs(intPart) > 1.
Increasing the number of iterations by a factor of 10, the difference with Double.toString() remains the
same. Double.toString() is sometimes faster, probably as a result of the values generated by
Math.random().
The difference with DecimalFormat.format(), on the other hand increases. 1650-1700ms vs. almost
4000ms for the latter. I suspect this difference to be caused by the fact that DecimalFormat.format() is
an instance method, where the other two are statics.
--
Configure bugmail: http://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.
DO NOT REPLY [Bug 43940] - Faster method for double formatting
Posted by bu...@apache.org.
DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG�
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://issues.apache.org/bugzilla/show_bug.cgi?id=43940>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND�
INSERTED IN THE BUG DATABASE.
http://issues.apache.org/bugzilla/show_bug.cgi?id=43940
------- Additional Comments From a_l.delmelle@pandora.be 2007-11-24 05:03 -------
Further info: formatting 50000 random doubles
formatDouble(source, 4, 8, target) [216ms]
seems to be slightly faster than
target.append(Double.toString(source)) [236ms]
while having the benefit of the decimals and precision arguments.
It is also almost twice as fast as
DecimalFormat df = new DecimalFormat("0.########", new DecimalFormatSymbols(Locale.US));
for (int i = 50000; --i >= 0; ) {
...
target.append(df.format(source));
}
which takes 421ms on my end.
Tests run on Apple JVM 1.4.2
--
Configure bugmail: http://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.