Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java SWING有时比openGL更快_Java_Swing_Opengl_Lwjgl - Fatal编程技术网

Java SWING有时比openGL更快

Java SWING有时比openGL更快,java,swing,opengl,lwjgl,Java,Swing,Opengl,Lwjgl,我想找出openGL(LWJGL)和swing在渲染简单正方形时的区别。不出所料,openGL赢了,但在一些测试中,结果很接近,甚至支持swing。为什么会这样 我的测试结果: Testing for a total of 500000 rectangles Swing time: 0.16367821 OpenGL time: 0.06317429 OpenGL relative to SWING is faster by 159.0899% Testing for a total of

我想找出openGL(LWJGL)和swing在渲染简单正方形时的区别。不出所料,openGL赢了,但在一些测试中,结果很接近,甚至支持swing。为什么会这样

我的测试结果:

Testing for a total of 500000 rectangles
Swing time: 0.16367821
OpenGL time: 0.06317429
OpenGL relative to SWING is faster by  159.0899%

Testing for a total of 750000 rectangles
OpenGL time: 0.066062525
Swing time: 0.16988374
OpenGL relative to SWING is faster by  157.156%

Testing for a total of 1000000 rectangles
OpenGL time: 0.0907693
Swing time: 0.15694521
OpenGL relative to SWING is faster by  72.90561%

Testing for a total of 1250000 rectangles
OpenGL time: 0.09591239
Swing time: 0.17011923
OpenGL relative to SWING is faster by  77.369385%

Testing for a total of 1500000 rectangles
OpenGL time: 0.11926948
Swing time: 0.21623997
OpenGL relative to SWING is faster by  81.303696%

Testing for a total of 1750000 rectangles
OpenGL time: 0.16327758
Swing time: 0.25741237
OpenGL relative to SWING is faster by  57.65323%

Testing for a total of 2000000 rectangles
OpenGL time: 0.17265266
Swing time: 0.2788536
OpenGL relative to SWING is faster by  61.511322%

Testing for a total of 2250000 rectangles
OpenGL time: 0.2492242
Swing time: 0.3022127
OpenGL relative to SWING is faster by  21.261368%

Testing for a total of 2500000 rectangles
Swing time: 0.3231118
OpenGL time: 0.29499054
OpenGL relative to SWING is faster by  9.532944%

Testing for a total of 2750000 rectangles
OpenGL time: 0.34329778
Swing time: 0.38381234
OpenGL relative to SWING is faster by  11.801575%

Testing for a total of 3000000 rectangles
OpenGL time: 0.34859535
Swing time: 0.39274055
OpenGL relative to SWING is faster by  12.663734%

Testing for a total of 3250000 rectangles
Swing time: 0.4241282
OpenGL time: 0.44056854
OpenGL relative to SWING is faster by  -3.7316208%

Testing for a total of 3500000 rectangles
Swing time: 0.4600469
OpenGL time: 0.4737318
OpenGL relative to SWING is faster by  -2.8887482%

Testing for a total of 3750000 rectangles
Swing time: 0.40855232
OpenGL time: 0.25052726
OpenGL relative to SWING is faster by  63.07698%

Testing for a total of 4000000 rectangles
Swing time: 0.5119725
OpenGL time: 0.55266017
OpenGL relative to SWING is faster by  -7.362152%

Testing for a total of 4250000 rectangles
OpenGL time: 0.5010328
Swing time: 0.57198834
OpenGL relative to SWING is faster by  14.16185%

Testing for a total of 4500000 rectangles
OpenGL time: 0.53123826
Swing time: 0.5992712
OpenGL relative to SWING is faster by  12.806473%

Testing for a total of 4750000 rectangles
OpenGL time: 0.5412617
Swing time: 0.6458795
OpenGL relative to SWING is faster by  19.328514%

Testing for a total of 5000000 rectangles
OpenGL time: 0.58324844
Swing time: 0.69343716
OpenGL relative to SWING is faster by  18.892242%
我使用的代码:

package swingVSopengl;

