I wrote this code to test the performance of move constructor, comparing to STL vector. Created 2 classes LargeType uses the vector container from the Standard Template Library (STL). On the other hand, LargeTypeRaw uses a raw pointer. At the end created two vectors of LargeType and LargeTypeRaw with random numbers Calls a custom insertSort function to sort the two vectors Measures the time it takes to sort the two vectors and prints the results. Results for 10.000 vectorsize are like:
Sorting LargeType Result: 2.52 sec --- Sorting LargeTypeRaw Result: 0.28 sec
My question : Is the difference between 2 tests are extremely high? and what might be the reason regarding the complete code below? Could you please help me figure out?
t#include <iostream>
#include <vector>
#include <ctime>
using namespace std;
template <typename T>
class LargeType {
public:
explicit LargeType(int initialSize = 10)
: data(initialSize) {}
int getSize() const {
return data.size();
}
// Operator '<' LargeType
bool operator<(const LargeType& rhs) {
return data.size() < rhs.data.size();
}
private:
vector<T> data;
};
// Assignment Part 2 -3
template <typename T>
class LargeTypeRaw {
public:
explicit LargeTypeRaw(int size = 10)
:sizeRaw {size}, dataRaw {new T[sizeRaw] }
{}
// LargeTypeRaw Copy constructor
LargeTypeRaw(const LargeTypeRaw <T>& rhs)
//: sizeRaw {rhs.sizeRaw}, dataRaw {new T[sizeRaw] }
{
sizeRaw = rhs.sizeRaw;
dataRaw = new T[sizeRaw];
copy(rhs.dataRaw, rhs.dataRaw + sizeRaw, dataRaw);
//copy(rhs.dataRaw, rhs.dataRaw + sizeRaw,dataRaw);
}
// LargeTypeRaw Move constructor
LargeTypeRaw(LargeTypeRaw&& rhs) noexcept
: sizeRaw{rhs.sizeRaw} , dataRaw {rhs.dataRaw}
{
rhs.dataRaw = nullptr;
}
// LargeTypeRaw Copy assignment operator '= '
LargeTypeRaw& operator=(const LargeTypeRaw& rhs) {
if (this != &rhs) {
delete[] dataRaw;
sizeRaw = rhs.sizeRaw;
dataRaw= new T [sizeRaw];
copy(rhs.dataRaw, rhs.dataRaw+sizeRaw, dataRaw);
}
return *this;
// LargeTypeRaw Smaller than operator ' <'
}
bool operator<(LargeTypeRaw & rhs) {
return this->sizeRaw < rhs.getSize();
}
// LargeTypeRaw Move assignment operator
LargeTypeRaw& operator=(LargeTypeRaw && rhs) noexcept {
if (this != &rhs) {
delete[] dataRaw;
sizeRaw= rhs.sizeRaw;
dataRaw = rhs.dataRaw;
rhs.dataRaw = nullptr;
}
return *this;
}
// LargeTypeRaw Destructor
~LargeTypeRaw() {
delete[] dataRaw;
}
int getSize() const
{
return sizeRaw;
}
private:
int* dataRaw;
int sizeRaw;
};
//Insertion sort algorithm with vector
template <typename T>
void insertSort(vector<T> & arr) {
for (int i = 1; i < arr.size(); i++) {
auto key = move(arr[i]);
int j = i - 1;
while (j > -1 && key < arr[j]) {
arr[j + 1] = move(arr[j]);
j--;
}
arr[j + 1] = move(key);
}
}
int main() {
srand (time(NULL));
// Assignment Part 4- Testing and comparing the Copy and Move Constructor performances
int vecSize = 10000;
// Filling the vector arrays with random numbers 1-100
vector<LargeType<int>> vecLarge (vecSize);
vector<LargeTypeRaw <int>> vecRaw (vecSize);
for (int i = 0; i < vecSize; i++) {
int randNum =rand() % 100;
vecLarge[i]= LargeType<int> {randNum};
vecRaw[i] = LargeTypeRaw<int> {randNum};
}
// Setting the start time
clock_t start1 = clock();
//Calling the sorting function and printing out the elapsed execution time
insertSort(vecLarge);
cout << "Sorting LargeType Result: " << (clock() - start1) / (double)(CLOCKS_PER_SEC ) << endl;
clock_t start2 = clock(); // Reset the start time for the second sort function call
insertSort(vecRaw);
cout << "Sorting LargeTypeRaw Result: " << (clock() - start2) / (double)(CLOCKS_PER_SEC ) << endl;
the expected result: largetype vector result should be faster than typeraw vector and the anticipated gap might be less?