C ++ 11 вложенный класс быстрее, чем отдельный класс, почему

У меня есть один вопрос, но ответа я не нашел. Я написал некоторый комбинаторный решатель и отдельную часть основной структуры сохранения для разделения на класс под названием Matrix. Который я заполнил в алгоритме возврата. Когда я создаю новый экземпляр указателя на Matrix и часто вызываю метод set и unset на классе Matrix, производительность снижается в 2 раза. Это стало в 2 раза медленнее, чем раньше. Но если я создаю вложенный класс, производительность будет в порядке. Но я не знаю почему? Есть ли какое-либо решение, как иметь отдельный класс без вложенного класса, но производительность будет в порядке? Спасибо за ваше время. Я вставляю Matrix.h и Matrix.cpp. Проект, который я тестировал на macOsx и linux, скомпилирован g ++ 4.2.1 с объявлением -O2

        class Matrix {
private:
int rows;
int columns;
int filledCells = { 0 };
int * data = nullptr;
int * freeCellInColumn = nullptr;
public:
Matrix(int rows, int columns);
~Matrix();
void set(const int & column, const int & value);
void unset(const int & column);
int & get(const int & row, const int & column);
int & getFreeCellsInColumn(const int & column);
int getFilledCellsInColumn(const int & column);
int * getData();
const int & getFilledCells() const;
};

и определение

#include <stdexcept>
#include "Matrix.h"
Matrix::Matrix(int rows, int columns) {
if (rows < 1 || columns < 1) {
std::runtime_error("Number of rows or columns at matrix is invalid.\n");
}
this->rows = rows;
this->columns = columns;
this->data = new int [rows * columns]();
this->freeCellInColumn = new int[columns]();
for (int i = 0; i < columns; i++) {
this->freeCellInColumn[i] = this->rows;
}
}

Matrix::~Matrix() {
delete [] this->data;
this->data = nullptr;

delete [] this->freeCellInColumn;
freeCellInColumn = nullptr;
}

void Matrix::set(const int & column, const int & value) {
this->data[(--this->freeCellInColumn[column]) * this->columns + column] =    value;
++this->filledCells;
}

void Matrix::unset(const int & column) {
this->data[(this->freeCellInColumn[column]++) * this->columns + column] = 0;
--this->filledCells;
}

int * Matrix::getData() {
return this->data;
}

int & Matrix::get(const int & row, const int & column) {
return this->data[row * this->columns + column];
}

const int & Matrix::getFilledCells() const {
return this->filledCells;
}

int & Matrix::getFreeCellsInColumn(const int & column) {
return this->freeCellInColumn[column];
}

int Matrix::getFilledCellsInColumn(const int & column) {
return this->rows - this->freeCellInColumn[column];
}

Я вставляю часть вложенного класса, что я подразумеваю под вложенным классом в классе Labeling, означая объявление класса Matrix в классе Labeling,
Метод int * getData () я использую только для результата печати только один раз.

class Labelling {
private:
class Matrix {
private:
int rows;
int columns;
int filledCells = { 0 };
int * data = nullptr;
int * freeCellInColumn = nullptr;
public:
Matrix(int rows, int columns);
~Matrix();
void set(const int & column, const int & value);
void unset(const int & column);
int & get(const int & row, const int & column);
int & getFreeCellsInColumn(const int & column);
int getFilledCellsInColumn(const int & column);
int * getData();
const int & getFilledCells() const;
};
TextPrinter * printer;
NSumProblemSolver * solver;
Matrix * solution;
...};

0

Решение

Задача ещё не решена.

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector