简体   繁体   English

如果我每次创建它时都被迫重新启动每个粒子,我是否应该使用Pools作为粒子

[英]Should I use Pools for particles if i forced to re-init every particle every time i create them

I am creating a particle system in XNA4 and I've bumped into problem. 我正在XNA4中创建一个粒子系统,但我遇到了问题。 My first particle system was a simple list of particles, whose instances are created when needed. 我的第一个粒子系统是一个简单的粒子列表,其实例是在需要时创建的。 But then I read about using pools. 但后来我读到了使用池。

My second system consists of a pool, filled with particles, and an emitter/controller. 我的第二个系统包括一个充满粒子的池和一个发射器/控制器。 My pool is pretty basic, this is the code: 我的游泳池很基本,这是代码:

class Pool<T> where T: new ()
{
    public T[] pool;
    public int nextItem = 0;


    public Pool(int capacity)
    {
        pool = new T[capacity];

        for (int i = 0; i < capacity; i++)
        {
            pool[i] = new T();
        }
    }
    public T Create()
    {
       return pool[nextItem++];
    }
    public void Destroy(T particle)
    {
        pool[--nextItem] = particle;
    }
}

The problem is pool system is much more hungry for CPU. 问题是池系统对CPU更加饥渴。 Every time I take out particle from pool to my emitter I'm forced to re-initialize and reset particles, due the constructor absence and this is a problem. 每次我从池中取出粒子到我的发射器时我都被迫重新初始化并重置粒子,因为构造函数不存在而且这是一个问题。

Is there any point at using pools, if I re-init those particles or I should leave pools for arrays of completely identical objects that never changes? 使用池有什么意义吗,如果我重新初始化那些粒子,或者我应该为完全相同的对象的数组留下池,这些对象永远不会改变?

In an XNA application, typically object pooling isn't supposed to speed up initialization. 在XNA应用程序中,通常不应该对象池加速初始化。 It is to speed up garbage collection. 这是为了加快垃圾收集。 The GCs on consoles are slower, and in a game where your entire update function has 16 milliseconds to run, a collection pause can be visibly noticeable. 控制台上的GC速度较慢,并且在整个更新功能运行16毫秒的游戏中,收集暂停可以明显地显着。 This may or may not a concern for your particular case, especially if this is a PC application, depending on the number and lifetimes of particles you are using. 这可能是或可能不是您特定情况的问题,特别是如果这是一个PC应用程序,取决于您使用的粒子的数量和生命周期。

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

相关问题 .NET ApplicationSettingsBase我每次加载时都应该调用Upgrade()吗? - .NET ApplicationSettingsBase Should I call Upgrade() every time I load? 我应该为每个使用的数据库都有一个类吗? - Should I have one class for every database I use? 我应该在初始化器中创建异常对象还是在每种方法中对其进行编码? - Should I create an exception object in an initializer or code it in every method? 我应该在每次使用它时创建一个全新的SqlConnection,还是每次尝试重新打开一个现有的连接? - Should I create a brand new SqlConnection each time I want to use it, or just attempt to re-open an existing connection each time? 我应该为具有页面对象模式的每个页面使用不同的类吗? - Should i use different class for every page with Page Object Pattern? 每次创建设置时如何更改设置 - how to change settings every time when I create setup 如何存储LinkedIn API AccessToken,以便每次使用LinkedIn API时都不必重新输入凭据? - How do I store the LinkedIn API AccessToken so I don't have to re-enter credentials every time I use the LinkedIn API? 每次我使用 OnCollisionEnter2D 它都不起作用 - Every time I use OnCollisionEnter2D it does not work 我应该将每个班级放在单独的文件中吗? - Should I place every class in separate file? 我应该执行每个查询还是单个查询? - Should I execute every query or single query?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM