1

For studying purposes, I was trying to create my own array class.

So i just created a simple class with an array and overloaded the [] operator to access the elements in the class:

template <class T>
class Array
{
  private:
    T *arr;
    int size;

  public:
    Array(int arrSize)
    {
        this->arr = new T [arrSize];
        this->size = arrSize;
    }

    T& operator[](int pos)
    {
        return this->arr[pos];
    }
};

When I run a simple test to check how fast different containers access 1.000.000 of their elements, I usually get these results (in seconds):

MyArrayClass: 0.017294

C Array: 0.009943

std::array: 0.014728

std::vector: 0.013836

Why is C array so much faster than my class? Weren't they supposed to be equally fast? At least, I wasn't expecting my own class to take twice the time the c array takes, considering it uses the same principle to access the elements.

Code used to measure the time (timenow = std::chrono::system_clock::now()) :

auto ti = timenow, te=timenow;


Array<int> tst (1000000);
int tst2[1000000];
std::array<int, 1000000> tst3;
vector<int> tst4(1000000);
ArrayT tst5(1000000);

ti = timenow;
for (int i = 0; i < 1000000; i++)
{
    tst[i] = 1;
}
te = timenow;

std::chrono::duration<float> et = te-ti;

cout << "MyArrayClass: " << et.count() << nl

ti = timenow;
for (int i = 0; i < 1000000; i++)
{
    tst2[i] = 1;
}
te = timenow;

et = te-ti;

cout << "C Array: " << et.count() << nl

ti = timenow;
for (int i = 0; i < 1000000; i++)
{
    tst3[i] = 1;
}
te = timenow;

et = te-ti;

cout << "std::array: " << et.count() << nl

ti = timenow;
for (int i = 0; i < 1000000; i++)
{
    tst4[i] = i;
}
te = timenow;

et = te-ti;

cout << "std::vector: " << et.count() << nl
Emanah
  • 56
  • 4
  • 12
    Did you compile this with or without optimizations? – Sam Varshavchik Aug 01 '21 at 21:46
  • 3
    "Weren't they supposed to be equally fast?" Based on what logic? I assume you have some ideas about what does or doesn't cause overhead in C++, but where do those ideas come from? What did they tell you about the specific conditions required to make "zero-overhead" things actually zero overhead? – Karl Knechtel Aug 01 '21 at 21:49
  • What are `ArrayT` and `tst5``. They seems unused. – Phil1970 Aug 01 '21 at 23:43
  • 1
    It is slower because it is executing your code instead of the single instruction that is required to fetch an element from a real array. – user207421 Aug 02 '21 at 01:04

1 Answers1

1

I assume the numbers you gave are for runs built with optimizations on.

There are different reasons why the custom Array may be slower than the built-in one. One remark I'd make, is that the custom array uses heap memory, while the built-in is on the stack. For more info on this topic, see this answer.

Another idea would be to look at generated assembly on smth. like godbolt.com, and compare the instructions.

Also, note that your example is leaking memory - you allocate in constructor but never deallocate that memory.

Maxim Chetrusca
  • 3,262
  • 1
  • 32
  • 28