C++ 用1D Arrat中的项替换2D数组中的项的最快方法
我这里有一个关于入境替代的问题。假设我们有一个固定大小的矩阵(平方)(未排序的2D数组),一个数字替换策略列表和另一个数字替换策略列表。我们在矩阵上循环,如果条目与replacementPolicy中的(第一个)元素具有相同的值,我们记住位置i,并用substitudeNUMBER中的第i个元素替换该条目。听起来有点复杂,代码如下:C++ 用1D Arrat中的项替换2D数组中的项的最快方法,c++,arrays,C++,Arrays,我这里有一个关于入境替代的问题。假设我们有一个固定大小的矩阵(平方)(未排序的2D数组),一个数字替换策略列表和另一个数字替换策略列表。我们在矩阵上循环,如果条目与replacementPolicy中的(第一个)元素具有相同的值,我们记住位置i,并用substitudeNUMBER中的第i个元素替换该条目。听起来有点复杂,代码如下: void substitute_entry() { // For each entry in the matrix for (int column
void substitute_entry() {
// For each entry in the matrix
for (int column = 0; column < MATRIX_SIZE; ++column) {
for (int row = 0; row < MATRIX_SIZE; ++row) {
// Search for the entry in the original number list
// and replace it with corresponding the element in the substituted number list
int index = -1;
for (int i = 0; i < LIST_SIZE; i++) {
if (replacementPolicy[i] == MATRIX[row][column]) {
index = i;
}
}
MATRIX[row][column] = substitutedNUMBER[index];
}
}
}
#include <iostream>
#include <chrono>
#include <omp.h>
#define MATRIX_SIZE 1000
#define LIST_SIZE 1000
int arr[MATRIX_SIZE][MATRIX_SIZE];
int replacementPolicy[LIST_SIZE];
int substitutedNUMBER[MATRIX_SIZE];
void substitute_entry() {
// For each entry in the matrix
#pragma omp parallel for
for (int column = 0; column < MATRIX_SIZE; ++column) {
#pragma omp parallel for
for (int row = 0; row < MATRIX_SIZE; ++row) {
// Search for the entry in the original number list
// and replace it with corresponding the element in the substituted number list
int index = -1;
for (int i = 0; i < LIST_SIZE; i++) {
if (replacementPolicy[i] == arr[row][column]) {
index = i;
}
}
arr[row][column] = substitutedNUMBER[index];
}
}
}
int main()
{
omp_set_num_threads(4);
for ( int i = 0; i<MATRIX_SIZE ; i++)
{
replacementPolicy[i] = i;
substitutedNUMBER[i] = i;
for ( int j=0; j<MATRIX_SIZE ; j++)
{
arr[i][j] = i+j;
}
}
auto start = std::chrono::high_resolution_clock::now();
substitute_entry();
auto end = std::chrono::high_resolution_clock::now();
uint64_t diff = std::chrono::duration_cast<std::chrono::microseconds>(end-start).count();
std::cerr << diff << '\n';
return 0;
}
void substitute_entry(){
//对于矩阵中的每个条目
for(int column=0;column
但是,我希望优化此代码以实现更快的运行时。我的第一个想法是切换for循环——首先切换列,然后切换行,但这不会显著影响运行时。我的第二个想法是使用更好的算法来替换条目,但不幸的是,我在测试时搞砸了。有没有更好的办法
谢谢大家! 我认为您的循环非常适合多线程解决方案,例如,使用OpenMP,凭借其功能,您可以预期性能会有显著提高。我对您的代码做了一些更改,如下所示:
void substitute_entry() {
// For each entry in the matrix
for (int column = 0; column < MATRIX_SIZE; ++column) {
for (int row = 0; row < MATRIX_SIZE; ++row) {
// Search for the entry in the original number list
// and replace it with corresponding the element in the substituted number list
int index = -1;
for (int i = 0; i < LIST_SIZE; i++) {
if (replacementPolicy[i] == MATRIX[row][column]) {
index = i;
}
}
MATRIX[row][column] = substitutedNUMBER[index];
}
}
}
#include <iostream>
#include <chrono>
#include <omp.h>
#define MATRIX_SIZE 1000
#define LIST_SIZE 1000
int arr[MATRIX_SIZE][MATRIX_SIZE];
int replacementPolicy[LIST_SIZE];
int substitutedNUMBER[MATRIX_SIZE];
void substitute_entry() {
// For each entry in the matrix
#pragma omp parallel for
for (int column = 0; column < MATRIX_SIZE; ++column) {
#pragma omp parallel for
for (int row = 0; row < MATRIX_SIZE; ++row) {
// Search for the entry in the original number list
// and replace it with corresponding the element in the substituted number list
int index = -1;
for (int i = 0; i < LIST_SIZE; i++) {
if (replacementPolicy[i] == arr[row][column]) {
index = i;
}
}
arr[row][column] = substitutedNUMBER[index];
}
}
}
int main()
{
omp_set_num_threads(4);
for ( int i = 0; i<MATRIX_SIZE ; i++)
{
replacementPolicy[i] = i;
substitutedNUMBER[i] = i;
for ( int j=0; j<MATRIX_SIZE ; j++)
{
arr[i][j] = i+j;
}
}
auto start = std::chrono::high_resolution_clock::now();
substitute_entry();
auto end = std::chrono::high_resolution_clock::now();
uint64_t diff = std::chrono::duration_cast<std::chrono::microseconds>(end-start).count();
std::cerr << diff << '\n';
return 0;
}
#包括
#包括
#包括
#定义矩阵大小为1000
#定义列表大小为1000
int arr[矩阵大小][矩阵大小];
int replacementPolicy[列表大小];
整数替代数[矩阵大小];
无效替代项(){
//对于矩阵中的每个条目
#pragma-omp并行
for(int column=0;column