Pygame 简单循环在 Mac 上运行非常慢
Pygame simple loop runs very slowly on Mac
E: 在 OS X 和 Linux 上测试相同后,我可以确认以下仅发生在 OS X 上. 在 Linux 上,它确实以每秒一千帧的速度运行,正如我碰巧想知道的那样。有什么解释吗?感谢 TextMate,我更喜欢在 Mac 上开发。
这是一个简单的循环,它几乎什么都不做,而且运行速度仍然很慢。谁能解释为什么? FPS 平均为 30 多一点,每次通过循环需要 30 多毫秒。 Window 大小似乎根本不会影响这一点,因为即使设置很小的 window 大小(如 (50,50))也具有相同的 fps。
我觉得这很奇怪,我希望任何现代硬件都可以为这样一个简单的循环做 1000 fps,即使我们每次都更新每个像素。从配置文件中我可以看到 {built-in method get}
和 {built-in method update}
加在一起似乎每次调用大约需要 30 毫秒的时间,这真的是我们在不使用脏 rects 的情况下能得到的最好结果吗?
pygame.init()
clock = pygame.time.Clock()
fps = 1000
#milliseconds from last frame
new_time, old_time = None, None
done = False
while not done:
clock.tick(fps)
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
# show fps and milliseconds
if new_time:
old_time = new_time
new_time = pygame.time.get_ticks()
if new_time and old_time:
pygame.display.set_caption("fps: " + str(int(clock.get_fps())) + " ms: " + str(new_time-old_time))
pygame.display.update()
这是 main 函数的 cProfile 的开头。
94503 function calls (92211 primitive calls) in 21.011 seconds
Ordered by: cumulative time
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.026 0.026 21.011 21.011 new_main.py:34(main)
652 14.048 0.022 14.048 0.022 {built-in method get}
652 5.864 0.009 5.864 0.009 {built-in method update}
1 0.444 0.444 0.634 0.634 {built-in method init}
651 0.278 0.000 0.278 0.000 {built-in method set_caption}
72/1 0.000 0.000 0.151 0.151 <frozen importlib._bootstrap>:2234(_find_and_load)
72/1 0.000 0.000 0.151 0.151 <frozen importlib._bootstrap>:2207(_find_and_load_unlocked)
71/1 0.000 0.000 0.151 0.151 <frozen importlib._bootstrap>:1186(_load_unlocked)
46/1 0.000 0.000 0.151 0.151 <frozen importlib._bootstrap>:1122(_exec)
46/1 0.000 0.000 0.151 0.151 <frozen importlib._bootstrap>:1465(exec_module)
74/1 0.000 0.000 0.151 0.151 <frozen importlib._bootstrap>:313(_call_with_frames_removed)
54/1 0.004 0.000 0.151 0.151 {built-in method exec}
1 0.000 0.000 0.151 0.151 macosx.py:1(<module>)
1 0.000 0.000 0.150 0.150 pkgdata.py:18(<module>)
25/3 0.000 0.000 0.122 0.041 <frozen importlib._bootstrap>:1156(_load_backward_compatible)
8/1 0.026 0.003 0.121 0.121 {method 'load_module' of 'zipimport.zipimporter' objects}
1 0.000 0.000 0.101 0.101 __init__.py:15(<module>)
1 0.000 0.000 0.079 0.079 config_reader.py:115(build_from_config)
2 0.000 0.000 0.056 0.028 common.py:43(reset_screen)
2 0.055 0.027 0.055 0.027 {built-in method set_mode}
72/71 0.001 0.000 0.045 0.001 <frozen importlib._bootstrap>:2147(_find_spec)
70/69 0.000 0.000 0.043 0.001 <frozen importlib._bootstrap>:1934(find_spec)
70/69 0.001 0.000 0.043 0.001 <frozen importlib._bootstrap>:1902(_get_spec)
92 0.041 0.000 0.041 0.000 {built-in method load_extended}
6 0.000 0.000 0.041 0.007 new_map.py:74(add_character)
6 0.000 0.000 0.041 0.007 new_character.py:32(added_to_map)
6 0.001 0.000 0.041 0.007 new_character.py:265(__init__)
1 0.000 0.000 0.038 0.038 macosx.py:14(Video_AutoInit)
1 0.038 0.038 0.038 0.038 {built-in method InstallNSApplication}
1 0.036 0.036 0.036 0.036 {built-in method quit}
65 0.001 0.000 0.036 0.001 re.py:277(_compile)
49 0.000 0.000 0.036 0.001 re.py:221(compile)
最终的答案是 OS X 下的视网膜显示是差异化因素。 运行 即使在同一个 Mac 的外部显示器上也能正常工作。但是将 window 移到视网膜显示器上会使它变慢。连接或不连接外接显示器。
另一方面,它在 Linux 下的同一视网膜显示器上运行得很好。目前尚不清楚显示管理器/渲染的差异是什么导致了这种情况,但我怀疑对此有什么办法。
将游戏分辨率更改为全屏对我很有帮助。
试试这个:
window = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
而不是:
window = pygame.display.set_mode((winx, winy))
E: 在 OS X 和 Linux 上测试相同后,我可以确认以下仅发生在 OS X 上. 在 Linux 上,它确实以每秒一千帧的速度运行,正如我碰巧想知道的那样。有什么解释吗?感谢 TextMate,我更喜欢在 Mac 上开发。
这是一个简单的循环,它几乎什么都不做,而且运行速度仍然很慢。谁能解释为什么? FPS 平均为 30 多一点,每次通过循环需要 30 多毫秒。 Window 大小似乎根本不会影响这一点,因为即使设置很小的 window 大小(如 (50,50))也具有相同的 fps。
我觉得这很奇怪,我希望任何现代硬件都可以为这样一个简单的循环做 1000 fps,即使我们每次都更新每个像素。从配置文件中我可以看到 {built-in method get}
和 {built-in method update}
加在一起似乎每次调用大约需要 30 毫秒的时间,这真的是我们在不使用脏 rects 的情况下能得到的最好结果吗?
pygame.init()
clock = pygame.time.Clock()
fps = 1000
#milliseconds from last frame
new_time, old_time = None, None
done = False
while not done:
clock.tick(fps)
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
# show fps and milliseconds
if new_time:
old_time = new_time
new_time = pygame.time.get_ticks()
if new_time and old_time:
pygame.display.set_caption("fps: " + str(int(clock.get_fps())) + " ms: " + str(new_time-old_time))
pygame.display.update()
这是 main 函数的 cProfile 的开头。
94503 function calls (92211 primitive calls) in 21.011 seconds
Ordered by: cumulative time
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.026 0.026 21.011 21.011 new_main.py:34(main)
652 14.048 0.022 14.048 0.022 {built-in method get}
652 5.864 0.009 5.864 0.009 {built-in method update}
1 0.444 0.444 0.634 0.634 {built-in method init}
651 0.278 0.000 0.278 0.000 {built-in method set_caption}
72/1 0.000 0.000 0.151 0.151 <frozen importlib._bootstrap>:2234(_find_and_load)
72/1 0.000 0.000 0.151 0.151 <frozen importlib._bootstrap>:2207(_find_and_load_unlocked)
71/1 0.000 0.000 0.151 0.151 <frozen importlib._bootstrap>:1186(_load_unlocked)
46/1 0.000 0.000 0.151 0.151 <frozen importlib._bootstrap>:1122(_exec)
46/1 0.000 0.000 0.151 0.151 <frozen importlib._bootstrap>:1465(exec_module)
74/1 0.000 0.000 0.151 0.151 <frozen importlib._bootstrap>:313(_call_with_frames_removed)
54/1 0.004 0.000 0.151 0.151 {built-in method exec}
1 0.000 0.000 0.151 0.151 macosx.py:1(<module>)
1 0.000 0.000 0.150 0.150 pkgdata.py:18(<module>)
25/3 0.000 0.000 0.122 0.041 <frozen importlib._bootstrap>:1156(_load_backward_compatible)
8/1 0.026 0.003 0.121 0.121 {method 'load_module' of 'zipimport.zipimporter' objects}
1 0.000 0.000 0.101 0.101 __init__.py:15(<module>)
1 0.000 0.000 0.079 0.079 config_reader.py:115(build_from_config)
2 0.000 0.000 0.056 0.028 common.py:43(reset_screen)
2 0.055 0.027 0.055 0.027 {built-in method set_mode}
72/71 0.001 0.000 0.045 0.001 <frozen importlib._bootstrap>:2147(_find_spec)
70/69 0.000 0.000 0.043 0.001 <frozen importlib._bootstrap>:1934(find_spec)
70/69 0.001 0.000 0.043 0.001 <frozen importlib._bootstrap>:1902(_get_spec)
92 0.041 0.000 0.041 0.000 {built-in method load_extended}
6 0.000 0.000 0.041 0.007 new_map.py:74(add_character)
6 0.000 0.000 0.041 0.007 new_character.py:32(added_to_map)
6 0.001 0.000 0.041 0.007 new_character.py:265(__init__)
1 0.000 0.000 0.038 0.038 macosx.py:14(Video_AutoInit)
1 0.038 0.038 0.038 0.038 {built-in method InstallNSApplication}
1 0.036 0.036 0.036 0.036 {built-in method quit}
65 0.001 0.000 0.036 0.001 re.py:277(_compile)
49 0.000 0.000 0.036 0.001 re.py:221(compile)
最终的答案是 OS X 下的视网膜显示是差异化因素。 运行 即使在同一个 Mac 的外部显示器上也能正常工作。但是将 window 移到视网膜显示器上会使它变慢。连接或不连接外接显示器。
另一方面,它在 Linux 下的同一视网膜显示器上运行得很好。目前尚不清楚显示管理器/渲染的差异是什么导致了这种情况,但我怀疑对此有什么办法。
将游戏分辨率更改为全屏对我很有帮助。 试试这个:
window = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
而不是:
window = pygame.display.set_mode((winx, winy))