Eclipse 调试问题
我有一个使用 Java 制作的 LWJGL 和 Slick2D 的游戏。我最近添加了以下代码来获取像素是否为空:
for (Entity e : onscreenents)
if (e.getBlockID() == 1 || e.getBlockID() == -2)
for (int x = e.getX(); x < e.getX() + 18; x++)
for (int y = e.getY(); y < e.getY() + 18; y++)
if (x > 0 && y > 0 && x < empty.length && y < empty[x].length)
empty[x][y] = false;
该代码似乎在 Eclipse 的运行模式下运行良好,但是当我在 Eclipse 的调试模式下启动程序时,游戏运行速度非常慢并且非常不稳定。删除这块代码可以使调试模式运行得像运行模式一样流畅。
有谁知道为什么会发生这种情况以及这是否是我的错?这真的很有帮助:)
I have a game using the LWJGL and Slick2D made in Java. I recently added this code to get if a pixel is empty:
for (Entity e : onscreenents)
if (e.getBlockID() == 1 || e.getBlockID() == -2)
for (int x = e.getX(); x < e.getX() + 18; x++)
for (int y = e.getY(); y < e.getY() + 18; y++)
if (x > 0 && y > 0 && x < empty.length && y < empty[x].length)
empty[x][y] = false;
This code seems to run fine in the Run mode of Eclipse but when I start the program in the Debug mode of Eclipse, the game runs really slowly and is very glitchy. Removing this block of code makes the debug mode run as smooth as the Run mode.
Does anyone know why this is happening and if it is my fault or not? It would really help :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这些可能会有所帮助:
使用 Eclipse IDE 在调试模式下加速 Tomcat
< a href="https://stackoverflow.com/questions/2195720/why-does-java-code-slow-down-in-debugger">为什么 Java 代码在调试器?
为什么使用方法入口调试时,被调试的程序会变慢这么多?
如果你确实需要性能改进,那么 bmargulies 注释是有效的。尝试将不变量移出循环,这样它们只计算一次。
因此,将
e.getX()
和e.getY()
调用移至 for 循环上方,以便它们仅被调用一次并将它们保存在本地变量中。与empty.length
相同(如果这些不随每次循环迭代而改变)。甚至可能将
empty[x]
保存到局部变量中,因为数组项取消引用比局部变量访问慢。您还可以对
x>0
和y>0
进行测试。如果您只对 +ve 值感兴趣,那么您可以测试e.getX()+18
和e.getY()+18
看看它们是否是 + ve 在循环之前,因为您甚至可能不需要执行它们。 (例如,如果e.getX()+18<=0
,则不必执行x
循环。对于y
也是如此)请注意但过早的优化。
These may help:
Speeding up Tomcat in debug mode with Eclipse IDE
Why does Java code slow down in debugger?
Why does the debugged program slow down so much when using method entry debugging?
If you really do need performance improvements, then bmargulies comment is valid. Try moving the invariants out of the loop(s) so they are only calculated once.
So move
e.getX()
ande.getY()
calls above the for loops so they are called only once and save them in local vars. Same withempty.length
(if these doesn't change with each loop iteration that is).And possibly even save
empty[x]
into a local variable as array item dereferencing is slower than local variable access.You also do a test for
x>0
andy>0
. If you're only interested in +ve values, then you could teste.getX()+18
ande.getY()+18
to see if they are +ve before the loops, as you may not even have to perform them. (e.g. ife.getX()+18<=0
you don't have to do thex
loop. Same fory
)Beware of premature optimisation though.