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