Java/Chart/JFreeChart Performance

Материал из Java эксперт
Версия от 06:52, 1 июня 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

A basic performance test for a couple of common operations

/* ===========================================================
 * JFreeChart : a free chart library for the Java(tm) platform
 * ===========================================================
 *
 * (C) Copyright 2000-2004, by Object Refinery Limited and Contributors.
 *
 * Project Info:  http://www.jfree.org/jfreechart/index.html
 *
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GNU Lesser General Public License as published by the Free Software Foundation;
 * either version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this
 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
 * in the United States and other countries.]
 *
 * ----------------
 * Performance.java
 * ----------------
 * (C) Copyright 2002-2004, by Object Refinery Limited.
 *
 * Original Author:  David Gilbert (for Object Refinery Limited).
 * Contributor(s):   -;
 *
 * $Id: Performance.java,v 1.10 2004/04/26 19:12:00 taqua Exp $
 *
 * Changes (since 11-Oct-2002)
 * ---------------------------
 * 11-Oct-2002 : Fixed errors reported by Checkstyle (DG);
 *
 */
package org.jfree.chart.demo;
import java.awt.geom.Line2D;
import java.util.Date;
/**
 * A basic performance test for a couple of common operations.
 *
 */
public class Performance {
    /** The value. */
    private double value = 2.0;
    /** The number. */
    private Double number = new Double(this.value);
    /**
     * Default constructor.
     */
    public Performance() {
        super();
    }
    /**
     * Creates lines in a loop.
     *
     * @param count  the number of lines to create.
     */
    public void createLines(final int count) {
        Line2D line = new Line2D.Double();
        for (int i = 0; i < count; i++) {
            line = new Line2D.Double(1.0, 1.0, 1.0, 1.0);
        }
        System.out.println(line.toString());
    }
    /**
     * Creates one line, then repeatedly calls the setLine method.
     *
     * @param count  the number of times to call the setLine method.
     */
    public void setLines(final int count) {
        final Line2D line = new Line2D.Double(0.0, 0.0, 0.0, 0.0);
        for (int i = 0; i < count; i++) {
            line.setLine(1.0, 1.0, 1.0, 1.0);
        }
    }
    /**
     * Repeatedly grabs a value from a Number instance.
     *
     * @param count  the number of times to call doubleValue().
     */
    public void getNumber(final int count) {
        double d = 0.0;
        for (int i = 0; i < count; i++) {
            d = this.number.doubleValue();
        }
        System.out.println(d);
    }
    /**
     * Repeatedly grabs a value from a double.
     *
     * @param count  the number of times to fetch the value.
     */
    public void getValue(final int count) {
        double d = 0.0;
        for (int i = 0; i < count; i++) {
            d = this.value;
        }
        System.out.println(d);
    }
    /**
     * Writes the current time to the console.
     *
     * @param text  the prefix.
     * @param time  the time.
     */
    public void writeTime(final String text, final Date time) {
        System.out.println(text + " : " + time.getTime());
    }
    /**
     * Starting point for the application.
     *
     * @param args  ignored.
     */
    public static void main(final String[] args) {
        final Performance p = new Performance();
        System.out.println("Simple performance tests.");
        final Date start1 = new Date();
        p.createLines(100000);
        final Date end1 = new Date();
        final Date start2 = new Date();
        p.setLines(100000);
        final Date end2 = new Date();
        p.writeTime("Start create lines", start1);
        p.writeTime("Finish create lines", end1);
        p.writeTime("Start set lines", start2);
        p.writeTime("Finish set lines", end2);
        final Date start3 = new Date();
        p.getNumber(1000000);
        final Date end3 = new Date();
        final Date start4 = new Date();
        p.getValue(1000000);
        final Date end4 = new Date();
        p.writeTime("Start get number", start3);
        p.writeTime("Finish get number", end3);
        p.writeTime("Start get value", start4);
        p.writeTime("Finish get value", end4);

    }
}





A basic performance test for a couple of common operations 2

/* ===========================================================
 * JFreeChart : a free chart library for the Java(tm) platform
 * ===========================================================
 *
 * (C) Copyright 2000-2004, by Object Refinery Limited and Contributors.
 *
 * Project Info:  http://www.jfree.org/jfreechart/index.html
 *
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GNU Lesser General Public License as published by the Free Software Foundation;
 * either version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this
 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
 * in the United States and other countries.]
 *
 * -----------------
 * Performance2.java
 * -----------------
 * (C) Copyright 2002-2004, by Object Refinery Limited.
 *
 * Original Author:  David Gilbert (for Object Refinery Limited).
 * Contributor(s):   -;
 *
 * $Id: Performance2.java,v 1.10 2004/04/26 19:12:00 taqua Exp $
 *
 * Changes (since 11-Oct-2002)
 * ---------------------------
 * 11-Oct-2002 : Fixed errors reported by Checkstyle (DG);
 *
 */
