Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ - Performance of vector of pointer to objects, vs performance of objects

In this case the question scenario is a game, so all resources are allocated at the beginning then iterated over for a level.

The objects being stored in the vector are instances of complex classes, and of course the actual copying them into the vector at load-time is time-consuming, but of low-concern.

But if my main concern is the speed of iteration over the class objects at runtime, would I be better to store the class objects themselves in the vector, rather than just pointers to the class objects as is traditionally recommended?

I am not worried about memory management in this example, only speed of iteration.

like image 292
metamorphosis Avatar asked Mar 28 '14 03:03

metamorphosis


People also ask

What is the main issue with storing objects in the vector class as opposed to pointers?

Having vector of objects is much slower than a vector of pointers. The results are because algorithms such as sorting need to move elements inside the container. So they not only read the data but also perform a copy (when the algorithm decides to swap items or move to a correct place according to the order).

Is STD array faster than vector?

A std::vector can never be faster than an array, as it has (a pointer to the first element of) an array as one of its data members. But the difference in run-time speed is slim and absent in any non-trivial program. One reason for this myth to persist, are examples that compare raw arrays with mis-used std::vectors.

Is Vector faster than set C++?

The time complexity for the insertion of a new element is O(log N). Vector is faster for insertion and deletion of elements at the end of the container. Set is faster for insertion and deletion of elements at the middle of the container.

How do you store vectors of pointers?

You can store pointers in a vector just like you would anything else. Declare a vector of pointers like this: vector<MyClass*> vec; The important thing to remember is that a vector stores values without regard for what those values represent.


2 Answers

I'm answering this question late, but the performance aspect is important and the answers online so far have been purely theoretical and/or focusing exclusively on the memory-management aspects. So here is some actual benchmarking info on three related scenarios I recently tried. Your results may be different but at least there's some idea of how things pan out in a practical application.

The class A referenced here has about 10 member fields, half of which are primitives and the other half are std::string, std::vector<int>, and other dynamically sized containers. The application has already been fairly optimized and thus we would like to see which architecture now gives us the fastest looping over the collection of A. The values of any of A object's member fields may be changing over the application lifetime, but the number of A objects in the vector do not change over the many repeated iterations we perform (this continual iterating constitutes about 95% of this application's execution time). In all scenarios, looping was performed with the typical std::iterator or std::const_iterator. Each enumerated A object has at least several member fields accessed.

Scenario 1 — Vector Of Object Pointers

Although the simplest, this architecture of std::vector<A*> ended being slightly slower than the others.

Scenario 2 — Vector Of Object Pointers, Objects Are Allocated Using Placement New

The idea behind this approach is that we can improve the locality of caching by forcing our objects to be allocated into contiguous memory space. So the std::vector<A*> of object pointers is guaranteed to be contiguous by the std::vector implementation and the A objects themselves will also be contiguous on the heap because we've used the placement new idiom. I used the same approach outlined in this answer; more info on placement new can be found here.

This scenario was 2.7% faster than Scenario 1.

Scenario 3 — Vector Of Objects

Here we use std::vector<A> directly. The std::vector implementation guarantees our A objects will be contiguous in memory. Note that a std::vector of objects does involve considerations of the move and copy constructors of A. To avoid unnecessary moving and/or reconstruction, it is best to std::vector.reserve() the maximum possibly needed size in advance (if possible) and then use std::vector.emplace_back() (instead of push_back()) if at all possible. Looping over this structure was the fastest because we are able to eliminate one level of pointer indirection.

This approach was 6.4% faster than Scenario 1.

A related answer to a different question also shows that plain objects (as class members) can be quite faster than the respective pointers (as class members).

like image 145
Special Sauce Avatar answered Oct 02 '22 20:10

Special Sauce


No, She is not wrong, she is absolutely right, though you are asking only about fast iteration, but that has alot of link with Memory... More the memory stack slower will be the access...

I have a live demo...

#include <iostream>
#include <string>
#include <vector>
#include "CHRTimer.h"

struct Items
{
    std::string name;
    int id;
    float value;
    float quantity;
};

void main()
{

    std::vector<Items> vecItems1;
    for(int i = 0; i < 10000; i++)
    {
        Items newItem;
        newItem.name = "Testing";
        newItem.id = i + 1;
        newItem.value = 10.00;
        newItem.quantity = 1.00;

        vecItems1.push_back(newItem);
    }

    CHRTimer g_timer;
    g_timer.Reset();
    g_timer.Start();
    for(int i = 0; i < 10000; i++)
    {
        Items currentItem = vecItems1[i];
    }
    g_timer.Stop();
    float elapsedTime1 = g_timer.GetElapsedSeconds();
    std::cout << "Time Taken to load Info from Vector of 10000 Objects -> " << elapsedTime1 << std::endl;

    std::vector<Items*> vecItems;
    for(int i = 0; i < 100000; i++)
    {
        Items *newItem = new Items();
        newItem->name = "Testing";
        newItem->id = i + 1;
        newItem->value = 10.00;
        newItem->quantity = 1.00;

        vecItems.push_back(newItem);
    }

    g_timer.Reset();
    g_timer.Start();
    for(int i = 0; i < 100000; i++)
    {
        Items *currentItem = vecItems[i];
    }
    g_timer.Stop();
    float elapsedTime = g_timer.GetElapsedSeconds();
    std::cout << "\nTime Taken to load Info from Vector of 100000 pointers of Objects -> " << elapsedTime;
}
like image 30
Kamal Singla Avatar answered Oct 02 '22 20:10

Kamal Singla