Hi,
I'm looking at Daniele's last commit and wondering if it's
really an improvement as stated in the commit log
(Improved loggings using StringBuilder appends instead of String
chaining). Here is the patch:
- if (LOGGER.isLoggable(java.util.logging.Level.FINE))
- LOGGER.fine("Got empty intersection for
granule "+this.toString()+ " with request "+request.toString());
+ if (LOGGER.isLoggable(java.util.logging.Level.FINE)){
+ LOGGER.fine(new StringBuilder("Got empty
intersection for granule ").append(this.toString())
+ .append(" with request
").append(request.toString()).append(" Resulting in no granule loaded:
Empty result").toString());
+ }
Ok, hard to read pasted like that, but you get the idea, instead
of concatenating together multiple string an inline StringBuilder
is used.
This certainly makes the code harder to read. Does it make it
faster, more scalable?
As far as I know, it does not, the version concatenating strings
is actually exactly the same as the one using StringBuilder once
compiled. From the java.lang.String javadoc:
----------------------------------------------------------------
The Java language provides special support for the string concatenation
operator ( + ), and for conversion of other objects to strings. String
concatenation is implemented through the StringBuilder(or StringBuffer)
class and its append method. String conversions are implemented through
the method toString, defined by Object and inherited by all classes in
Java. For additional information on string concatenation and conversion,
see Gosling, Joy, and Steele, The Java Language Specification.
----------------------------------------------------------------
To prove it's actually just counter productive to use explicit
StringBuilders for direct concatenation (as opposed to string
building in a loop or over more lines of code)
I wrote this little java code:
public class Concatenate {
public static void main(String[] args) {
String s1 = "abc " + Math.random() + " def";
String s2 = new
StringBuilder("abc").append(Math.random()).append("def").toString();
}
}
and then went on the command line and got a disassembly of the generated
bytecode using javap, here we go:
Compiled from "Concatenate.java"
public class Concatenate extends java.lang.Object{
public Concatenate();
Code:
0: aload_0
1: invokespecial #8; //Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]);
Code:
0: new #16; //class java/lang/StringBuilder
3: dup
4: ldc #18; //String abc
6: invokespecial #20; //Method
java/lang/StringBuilder."<init>":(Ljava/lang/String;)V
9: invokestatic #23; //Method java/lang/Math.random:()D
12: invokevirtual #29; //Method
java/lang/StringBuilder.append:(D)Ljava/lang/StringBuilder;
15: ldc #33; //String def
17: invokevirtual #35; //Method
java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
20: invokevirtual #38; //Method
java/lang/StringBuilder.toString:()Ljava/lang/String;
23: astore_1
24: new #16; //class java/lang/StringBuilder
27: dup
28: ldc #42; //String abc
30: invokespecial #20; //Method
java/lang/StringBuilder."<init>":(Ljava/lang/String;)V
33: invokestatic #23; //Method java/lang/Math.random:()D
36: invokevirtual #29; //Method
java/lang/StringBuilder.append:(D)Ljava/lang/StringBuilder;
39: ldc #44; //String def
41: invokevirtual #35; //Method
java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
44: invokevirtual #38; //Method
java/lang/StringBuilder.toString:()Ljava/lang/String;
47: astore_2
48: return
}
Can anybody see a difference between the first and the second
calculation?
If there is actually none, please, let's favor readability.
Cheers
Andrea
--
Andrea Aime
OpenGeo - http://opengeo.org
Expert service straight from the developers.
------------------------------------------------------------------------------
This SF.net email is sponsored by Sprint
What will you do first with EVO, the first 4G phone?
Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
_______________________________________________
Geotools-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-devel