简体   繁体   English

Java Swing性能OS X 10.6与Windows 7

[英]Java Swing Performance OS X 10.6 vs. Windows 7

This is my first post to this forum, I have about 6 months of experience of Java in general, and about 1 month of Java GUI programming (Swing), so consider me as a Java newbie. 这是我在这个论坛的第一篇文章,我有大约6个月的Java经验,以及大约1个月的Java GUI编程(Swing),所以把我当作Java新手。

I started to develop a simple 2D shooter game with Java swing as a hobby project. 我开始用Java swing开发一个简单的2D射击游戏作为业余爱好项目。 I am using Java 1.6.0_65 with Mac OS X 10.6.8, Macbook Pro 2.4 GHz Intel Core 2 Duo (Spring 2010 model), the display card is NVIDIA GeForce 320M. 我使用Java 1.6.0_65与Mac OS X 10.6.8,Macbook Pro 2.4 GHz Intel Core 2 Duo(2010年春季型号),显卡是NVIDIA GeForce 320M。

In my paint method, I draw about 30-50 rectangles with Swing drawRect() method, sizes of 32 * 32 pixels, few are of 7 * 7 pixels of size, the smaller ones are filled with fillRect . 在我的绘画方法中,我用Swing drawRect()方法绘制大约30-50个矩形,大小为32 * 32像素,少数是7 * 7像素大小,较小的矩形用fillRect填充。 I call the paintComponent() method 50 times/sec from a timer callback. 我从计时器回调中调用paintComponent()方法50次/秒。

To get some kind of idea of how fast/slow the painting is, I added some measurement code, to record starting and end times of paintComponent() method, and calculate the average duration, using System.nanoTime() method. 为了了解绘画的快/慢速度,我添加了一些测量代码,用于记录paintComponent()方法的开始和结束时间,并使用System.nanoTime()方法计算平均持续时间。

Average execution time per single paintComponent() seems to be about 3000 usec on OS X 10.6. 每个paintComponent()平均执行时间似乎在OS X 10.6上约为3000 usec。

I also run same code in couple for Windows 7 PCs (Sony Vaio laptop, AMD dual core 2.x GHz and HP laptop, Intel quad core, 2.x GHz, don't remember all details now). 我也为Windows 7 PC(Sony Vaio笔记本电脑,AMD双核2.x GHz和HP笔记本电脑,Intel四核,2.x GHz,现在不记得所有细节)运行相同的代码。 In Windows machines, average execution time was only about 100 us, that is about 30 x faster! 在Windows机器中,平均执行时间仅为约100 us,大约快30倍!

What can explain such a big difference? 有什么可以解释这么大的差异? Can System.nanoTime() work with different resolutions between Windows 7 & OS X, or is painting really that slow in OS X? System.nanoTime()可以在Windows 7和OS X之间使用不同的分辨率,还是在OS X中绘制的速度真的很慢?

On the other hand, I also measured average execution time of the non-GUI related part of my game engine (moving of objects, collision detections etc.), and it was between 50 - 100 us on both system, so maybe System.nanoTime() is reliable, and painting really is that slow in OS X? 另一方面,我还测量了我的游戏引擎的非GUI相关部分的平均执行时间(移动对象,碰撞检测等),并且在两个系统上它都在50到100之间,所以也许System.nanoTime()是可靠的,在OS X中绘画真的很慢吗?

Edit: I am unable to copy-paste any code here from xCode and Eclipse, I keep getting complains about formatting, but you can look a test app I wrote to illustrate this problem, at: http://www.java-gaming.org/topics/swing-performance-os-x-10-6-vs-windows-7/31654/view.html . 编辑:我无法从xCode和Eclipse复制粘贴任何代码,我不断抱怨格式化,但你可以看一下我写的测试应用来说明这个问题,请访问: http://www.java-gaming。 org / topics / swing-performance-os-x-10-6-vs-windows-7/31654 / view.html

I am getting quite slow performance on Mac also with this test app, about 1400 - 1800 usec / paint operation. 我使用这个测试应用程序在Mac上的性能相当慢,大约1400 - 1800次usec / paint操作。

I have the same issue with SWT on Mac. 我在Mac上遇到了与SWT相同的问题。 Another observation is that on Mac, as opposed to Windows, there is never any flicker, even without double buffering. 另一个观察结果是,在Mac上,与Windows相反,即使没有双缓冲也没有任何闪烁。 That may or may not indicate that the underlying reason for the weak performance is in added safety of operations (they may block until a vertical refresh, to avoid flicker). 这可能会或可能不会表明性能较弱的根本原因在于增加了操作的安全性(它们可能会阻塞直到垂直刷新,以避免闪烁)。

It appears the rendering on MacOS X for built in Intel graphics based Retina displays is slow. 看起来MacOS X上内置基于英特尔图形的Retina显示屏的渲染速度很慢。 This is highlighted in this JDK bug comment . 这在JDK错误评论中突出显示。 It is resolved in JDK 8 patch 40 (8u40). 它在JDK 8补丁40(8u40)中得到解决

I bumped into this as well whilst creating a 2D side/vertical scrolling game and am investigating the rendering on Mac: 我在创建2D侧/垂直滚动游戏时也遇到了这个问题,我正在调查Mac上的渲染:

  • Yosemite 10.10.3 优胜美地10.10.3
  • MacBook Pro (Retina Mid 2012) MacBook Pro(Retina Mid 2012)
  • Processor 2.7Ghz Intel Core i7 处理器2.7Ghz英特尔酷睿i7
  • Java(TM) SE Runtime Environment (build 1.6.0_65-b14-466.1-11M4716) Java(TM)SE运行时环境(版本1.6.0_65-b14-466.1-11M4716)

I am performing my rendering via the standard JComponent#paintComponent(Graphics) approach and using my own rolled double buffering approach so I can isolate the time spent preparing the image versus the time spent actually drawing the image with the single call to Graphics#drawImage() to the display device. 我正在通过标准的JComponent#paintComponent(Graphics)方法执行渲染并使用我自己的滚动双缓冲方法,因此我可以将准备图像所花费的时间与实际绘制图像所花费的时间隔离,只需调用Graphics#drawImage()到显示设备。

If I use the MBP's built in display ( Intel HD Graphics 4000 ) to draw an image 1680x1500, it takes 30ms to complete this. 如果我使用MBP的内置显示器( Intel HD Graphics 4000 )绘制图像1680x1500,则需要30ms才能完成此操作。

If I then switch to the external display ( NVIDIA GeForce GT 650M ), for the same code and same image size I get a time of 5ms . 如果我然后切换到外部显示器( NVIDIA GeForce GT 650M ),对于相同的代码和相同的图像尺寸,我得到5ms的时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM