tag:blogger.com,1999:blog-1567717090855451997.post6308105255861302226..comments2024-03-28T04:39:48.819+00:00Comments on Dan Dar3: Java - String.format(), Formatter.format(), StringUtils performanceDan Draguthttp://www.blogger.com/profile/09000904627186150798noreply@blogger.comBlogger7125tag:blogger.com,1999:blog-1567717090855451997.post-33372095985703599302016-09-09T20:20:31.538+01:002016-09-09T20:20:31.538+01:00Understood. I treated your article as the 3 repres...Understood. I treated your article as the 3 represented options performance comparison. Was wrong.<br /><br />As for performance analysis, eventually I collated one method and got such performance gauging results per 100 000 000 iterations:<br /><br />Total execution time of String.format = 24570<br />Average execution time of String.format = 24<br />Total execution time of formatter.format = Anonymoushttps://www.blogger.com/profile/16202579770826964288noreply@blogger.comtag:blogger.com,1999:blog-1567717090855451997.post-79634280048311911272016-09-09T13:43:46.485+01:002016-09-09T13:43:46.485+01:00Hello,
You're absolutely right, timings go ch...Hello,<br /><br />You're absolutely right, timings go change in between runs, that is why so important to run tests repeatedly to see whether the pattern repeats.<br /><br />More importantly, as I described in the post, I wrote the test to get some proof on what possibly might have helped me to sqeeze some performance out of my code with little changes, then go back to make them in my Dan Dar3https://www.blogger.com/profile/15102489451012121803noreply@blogger.comtag:blogger.com,1999:blog-1567717090855451997.post-50102830486918364222016-09-08T13:48:31.024+01:002016-09-08T13:48:31.024+01:00Hi there,
I ran your test and the results are abs...Hi there,<br /><br />I ran your test and the results are absolutely unreliable!<br />Any further method is executed quicker than previous!<br />Hence if I interchange methods or even put them in a reverse order (like StringUtils then Formatter.format then String.format) then I'm getting totally different results! A first method is always the slowest and last is always the quickest.<br /><br /Anonymoushttps://www.blogger.com/profile/16202579770826964288noreply@blogger.comtag:blogger.com,1999:blog-1567717090855451997.post-1929839891066937452014-12-29T14:46:47.332+00:002014-12-29T14:46:47.332+00:00You're perfectly right, Caleb!
You can attach...You're perfectly right, Caleb!<br /><br />You can attach the src.zip (if available) in say Eclipse:<br />http://www.mkyong.com/eclipse/eclipse-how-to-attach-jdk-source-code/<br /><br />Or you can explore the JDK sources from OpenJDK:<br />http://www.oracle.com/technetwork/articles/javase/opensourcejdk-jsp-136417.html<br /><br />and it appears String.format() in fact employs Formatter.format()Dan Dar3https://www.blogger.com/profile/15102489451012121803noreply@blogger.comtag:blogger.com,1999:blog-1567717090855451997.post-23704666057722948732014-12-28T23:43:07.883+00:002014-12-28T23:43:07.883+00:00You could just download the Java source, I think t...You could just download the Java source, I think that most IDE's support linking that, though the process is more manual than with maven.Anonymoushttps://www.blogger.com/profile/08185254298048097278noreply@blogger.comtag:blogger.com,1999:blog-1567717090855451997.post-65979127532187423702014-10-22T12:51:23.475+01:002014-10-22T12:51:23.475+01:00Very good question - at the time the JavaDoc note ...Very good question - at the time the JavaDoc note for Formatter re thread safety caugh my eye ("Formatters are not necessarily safe for multithreaded access. Thread safety is optional and is the responsibility of users of methods in this class."), but diging into it again brings another issue - Formatter writes into a destination, in this case a StringBuilder, so reusing it is not an Dan Dar3https://www.blogger.com/profile/15102489451012121803noreply@blogger.comtag:blogger.com,1999:blog-1567717090855451997.post-86017014543449993672014-10-22T11:39:22.883+01:002014-10-22T11:39:22.883+01:00I just wonder why don't you reuse the Formatte...I just wonder why don't you reuse the Formatter? thread-safe problem?Anonymousnoreply@blogger.com