Choice of the most performant container (array)

后端 未结 5 817
野性不改
野性不改 2021-02-08 02:09

This is my little big question about containers, in particular, arrays.

I am writing a physics code that mainly manipulates a big (> 1 000 000) set of \"particles\" (wit

5条回答
  •  面向向阳花
    2021-02-08 02:31

    So is it a good idea to use std::vector ?

    Usually, a std::vector should be the first choice of container. You could use either std::vector<>::reserve() or std::vector<>::resize() to avoid reallocations while populating the vector. Whether any other container is better can be found by measuring. And only by measuring. But first measure whether anything the container is involved in (populating, accessing elements) is worth optimizing at all.

    If I use a std::vector, should I create a two dimensional array like std::vector > [...]?

    No. IIUC, you are accessing your data per particle, not per row. If that's the case, why not use a std::vector, where particle is a struct holding six values? And even if I understood incorrectly, you should rather write a two-dimensional wrapper around a one-dimensional container. Then align your data either in rows or columns - what ever is faster with your access patterns.

    Do you think that Blitz is OK, or is it useless in my case?

    I have no practical knowledge about blitz++ and the areas it is used in. But isn't blitz++ all about expression templates to unroll loop operations and optimizing away temporaries when doing matrix manipulations? ICBWT.

提交回复
热议问题