I have a problem. My application interface works much slower if i use eastern languages there. Especially i felt it in components such as JList, JCombobox, JTable.
How i found the performance of FontMetrics.stringWidth method is very slow (500+ times) if in the text at least one letter is arabic or persian. How i know it is commonly used method in various swing components.
Is there a way to boost this method performance?
Here is the example class which demonstrates the problem:
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
public class FontMetricsSpeedTest
{
public static void main( String args[] ) {
String persian="صصصصصصصصصصصصصصصصصصصصص";
String english="abcde()agjklj;lkjelwk";
FontMetrics fm=createFontMetrics(new Font("dialog",Font.PLAIN,12));
int size=50000;
long start=System.currentTimeMillis();
for(int i=0;i<size;i++)
{
fm.stringWidth(persian);
}
System.out.println("Calculation time for persian: "+(System.currentTimeMillis()-start)+" ms");
start=System.currentTimeMillis();
for(int i=0;i<size;i++)
{
fm.stringWidth(english);
}
System.out.println("Calculation time for english: "+(System.currentTimeMillis()-start)+" ms");
}
private static FontMetrics createFontMetrics(Font font)
{
BufferedImage bi = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB_PRE);
Graphics g = bi.getGraphics();
FontMetrics fm = g.getFontMetrics(font);
g.dispose();
bi = null;
return fm;
}
}
For me it gives next output:
Calculation time for persian: 5482 ms
Calculation time for english: 11 ms