Re: [Geotools-devel] Question about string concatenation in geotools code
this being conditionally executed code (specifically the value java.util.logging.Level.FINE) I think performance is not a relevant argument for applying this patch; it will only be run by people actually needing this level of debugging, I'm pretty sure they would not be concerned with performance.. As for readability, using "new" is, I tink, in general bad for readability. mark -- 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 Geotools-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/geotools-devel
Re: [Geotools-devel] Question about string concatenation in geotools code
you are right the problem was with "+=" not with the simple "+" Simone --- Ing. Simone Giannecchini GeoSolutions S.A.S. Founder - Software Engineer Via Carignoni 51 55041 Camaiore (LU) Italy phone: +39 0584983027 fax: +39 0584983027 mob:+39 333 8128928 http://www.geo-solutions.it http://geo-solutions.blogspot.com/ http://www.linkedin.com/in/simonegiannecchini http://twitter.com/simogeo --- On Fri, Jul 2, 2010 at 1:02 PM, Andrea Aime wrote: > Simone Giannecchini ha scritto: >> string1 + string2 + string3 >> >> becomes >> >> temp=new StringBuffer(string1).append(string2) >> temp2= new StringBuffer(temp),apend(string3) >> >> result = temp2.toString(); >> >> and this was true until the last time I checked, which was a while >> ago, but It was at least using Java 5. > > My javap dump seems to suggest otherwise. I'm using Java 5 as well. > > Cheers > Andrea > >>> Compiled from "Concatenate.java" >>> public class Concatenate extends java.lang.Object{ >>> public Concatenate(); >>> Code: >>> 0: aload_0 >>> 1: invokespecial #8; //Method java/lang/Object."":()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."":(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."":(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 >>> >>> } >>> > > > -- > 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 > Geotools-devel@lists.sourceforge.net > https://lists.sourceforge.net/lists/listinfo/geotools-devel > -- 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 Geotools-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/geotools-devel
Re: [Geotools-devel] Question about string concatenation in geotools code
Simone Giannecchini ha scritto: > string1 + string2 + string3 > > becomes > > temp=new StringBuffer(string1).append(string2) > temp2= new StringBuffer(temp),apend(string3) > > result = temp2.toString(); > > and this was true until the last time I checked, which was a while > ago, but It was at least using Java 5. My javap dump seems to suggest otherwise. I'm using Java 5 as well. Cheers Andrea >> Compiled from "Concatenate.java" >> public class Concatenate extends java.lang.Object{ >> public Concatenate(); >> Code: >>0: aload_0 >>1: invokespecial #8; //Method java/lang/Object."":()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."":(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."":(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 >> >> } >> -- 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 Geotools-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/geotools-devel
Re: [Geotools-devel] Question about string concatenation in geotools code
Jody Garnett ha scritto: > Hi Andrea: > > StringBuilder is supposed to be slightly faster (and non threadsafe); > but it is murder on readability. Slightly faster than StringBuffer, but if you look the bytecode below you'll see normal string concatenation already uses it. So there is no difference between using StringBuilder and direct concatenation. > I used a java 5 trick in some GeoTools code that I am starting to be > very fond of. Using the Object... param syntax to hide the > concatenation. > > If we were add such a method to LOGGER the the example would be: > > LOGGER.fine( "Got empty intersection for granule ", this, " with > request ", request ); Mind this is creating an Object[] in memory every time it's called, so it's not zero cost. However it avoids the concatenation if the current logging level is below the FINE threshold, so it's a good way to avoid the if(Logger.isLoggable(...)) for non tight loops. For tight loops array creation will kill you anyways, I think Gabriel had a horror story about using varargs in his binary gml encoder and getting a solid and non obvious slowdown Cheers Andrea >> Compiled from "Concatenate.java" public class Concatenate extends >> java.lang.Object{ public Concatenate(); Code: 0: aload_0 1: >> invokespecial#8; //Method java/lang/Object."":()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."":(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."":(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 -- 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 Geotools-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/geotools-devel
Re: [Geotools-devel] Question about string concatenation in geotools code
string1 + string2 + string3 becomes temp=new StringBuffer(string1).append(string2) temp2= new StringBuffer(temp),apend(string3) result = temp2.toString(); and this was true until the last time I checked, which was a while ago, but It was at least using Java 5. Simone --- Ing. Simone Giannecchini GeoSolutions S.A.S. Founder - Software Engineer Via Carignoni 51 55041 Camaiore (LU) Italy phone: +39 0584983027 fax: +39 0584983027 mob:+39 333 8128928 http://www.geo-solutions.it http://geo-solutions.blogspot.com/ http://www.linkedin.com/in/simonegiannecchini http://twitter.com/simogeo --- On Fri, Jul 2, 2010 at 12:38 PM, Andrea Aime wrote: > 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."":()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."":(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."":(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:
Re: [Geotools-devel] Question about string concatenation in geotools code
Hi Andrea: StringBuilder is supposed to be slightly faster (and non threadsafe); but it is murder on readability. I used a java 5 trick in some GeoTools code that I am starting to be very fond of. Using the Object... param syntax to hide the concatenation. If we were add such a method to LOGGER the the example would be: LOGGER.fine( "Got empty intersection for granule ", this, " with request ", request ); Jody On 02/07/2010, at 8:38 PM, Andrea Aime wrote: > 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."":()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."":(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."":(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.