package org.jfree.chart.demo;
import java.util.Date;
/**
 * A basic performance test for a couple of common operations.
 *
 */
public class Performance2 {
    /** A double primitive. */
    private double primitive = 42.0;
    /** A number object. */
    private Number object = new Double(42.0);
    /**
     * Default constructor.
     */
    public Performance2() {
        super();
    }
    /**
     * Just use double value - should be fast.
     *
     * @return the double value.
     */
    public double getPrimitive() {
        return this.primitive;
    }
    /**
     * Creates a Number object every time the primitive is accessed - should be really slow.
     *
     * @return creates and returns a Number object.
     */
    public Number getPrimitiveAsObject() {
        return new Double(this.primitive);
    }
    /**
     * Returns the object - caller has to use doubleValue() method.
     *
     * @return an existing Number object.
     */
    public Number getObject() {
        return this.object;
    }
    /**
     * Returns a double value generated from the Object - should be similar to previous method,
     * but is not!
     *
     * @return the doubleValue() for the Number.
     */
    public double getObjectAsPrimitive() {
        return this.object.doubleValue();
    }
    /**
     * Cycles through accessing the primitive.
     *
     * @param count  the number of times to access.
     */
    public void getPrimitiveLoop(final int count) {
        double d = 0.0;
        for (int i = 0; i < count; i++) {
            d = getPrimitive();
        }
        System.out.println(d);
    }
    /**
     * Cycles through accessing the primitive as an object.
     *
     * @param count  the number of times to access.
     */
    public void getPrimitiveAsObjectLoop(final int count) {
        double d = 0.0;
        for (int i = 0; i < count; i++) {
            d = getPrimitiveAsObject().doubleValue();
        }
        System.out.println(d);
    }
    /**
     * Cycles through accessing the object as a primitive.
     *
     * @param count  the number of times to access.
     */
    public void getObjectAsPrimitiveLoop(final int count) {
        double d = 0.0;
        for (int i = 0; i < count; i++) {
            d = getObjectAsPrimitive();
        }
        System.out.println(d);
    }
    /**
     * Cycles through accessing the object.
     *
     * @param count  the number of times to access.
     */
    public void getObjectLoop(final int count) {
        double d = 0.0;
        for (int i = 0; i < count; i++) {
            d = getObject().doubleValue();
        }
        System.out.println(d);
    }
    /**
     * Outputs the current status to the console.
     *
     * @param label  the label.
     * @param start  the start time.
     * @param end  the end time.
     */
    public void status(final String label, final Date start, final Date end) {
        final long elapsed = end.getTime() - start.getTime();
        System.out.println(label + start.getTime() + "-->" + end.getTime() + " = " + elapsed);
    }
    /**
     * The starting point for the performance test.
     *
     * @param args  ignored.
     */
    public static void main(final String[] args) {
        final Performance2 performance = new Performance2();
        final int count = 10000000;
        for (int repeat = 0; repeat < 3; repeat++) {  // repeat a few times just to make
                                                      // sure times are consistent
            final Date s1 = new Date();
            performance.getPrimitiveLoop(count);
            final Date e1 = new Date();
            performance.status("getPrimitive() : ", s1, e1);
            final Date s2 = new Date();
            performance.getPrimitiveAsObjectLoop(count);
            final Date e2 = new Date();
            performance.status("getPrimitiveAsObject() : ", s2, e2);
            final Date s3 = new Date();
            performance.getObjectLoop(count);
            final Date e3 = new Date();
            performance.status("getObject() : ", s3, e3);
            final Date s4 = new Date();
            performance.getObjectAsPrimitiveLoop(count);
            final Date e4 = new Date();
            performance.status("getObjectAsPrimitive() : ", s4, e4);
            System.out.println("-------------------");
        }
    }
}





Chart Timing 1

/* ===========================================================
 * JFreeChart : a free chart library for the Java(tm) platform
 * ===========================================================
 *
 * (C) Copyright 2000-2004, by Object Refinery Limited and Contributors.
 *
 * Project Info:  http://www.jfree.org/jfreechart/index.html
 *
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GNU Lesser General Public License as published by the Free Software Foundation;
 * either version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this
 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
 * in the United States and other countries.]
 *
 * -----------------
 * ChartTiming1.java
 * -----------------
 * (C) Copyright 2001-2004, by Object Refinery Limited and Contributors.
 *
 * Original Author:  David Gilbert (for Object Refinery Limited);
 * Contributor(s):   -;
 *
 * $Id: ChartTiming1.java,v 1.12 2004/04/26 19:11:53 taqua Exp $
 *
 * Changes (from 24-Apr-2002)
 * --------------------------
 * 24-Apr-2002 : Added standard header (DG);
 * 29-Oct-2002 : Modified to use javax.swing.Timer (DG);
 *
 */
