#include <iostream>
#include <format>
#include <chrono>
#include <vector>
#include <functional>
#include "Random.h"
#include "MatrixOperations.h"
int main()
{
setlocale(LC_ALL, "ru");
auto& random = Random::Current();
auto GenerateSquareMatrix = [&random](int size)
{
std::vector<int> matrix(size * size);
for(auto& element : matrix)
{
element = random.GenerateNumber(1, 9);
}
return matrix;
};
auto MeasureTaskTime = [](std::function<void()> action)
{
auto startTime = std::chrono::high_resolution_clock::now();
action();
auto endTime = std::chrono::high_resolution_clock::now();
return endTime - startTime;
};
auto PrintMultiplyMatrixTime = [](int matrixSize, int threadsAmount, auto time)
{
std::cout << std::format(
"Время умножения матрицы {} x {} при числе потоков {} = {}\n",
matrixSize,
matrixSize,
threadsAmount,
time
);
};
const int OneThread = 1;
const int FourThreads = 4;
const int MatrixSize8x8 = 8;
const int MatrixSize10x10 = 10;
// Умножение матриц 8x8
std::vector<int> matrixA = GenerateSquareMatrix(MatrixSize8x8);
std::vector<int> matrixB = GenerateSquareMatrix(MatrixSize8x8);
auto timeMultiplying8x8By1Thread = MeasureTaskTime([&]()
{
MatrixOperations::Multiply(matrixA, matrixB);
});
auto timeMultiplying8x8By4Thread = MeasureTaskTime([&]()
{
MatrixOperations::Multiply(matrixA, matrixB, FourThreads);
});
// Умножение матриц 10x10
matrixA = GenerateSquareMatrix(MatrixSize10x10);
matrixB = GenerateSquareMatrix(MatrixSize10x10);
auto timeMultiplying10x10By1Thread = MeasureTaskTime([&]()
{
MatrixOperations::Multiply(matrixA, matrixB);
});
auto timeMultiplying10x10By4Thread = MeasureTaskTime([&]()
{
MatrixOperations::Multiply(matrixA, matrixB, FourThreads);
});
// Вывод результата
PrintMultiplyMatrixTime(
MatrixSize8x8,
OneThread,
std::chrono::duration_cast<std::chrono::microseconds>(timeMultiplying8x8By1Thread)
);
PrintMultiplyMatrixTime(
MatrixSize8x8,
FourThreads,
std::chrono::duration_cast<std::chrono::microseconds>(timeMultiplying8x8By4Thread)
);
PrintMultiplyMatrixTime(
MatrixSize10x10,
OneThread,
std::chrono::duration_cast<std::chrono::microseconds>(timeMultiplying10x10By1Thread)
);
PrintMultiplyMatrixTime(
MatrixSize10x10,
FourThreads,
std::chrono::duration_cast<std::chrono::microseconds>(timeMultiplying10x10By4Thread)
);
}