Hi Laserlight,
I performed a simple test to compare the performance speed on malloc() and vector<>. I would like to share with you. The taste case was on a 2D array with 1000x1000 with the number of iteration of 1000. The malloc array took 91.3s where as the vector array took 131.5 s. I wonder about the correct implementation on code for the comparison on the those two array. In the real time simulation of heat transfer, the number of iteration can be around 10000000 for a 2D (1000x1000) matrix. The comparison code is as follows.
Code:
//main.cpp
#include <iostream>
#include <vector>
#include <ctime>
#include <stdlib.h>
template <class dataType>
int allocateData2DbyMalloc(dataType**& data2D, unsigned int nRow, unsigned int nColumn) {
data2D = (dataType * *)malloc(nRow * sizeof(dataType*)); // calloc(nRow, sizeof(type1*));
if (data2D == NULL) {
return EXIT_FAILURE;
}
// Now point each row to an array of nColumn type1 objects
for (unsigned int i = 0; i < nColumn; ++i) {
data2D[i] = (dataType*)malloc(nColumn * sizeof(dataType)); //calloc(nColumn, sizeof(type1));
if (data2D[i] == NULL) {
return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
}
template <class dataType>
void deAllocateData2DbyFree(dataType** data2D, unsigned int numberOfRow) {
for (unsigned int row = 0; row < numberOfRow; row++)
free(data2D[row]);
free(data2D);
}
template<typename T>
std::vector<std::vector<T>> createData2D(std::size_t nRow, std::size_t nColumn) {
return std::vector<std::vector<T>>(nRow, std::vector<T>(nColumn));
}
int main()
{
int matDim = 1000;
int matDimX = matDim;
int matDimY = matDim;
int itarationNumber = 1000;
double **matrix2D = nullptr;
std::clock_t start;
double duration;
//-------------------------------- For 2DMallocArray it took 91.3s ----------------------
start = std::clock();
for (int i = 0; i < itarationNumber; i++)
{
allocateData2DbyMalloc(matrix2D, matDimX, matDimY);
if (matrix2D == nullptr) {
std::cout << "Error in 2D Array" << std::endl;
exit(0);
}
for (int row = 0; row < matDimX; row++)
{
for (int col = 0; col < matDimY; col++)
{
int lowest = 1, highest = 10;
int range = (highest - lowest) + 1;
double random_number = lowest + (range * (rand() / (RAND_MAX + 1.0)));
matrix2D[row][col] = random_number;
}
}
if (matrix2D)
{
deAllocateData2DbyFree(matrix2D, matDimX);
matrix2D = nullptr;
}
}
duration = (std::clock() - start) / (double)CLOCKS_PER_SEC;
std::cout << "printf: " << duration << '\n';
//-------------------------------- For 2DVectorarray it took 131.5s ---------------------------------------
/* start = std::clock();
for (int i = 0; i < matDim; i++)
{
auto data2Dnested = createData2D<int>(matDim, matDim);
for (int row = 0; row < matDimX; row++)
{
for (int col = 0; col < matDimY; col++)
{
int lowest = 1, highest = 10;
int range = (highest - lowest) + 1;
double random_number = lowest + (range * (rand() / (RAND_MAX + 1.0)));
data2Dnested[row][col] = random_number;
}
}
}
duration = (std::clock() - start) / (double)CLOCKS_PER_SEC;
std::cout << "printf: " << duration << '\n';*/
//------------------------------- End --------------------------------------------
return 0;
}