import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import static org.lwjgl.opengl.GL11.*;

public class SwingVsOpenGL {

    static int amount = 0;
    static float swingtime, opengltime;

    public static void main(String[] args) {

        for (amount = 500000; amount <= 5000000; amount += 250000) {

            System.out.println("Testing for a total of "
                    + String.valueOf(amount) + " rectangles");
            new SwingWay();
            new OpenGLWay();
            System.out.println("OpenGL relative to SWING is faster by  "
                    + String.valueOf(swingtime / opengltime * 100 - 100) + "%");
            System.out.println();
        }

        System.exit(0);
    }

    private static class SwingWay extends JFrame {

        public SwingWay() {
            setSize(64, 64);
            add(new JPanel() {
                @Override
                public void paint(Graphics g) {
                    super.paint(g);
                    long time = System.nanoTime();
                    for (int i = 0; i < amount; i++) {
                        g.fillRect(0, 0, 16, 16);
                    }
                    swingtime = (float) (System.nanoTime() - time) / 1000000000;
                    System.out.print("Swing time: ");
                    System.out.println(swingtime);
                    dispose();
                }
            });
            setVisible(true);
        }

    }

    private static class OpenGLWay {
        OpenGLWay() {
            try {
                Display.setDisplayMode(new DisplayMode(64, 64));
                Display.setTitle("A fresh display!");
                Display.create();
            } catch (LWJGLException e) {
                e.printStackTrace();
                Display.destroy();
                System.exit(1);
            }

            glMatrixMode(GL_PROJECTION);
            glOrtho(0, 640, 480, 0, 1, -1);
            glMatrixMode(GL_MODELVIEW);

            long time = System.nanoTime();
            for (int i = 0; i < amount; i++) {
                glBegin(GL_QUADS);
                glVertex2i(0, 0);
                glVertex2i(16, 0);
                glVertex2i(16, 16);
                glVertex2i(0, 16);
                glEnd();
            }
            opengltime = (float) (System.nanoTime() - time) / 1000000000;
            System.out.print("OpenGL time: ");
            System.out.println(opengltime);

            Display.destroy();
        }
    }

}
package swingVSopengl;
导入java.awt.Graphics;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入org.lwjgl.LWJGLException;
导入org.lwjgl.opengl.Display;
导入org.lwjgl.opengl.DisplayMode;
导入静态org.lwjgl.opengl.GL11.*;
公共级SwingVsOpenGL{
静态整数金额=0;
静态浮动swingtime、opengltime;
公共静态void main(字符串[]args){

对于(amount=500000;amount来说,渲染一个正方形不是很困难。本质上,它归结为打开一块内存的窗口并用一个特定的值填充它


此外,您使用OpenGL的方式也面临着巨大的开销。您衡量的不是SWING与OpenGL的效率,而是进行函数调用的效率。Ditch
glBegin
glEnd
(它们已经过时超过15年了)使用顶点数组。也不要用绘图命令填鸭式地给GPU喂食,而是通过每个绘图批至少有100个基本体的顶点数组来提供一个你能吃的东西。只有这样,你才能摆脱头顶的束缚。

绘制一个正方形不是很困难。本质上,它可以归结为打开一个窗口并用特定的值填充它


此外,您使用OpenGL的方式也面临着巨大的开销。您衡量的不是SWING与OpenGL的效率,而是进行函数调用的效率。Ditch
glBegin
glEnd
(它们已经过时超过15年了)并使用顶点数组。也不要用绘图命令填鸭式地喂养GPU,让GPU挨饿。相反,通过每个绘图批至少包含100个基本体的顶点数组,为GPU提供一个你能吃的一切。只有这样,你才能摆脱头顶的束缚。

除此之外,代码也没有测量渲染时间。至少需要一个
glFinish()
最后,否则它只是测量命令排队的时间,而不是进行任何渲染。除此之外,代码也没有测量渲染时间。至少需要一个
glFinish()
最后,否则它只是测量命令排队的时间,而不是进行任何渲染。