简体   繁体   English

Pygame:多次加载图像或将表面保留在内存中是否更好?

[英]Pygame: Is it better to load an image multiple times, or keep a surface in memory?

Me and a friend are working on a game, and as part of the streamlining of code (we initially wrote it for a gamejam, that code is not neat at all :p ) he decided to set up the asset loader such that it loads sprites from a file each time they're requested. 我和一个朋友正在开发一个游戏,作为简化代码的一部分(我们最初是为gamejam编写的,该代码根本不是整洁的:p),他决定设置资产加载程序以加载精灵。每次被请求时从文件中删除。

This seems a bit heavy-handed to me. 这对我来说似乎有些沉重。 Isn't disk I/O going to be a big slowdown if we do that? 如果这样做的话,磁盘I / O是否会成为一个很大的放缓? Or am I worrying about nothing here? 还是我在担心什么呢? I haven't looked at his latest changes but I assume that he plans to later expand this to deal with sound effects and music as well. 我没有看过他的最新变化,但我认为他计划以后再扩展它以处理声音效果和音乐。

The code is here: https://github.com/avivbeeri/GameJam 代码在这里: https : //github.com/avivbeeri/GameJam

Any thoughts or insight would be appreciated! 任何想法或见识将不胜感激!

It's much better to keep the surface in memory. 最好将表面保留在内存中。 Reading in the image is a multi-step process that freezes the code while it's being run (unless you're threading, of course). 读取图像是一个多步骤过程,该过程会在代码运行时冻结代码(当然,除非您要进行线程化)。 I mean, unless you're keeping 500+ surfaces in memory, you're not really gonna run into a problem. 我的意思是,除非您要在内存中保留500多个曲面,否则您不会真的遇到问题。 But if you do get to the point of having 500+ images, the best solution would be to make an engine out of a lower level like C anyways. 但是,如果您确实拥有500幅以上的图像,最好的解决方案是无论如何都要使引擎脱离C之类的较低水平。

Short answer: Loading the image every time it's used is slower and worse practice than just keeping it in memory. 简短的回答:每次将图像加载时,比仅将其保存在内存中的做法更慢,更糟。

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

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