package org.jfree.chart.demo;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import javax.swing.Timer;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.data.general.DefaultPieDataset;
/**
 * Draws a pie chart over and over for 10 seconds.  Reports on how many redraws were achieved.
 * <p>
 * On my PC (SuSE Linux 8.2, JDK 1.4, 256mb RAM, 2.66ghz Pentium) I get 90-95 charts per second.
 *
 */
public class ChartTiming1 implements ActionListener {
    /** A flag that indicates when time is up. */
    private boolean finished;
    /**
     * Creates a new application.
     */
    public ChartTiming1() {
        // nothing to do
    }
    /**
     * Runs the timing.
     */
    public void run() {
        this.finished = false;
        // create a dataset...
        final DefaultPieDataset data = new DefaultPieDataset();
        data.setValue("One", new Double(10.3));
        data.setValue("Two", new Double(8.5));
        data.setValue("Three", new Double(3.9));
        data.setValue("Four", new Double(3.9));
        data.setValue("Five", new Double(3.9));
        data.setValue("Six", new Double(3.9));
        // create a pie chart...
        final boolean withLegend = true;
        final JFreeChart chart = ChartFactory.createPieChart(
            "Testing", 
            data, 
            withLegend,
            true,
            false
        );
        final BufferedImage image = new BufferedImage(400, 300, BufferedImage.TYPE_INT_RGB);
        final Graphics2D g2 = image.createGraphics();
        final Rectangle2D chartArea = new Rectangle2D.Double(0, 0, 400, 300);
        // set up the timer...
        final Timer timer = new Timer(10000, this);
        timer.setRepeats(false);
        int count = 0;
        timer.start();
        while (!this.finished) {
            chart.draw(g2, chartArea, null, null);
            System.out.println("Charts drawn..." + count);
            if (!this.finished) {
                count++;
            }
        }
        System.out.println("DONE");
    }
    /**
     * Receives notification of action events (in this case, from the Timer).
     *
     * @param event  the event.
     */
    public void actionPerformed(final ActionEvent event) {
        this.finished = true;
    }
    /**
     * Starting point for the application.
     *
     * @param args  ignored.
     */
    public static void main(final String[] args) {
        final ChartTiming1 app = new ChartTiming1();
        app.run();
    }
}





Chart Timing 3

/* ===========================================================
 * JFreeChart : a free chart library for the Java(tm) platform
 * ===========================================================
 *
 * (C) Copyright 2000-2004, by Object Refinery Limited and Contributors.
 *
 * Project Info:  http://www.jfree.org/jfreechart/index.html
 *
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GNU Lesser General Public License as published by the Free Software Foundation;
 * either version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this
 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
 * in the United States and other countries.]
 *
 * -----------------
 * ChartTiming3.java
 * -----------------
 * (C) Copyright 2002-2004, by Object Refinery Limited and Contributors.
 *
 * Original Author:  David Gilbert (for Object Refinery Limited);
 * Contributor(s):   -;
 *
 * $Id: ChartTiming3.java,v 1.15 2004/04/26 19:11:53 taqua Exp $
 *
 * Changes
 * -------
 * 29-Oct-2002 : Version 1 (DG);
 *
 */
package org.jfree.chart.demo;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import javax.swing.Timer;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYDotRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
/**
 * Draws a scatter plot over and over for 10 seconds.  Reports on how many redraws were achieved.
 * <p>
 * On my PC (SuSE Linux 8.2, JDK 1.4, 256mb RAM, 2.66ghz Pentium) I get 13-14 charts per second.
 *
 */
