• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String.format() and FontMetrics.stringWidth() - jre1.5.0_07 vs jre1.5.0_22

 
Ranch Hand
Posts: 1143
1
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Our application must use Java 5.
I noticed the performance of methods "String.format()" and "FontMetrics.stringWidth()" is much better in jre1.5.0_07 than in jre1.5.0_22
Perhaps someone can help me understand why?
Here is the code I used to test the methods:

Output I got when I ran the above code:
With jre1.5.0_07, I got ...
String.format() = 7021200
FontMetrics.stringWidth() = 2101975

With jre1.5.0_22, I got ...
String.format() = 154023576
FontMetrics.stringWidth() = 7462347

Cheers,
Avi.
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Avi,
When benchmarking, you want to run the code in a loop many times. The JVM often optimizes as you go. Can you confirm that running a loop 1000 times still has the pattern you are observing.

Also, you'll want to confirm that this is a performance problem in your actual application. If it is, you could re-write the formatting logic for these two specific cases by yourself.

Also, you might try removing the %s and %n to see if that helps.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you worrying about the difference between two Java5 versions? Update to Java7(u79/80) or Java8(u60).
 
Avi Abrami
Ranch Hand
Posts: 1143
1
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perhaps the Performance forum was not the most appropriate?
Jeanne, I did not mention benchmarking. I asked if someone knows why there is a huge difference between the two JRE versions.
In any event the above code, in my application, runs only once anyway. The formatted String serves as an entry in a combo box list.
I calculate the greatest (String) width in order to determine the width of the combo box's dropdown list.

Campbell, for your information, our application does not run correctly using Java 7 - but that's another story.
And also for your information, I ran the above code - which is merely meant to serve as a SSCCI - under Java 7 and got the following results:

jre1.7.0_79
String.format() = 464975000
FontMetrics.stringWidth() = 23905617

In other words, jre1.7.0_79 is three times slower than jre1.5.0_22

Cheers,
Avi.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Avi Abrami wrote:Jeanne, I did not mention benchmarking. I asked if someone knows why there is a huge difference between the two JRE versions.



If you didn't do a proper benchmark then you can't assert (yet) anything about the performance of the two versions. And as Jeanne pointed out, you didn't do a proper benchmark. She also suggested a way to improve what you did, to make it more meaningful.
 
Avi Abrami
Ranch Hand
Posts: 1143
1
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After some more Google-ing, I discovered that this is a known problem.
In fact there is a bug about it.
FontMetrics.stringWidth() is slow if the String contains non-latin characters.
In my case the Strings contain Hebrew characters.
In my application, I retrieve about 500 Strings from a database and invoke FontMetrics.stringWidth() on each one - does that serve as a proper benchmark?
In any case, I believe my original question is still the issue and that is:

Why is FontMetrics.stringWidth() faster in jdk1.5.0_07?

In order to obtain an answer to that question, I guess I should compare the source code for the method in the different java versions.
So I will now Google for the source code - but if someone can point me in the right direction, I would appreciate it.

Cheers,
Avi.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can download legacy versions of the JDK from the Oracle website. Look on the normal download page for archives. Install the old versions of Java8 and search their installation folders for a file called src.zip. Most of the standard classes' source code is to be found in there.
reply
    Bookmark Topic Watch Topic
  • New Topic