gpt4 book ai didi

java - SWING 有时比 openGL 更快

转载 作者:行者123 更新时间:2023-12-01 08:00:51 29 4
gpt4 key购买 nike

我想弄清楚 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();
}
}

}

最佳答案

渲染单个正方形并不是很困难。本质上,它可以归结为对一段内存进行窗口化并用特定值填充它。

此外,您使用 OpenGL 的方式也会遭受大量开销。您测量的不是 SWING 与 OpenGL 的效率,而是函数调用的效率。抛弃 glBegin ... glEnd (它们已经过时了 15 年多了)并使用顶点数组。也不要通过用勺子喂绘图命令来让 GPU 挨饿。相反,通过每个绘图批处理至少包含 100 个图元的顶点数组为其提供无限量的食物。只有这样你才能离开高空土地。

关于java - SWING 有时比 openGL 更快,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25335341/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com