public class ChartTiming3 implements ActionListener {
    /** A flag that indicates when time is up. */
    private boolean finished;
    /**
     * Creates a new application.
     */
    public ChartTiming3() {
        // nothing to do
    }
    /**
     * Runs the test.
     */
    public void run() {
        this.finished = false;
        // create a dataset...
        final XYSeries series = new XYSeries("Random Data");
        for (int i = 0; i < 1440; i++) {
            final double x = Math.random();
            final double y = Math.random();
            series.add(x, y);
        }
        final XYDataset data = new XYSeriesCollection(series);
        // create a scatter chart...
        final boolean withLegend = true;
        final JFreeChart chart = ChartFactory.createScatterPlot(
            "Scatter plot timing", "X", "Y",
            data, 
            PlotOrientation.VERTICAL,
            withLegend, 
            false, 
            false
        );
        final XYPlot plot = chart.getXYPlot();
        plot.setRenderer(new XYDotRenderer());
        final BufferedImage image = new BufferedImage(400, 300, BufferedImage.TYPE_INT_RGB);
        final Graphics2D g2 = image.createGraphics();
        final Rectangle2D chartArea = new Rectangle2D.Double(0, 0, 400, 300);
        // set up the timer...
        final Timer timer = new Timer(10000, this);
        timer.setRepeats(false);
        int count = 0;
        timer.start();
        while (!this.finished) {
            chart.draw(g2, chartArea, null, null);
            System.out.println("Charts drawn..." + count);
            if (!this.finished) {
                count++;
            }
        }
        System.out.println("DONE");
    }
    /**
     * Receives notification of action events (in this case, from the Timer).
     *
     * @param event  the event.
     */
    public void actionPerformed(final ActionEvent event) {
        this.finished = true;
    }
    /**
     * Starting point for the application.
     *
     * @param args  ignored.
     */
    public static void main(final String[] args) {
        final ChartTiming3 app = new ChartTiming3();
        app.run();
    }
}





Chart Timing 4

/* ===========================================================
 * JFreeChart : a free chart library for the Java(tm) platform
 * ===========================================================
 *
 * (C) Copyright 2000-2004, by Object Refinery Limited and Contributors.
 *
 * Project Info:  http://www.jfree.org/jfreechart/index.html
 *
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GNU Lesser General Public License as published by the Free Software Foundation;
 * either version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this
 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
 * in the United States and other countries.]
 *
 * -----------------
 * ChartTiming4.java
 * -----------------
 * (C) Copyright 2002-2004, by Object Refinery Limited and Contributors.
 *
 * Original Author:  David Gilbert (for Object Refinery Limited);
 * Contributor(s):   -;
 *
 * $Id: ChartTiming4.java,v 1.16 2004/04/26 19:11:53 taqua Exp $
 *
 * Changes
 * -------
 * 29-Oct-2002 : Version 1 (DG);
 *
 */
package org.jfree.chart.demo;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import javax.swing.Timer;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.FastScatterPlot;
import org.jfree.chart.plot.Plot;
/**
 * Draws a scatter plot over and over for 10 seconds.  Reports on how many redraws were achieved.
 * <p>
 * On my PC (SuSE Linux 8.2, JDK 1.4, 256mb RAM, 2.66ghz Pentium) I get 31 charts per second.
 *
 */
public class ChartTiming4 implements ActionListener {
    /** A flag that indicates when time is up. */
    private boolean finished;
    /** Storage for the data. */
    private float[][] data = new float[2][1440];
    /**
     * Creates a new application.
     */
    public ChartTiming4() {
        // nothing to do
    }
    /**
     * Runs the test.
     */
    public void run() {
        this.finished = false;
        // create a dataset...
        populateData();
        // create a fast scatter chart...
        final Plot plot = new FastScatterPlot(this.data, new NumberAxis("X"), new NumberAxis("Y"));
        final JFreeChart chart = new JFreeChart(
            "Fast Scatter Plot Timing",
            JFreeChart.DEFAULT_TITLE_FONT,
            plot, 
            true
        );
        final BufferedImage image = new BufferedImage(400, 300, BufferedImage.TYPE_INT_RGB);
        final Graphics2D g2 = image.createGraphics();
        final Rectangle2D chartArea = new Rectangle2D.Double(0, 0, 400, 300);
        // set up the timer...
        final Timer timer = new Timer(10000, this);
        timer.setRepeats(false);
        int count = 0;
        timer.start();
        while (!this.finished) {
            chart.draw(g2, chartArea, null, null);
            System.out.println("Charts drawn..." + count);
            if (!this.finished) {
                count++;
            }
        }
        System.out.println("DONE");
    }
    /**
     * Receives notification of action events (in this case, from the Timer).
     *
     * @param event  the event.
     */
    public void actionPerformed(final ActionEvent event) {
        this.finished = true;
    }
    /**
     * Populates the data array with random values.
     */
    private void populateData() {
        for (int i = 0; i < this.data[0].length; i++) {
            final float x = i;
            this.data[0][i] = x;
            this.data[1][i] = 100 + (2 * x) + (float) Math.random() * 1440;
        }
    }
    /**
     * Starting point for the application.
     *
     * @param args  ignored.
     */
    public static void main(final String[] args) {
        final ChartTiming4 app = new ChartTiming4();
        app.run();
    }
}