tag:blogger.com,1999:blog-4884093826755448868.post5259309813555032751..comments2023-11-05T08:35:20.183+01:00Comments on Jevopi's Developer Blog: String.format vs. MessageFormat.format vs. String.+ vs. StringBuilder.appendJens v.P.http://www.blogger.com/profile/14089732885850015120noreply@blogger.comBlogger18125tag:blogger.com,1999:blog-4884093826755448868.post-76306058226954754662016-04-28T23:38:21.229+02:002016-04-28T23:38:21.229+02:00@Zoltan Farkas: Thank you very much for the link t...@Zoltan Farkas: Thank you very much for the link to JMH and your benchmarks!Jens v.P.https://www.blogger.com/profile/14089732885850015120noreply@blogger.comtag:blogger.com,1999:blog-4884093826755448868.post-34308113772047534542016-04-27T05:04:40.648+02:002016-04-27T05:04:40.648+02:00Here is an example JMH benchmark in spf4j for 3 me...Here is an example JMH benchmark in <a href="www.spf4j.org" rel="nofollow">spf4j</a> for 3 message formatter implementations:<br /><br /><a href="https://github.com/zolyfarkas/spf4j/blob/master/spf4j-benchmarks/src/test/java/org/spf4j/text/MessageFormatterBenchmark.java" rel="nofollow">MessageFormatterBenchmark</a>Zoltan Farkashttps://www.blogger.com/profile/13403082247059421847noreply@blogger.comtag:blogger.com,1999:blog-4884093826755448868.post-82384385879536550132016-04-27T05:02:09.083+02:002016-04-27T05:02:09.083+02:00Here is an example JMH benchmark in spf4j for 3 me...Here is an example JMH benchmark in <a href="www.spf4j.org" rel="nofollow">spf4j</a> for 3 message formatter implementations:<br /><br /><a href="https://github.com/zolyfarkas/spf4j/blob/master/spf4j-benchmarks/src/test/java/org/spf4j/text/MessageFormatterBenchmark.java" rel="nofollow">MessageFormatterBenchmark</a><br /><br /><br /><br />Zoltan Farkashttps://www.blogger.com/profile/13403082247059421847noreply@blogger.comtag:blogger.com,1999:blog-4884093826755448868.post-60944170699614939942016-04-26T18:11:04.417+02:002016-04-26T18:11:04.417+02:00Redo your benchmarks with JMH (http://openjdk.java...Redo your benchmarks with JMH (http://openjdk.java.net/projects/code-tools/jmh/) it will give a more precise and potentially different result... I have see a lot of bad decisions made from bad benchmark code...Zoltan Farkashttps://www.blogger.com/profile/13403082247059421847noreply@blogger.comtag:blogger.com,1999:blog-4884093826755448868.post-74864119040237086002013-02-13T19:49:50.930+01:002013-02-13T19:49:50.930+01:00This comment has been removed by the author.Anonymoushttps://www.blogger.com/profile/14104287341799197465noreply@blogger.comtag:blogger.com,1999:blog-4884093826755448868.post-76396352956376443012012-02-28T10:46:38.599+01:002012-02-28T10:46:38.599+01:00String.format() allows 'argument_index' to...String.format() allows 'argument_index' to be specified and is thus capable of reordering:<br /><br /> "Second = %2$s, First = %1$s"<br /><br />will reverse the order of two string arguments.Thomas Hallgrenhttps://www.blogger.com/profile/14160225243609425486noreply@blogger.comtag:blogger.com,1999:blog-4884093826755448868.post-32291734048346291592012-02-28T04:44:57.884+01:002012-02-28T04:44:57.884+01:00I think you'll find the difference between Str...I think you'll find the difference between String.format and MessageFormat to be related to internationalisation concerns. Often it is necessary to reorder terms within a message to localise for a different language. While String.format appears to be derived from the C "printf" function the {0}, {1}, etc in MessageFormat are a little more polymorphic and allow for any reordering Benjamin Carlylehttps://www.blogger.com/profile/11945994555021875350noreply@blogger.comtag:blogger.com,1999:blog-4884093826755448868.post-8630075675864184242011-10-30T17:48:39.890+01:002011-10-30T17:48:39.890+01:00How about the memory between them?How about the memory between them?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-4884093826755448868.post-68022814343941675532009-10-04T05:58:38.162+02:002009-10-04T05:58:38.162+02:00Maybe I'm focusing on the wrong thing, but I c...Maybe I'm focusing on the wrong thing, but I cringe whenever I see logging code that is wrapped in an if statement that checks the logging level. What that does is take a line of logging code, which is noise that we want to ignore 99% of the time, and turns it into 3 lines. It's ugly, ugly , UGLY.<br />The slf4j approach is much better, and also pretty much eliminates the need to worry Eric Rizzohttps://www.blogger.com/profile/08407727335073173803noreply@blogger.comtag:blogger.com,1999:blog-4884093826755448868.post-73493195283799519902009-10-01T07:42:37.884+02:002009-10-01T07:42:37.884+02:00If you want to know what compiler does then you co...If you want to know what compiler does then you could use Bytecode Outline plugin.<br />http://andrei.gmxhome.de/bytecode/index.html<br />You need a little effort to understand the VM instructions but the "Bytecode Reference" view documents it quite good also with examples.Unknownhttps://www.blogger.com/profile/15369637507187972306noreply@blogger.comtag:blogger.com,1999:blog-4884093826755448868.post-29754595854978244642009-09-30T19:00:36.858+02:002009-09-30T19:00:36.858+02:00Hi Jens,
One other comment - the loop example you...Hi Jens,<br /><br />One other comment - the loop example you have added is still being optimized away by the compiler it looks like. If you look at Laurent's example, the big difference between his and yours is that he is modifying a string outside of the loop, where as your example simply builds the string inside the loop. Here is a loop that shows where the compiler can fall down.<br /><br Unknownhttps://www.blogger.com/profile/08499659371802102353noreply@blogger.comtag:blogger.com,1999:blog-4884093826755448868.post-68455851365726637642009-09-30T16:16:32.745+02:002009-09-30T16:16:32.745+02:00@Jens:
My point was that in general you use String...@Jens:<br />My point was that in general you use StringBuilder because you can optimize things like loops where the buffers used for building the string can be reused. You can't do that when you use the '+' operator. Instead you'll have to rely on that the compiler does that for you. So StringBuilders give you more control, albeit to the cost of good readability.<br /><br />Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-4884093826755448868.post-7503069793558460382009-09-30T15:12:49.947+02:002009-09-30T15:12:49.947+02:00Oh, thanks for that information, daObi and Laurent...Oh, thanks for that information, daObi and Laurent. I didn't knew that, but as I wrote I'm not a performance freak and no VM specialist ;-)<br /><br />And of course, if a string has to be created as in your example, Laurent,I will always use a StringBuilder. In my "test" I focused on typical "toString()" methods and what I'd need there. <br /><br />I have added a Jens v.P.https://www.blogger.com/profile/14089732885850015120noreply@blogger.comtag:blogger.com,1999:blog-4884093826755448868.post-29243134756358750632009-09-30T14:24:28.468+02:002009-09-30T14:24:28.468+02:00Even though you did try to avoid compiler optimisa...Even though you did try to avoid compiler optimisation, the "+" you use here is equvalent to a StringBuilder in Java 5 and above as it is on a single line.<br /><br />Usually in a Java program, what we use if more like (Yes, I know this is exagerated, we would inline some of these ;)) :<br /><br />String s = "Iteration ";<br />s += i;<br />s += ", result '"<br />Laurent Goubethttps://www.blogger.com/profile/05605492202750997843noreply@blogger.comtag:blogger.com,1999:blog-4884093826755448868.post-56009189677137058192009-09-30T14:07:09.007+02:002009-09-30T14:07:09.007+02:00"String +" and StringBuilder are equival..."String +" and StringBuilder are equivalent.<br />E.g. JDK 1.5 compiler translates<br /><br />"Iteration " + i + ", result '" + value[i%2] + "'"<br /><br />to<br /><br />new StringBuilder("Iteration ").append(i).append(", result '").append(value[i%2]).append("'").toString()!Unknownhttps://www.blogger.com/profile/15369637507187972306noreply@blogger.comtag:blogger.com,1999:blog-4884093826755448868.post-88505454049125209172009-09-30T14:05:10.243+02:002009-09-30T14:05:10.243+02:00@Manual:
Looks nice, is slf4j in the Eclipse orbit...@Manual:<br />Looks nice, is slf4j in the Eclipse orbit, i.e. is it IP approved (the crucial question when it comes to Eclipse projects)? I used jakarta commons logging in the past, but then I switched to JDK logging, as it comes out of the box (I do not have to use another jar) and it is well supported by log4e.<br /><br />@Thomas: <br />That certainly is true, Thomas. On the other hand, the Jens v.P.https://www.blogger.com/profile/14089732885850015120noreply@blogger.comtag:blogger.com,1999:blog-4884093826755448868.post-61234670214913296672009-09-30T13:18:01.100+02:002009-09-30T13:18:01.100+02:00I bet you could make the StringBuilder outperform ...I bet you could make the StringBuilder outperform the + operator by reusing the StringBuilder instance and just call its clear() method at the beginning of each iteration.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-4884093826755448868.post-53923995461531501222009-09-30T13:05:47.450+02:002009-09-30T13:05:47.450+02:00Interesting results. For logging in particular, I ...Interesting results. For logging in particular, I found <a href="http://www.slf4j.org/" rel="nofollow">slf4j</a> to offer a good solution to this problem, e.g.<br /><br />logger.debug("Temperature set to {}. Old temperature was {}.", t, oldT);<br /><br />This style omits the StringFormat call, and supports formatting right out of the box. The other nice thing about it is that the stringManuel Woelkerhttps://www.blogger.com/profile/14669351231452909792noreply@blogger.com