Kilon
January 6th, 2009, 07:06 PM
I have created a small benchmark , to test the speed of Jython when calling swing components and JAVA2D methods. I was aiming to 5x times slower speed in JYTHON but JYTHON proved me very wrong. IT was only 3 times slower than JAVA.
The benchmark was not only for speed reasons but also a way to learn Jython .
I share with you the code of both JAVA and Jython. You may run the test in your computer and share results.
The app is building a window and draws 1 point lines and then displays a very large button with the amount of nanoseconds it took to execute the program.
I run the test in a ACER ASPIRE ONE with 1.GHz Pentium Atom Processor, 500 MB RAM, 8 GB SDD HD and WINXP.
JAVA CODE
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.color.*;
import java.awt.geom.Line2D;
import java.awt.*;
import java.util.Observable;
import javax.swing.JComponent;
import java.util.Observer;
public class Central {
static JFrame aWindow = new JFrame("This is my window");
public static void main(String[] args)
{
//Toolkit theKit = aWindow.getToolkit();
//Dimension wndSize = theKit.getScreenSize();
long StartTime = System.nanoTime();
CentralView view;
view = new CentralView() ;
aWindow.getContentPane().add(view, BorderLayout.CENTER);
aWindow.setBounds(0,0,800,500);
aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CL OSE);
aWindow.setCursor(Cursor.getPredefinedCursor(Curso r.CROSSHAIR_CURSOR));
aWindow.getContentPane().setBackground(Color.PINK) ;
aWindow.setVisible(true);
long EndTime = System.nanoTime() ;
long TimeExec= EndTime - StartTime;
aWindow.getContentPane().add(new JButton("Time of Excution in nano seconds:"+TimeExec));
}
}
class CentralView extends JComponent implements Observer {
/**
*
*/
private static final long serialVersionUID = 1L;
public void paint(Graphics g){
Graphics2D g2D = (Graphics2D)g;
for (double x=0 ; x<801; x++){
for(double y=0; y<501; y++){
Line2D.Double l1 = new Line2D.Double(x,y,x,y);
g2D.setPaint(Color.BLUE);
g2D.draw(l1);
}
}
g2D.setPaint(Color.RED);
g2D.draw3DRect(50, 50, 100, 100, true);
g2D.drawString("A Speed Test ", 60, 100);
}
@Override
public void update(Observable arg0, Object arg1) {
// TODO Auto-generated method stub
}
}
Jython Code
"""\
Equivalent to the ../awt/simple.py example but using swing components
instead of AWT ones.
"""
# This line will import the appropriate swing library for your system (jdk 1.1 or 1.2)
from javax import swing
from java import awt
from java import util
from java import lang
class CentralView(swing.JComponent, util.Observer):
def paint(self,g):
for x in range(0,800):
for y in range(0,500):
l1 = awt.geom.Line2D.Double(x,y,x,y)
g.draw(l1)
g.setPaint(awt.Color.RED)
g.draw3DRect(50,50,100,100,1)
g.drawString("a nice square", 60, 100)
StartTime = lang.System.nanoTime()
view = CentralView()
aWindow = swing.JFrame('This is my window ')
aWindow.getContentPane().add(view, awt.BorderLayout.CENTER)
aWindow.setBounds(0,0,800,500)
aWindow.setDefaultCloseOperation(swing.JFrame.EXIT _ON_CLOSE)
aWindow.setCursor(awt.Cursor.getPredefinedCursor(a wt.Cursor.CROSSHAIR_CURSOR))
aWindow.getContentPane().add(view,)
aWindow.setVisible(1)
EndTime = lang.System.nanoTime()
TimeExec = EndTime - StartTime
aWindow.getContentPane().add(swing.JButton("Time of Execution in nano seconds:"+str(TimeExec)))
The benchmark was not only for speed reasons but also a way to learn Jython .
I share with you the code of both JAVA and Jython. You may run the test in your computer and share results.
The app is building a window and draws 1 point lines and then displays a very large button with the amount of nanoseconds it took to execute the program.
I run the test in a ACER ASPIRE ONE with 1.GHz Pentium Atom Processor, 500 MB RAM, 8 GB SDD HD and WINXP.
JAVA CODE
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.color.*;
import java.awt.geom.Line2D;
import java.awt.*;
import java.util.Observable;
import javax.swing.JComponent;
import java.util.Observer;
public class Central {
static JFrame aWindow = new JFrame("This is my window");
public static void main(String[] args)
{
//Toolkit theKit = aWindow.getToolkit();
//Dimension wndSize = theKit.getScreenSize();
long StartTime = System.nanoTime();
CentralView view;
view = new CentralView() ;
aWindow.getContentPane().add(view, BorderLayout.CENTER);
aWindow.setBounds(0,0,800,500);
aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CL OSE);
aWindow.setCursor(Cursor.getPredefinedCursor(Curso r.CROSSHAIR_CURSOR));
aWindow.getContentPane().setBackground(Color.PINK) ;
aWindow.setVisible(true);
long EndTime = System.nanoTime() ;
long TimeExec= EndTime - StartTime;
aWindow.getContentPane().add(new JButton("Time of Excution in nano seconds:"+TimeExec));
}
}
class CentralView extends JComponent implements Observer {
/**
*
*/
private static final long serialVersionUID = 1L;
public void paint(Graphics g){
Graphics2D g2D = (Graphics2D)g;
for (double x=0 ; x<801; x++){
for(double y=0; y<501; y++){
Line2D.Double l1 = new Line2D.Double(x,y,x,y);
g2D.setPaint(Color.BLUE);
g2D.draw(l1);
}
}
g2D.setPaint(Color.RED);
g2D.draw3DRect(50, 50, 100, 100, true);
g2D.drawString("A Speed Test ", 60, 100);
}
@Override
public void update(Observable arg0, Object arg1) {
// TODO Auto-generated method stub
}
}
Jython Code
"""\
Equivalent to the ../awt/simple.py example but using swing components
instead of AWT ones.
"""
# This line will import the appropriate swing library for your system (jdk 1.1 or 1.2)
from javax import swing
from java import awt
from java import util
from java import lang
class CentralView(swing.JComponent, util.Observer):
def paint(self,g):
for x in range(0,800):
for y in range(0,500):
l1 = awt.geom.Line2D.Double(x,y,x,y)
g.draw(l1)
g.setPaint(awt.Color.RED)
g.draw3DRect(50,50,100,100,1)
g.drawString("a nice square", 60, 100)
StartTime = lang.System.nanoTime()
view = CentralView()
aWindow = swing.JFrame('This is my window ')
aWindow.getContentPane().add(view, awt.BorderLayout.CENTER)
aWindow.setBounds(0,0,800,500)
aWindow.setDefaultCloseOperation(swing.JFrame.EXIT _ON_CLOSE)
aWindow.setCursor(awt.Cursor.getPredefinedCursor(a wt.Cursor.CROSSHAIR_CURSOR))
aWindow.getContentPane().add(view,)
aWindow.setVisible(1)
EndTime = lang.System.nanoTime()
TimeExec = EndTime - StartTime
aWindow.getContentPane().add(swing.JButton("Time of Execution in nano seconds:"+str(TimeExec)))