[英]Fastest way to substitute the entries in a 2D Array with entries from 1D Arrat
我在這里有一個關於條目替代的問題。 假設我們有一個固定大小的矩陣(平方) MATRIX_SIZE(未排序的二維數組)、一個數字列表 replacementPolicy 和另一個數字 substitudeNUMBER 列表。 我們遍歷矩陣,如果該條目與 replacementPolicy 中的(第一個)元素具有相同的值,我們記住 position i,並用 substitudeNUMBER 中的第 i 個元素替換該條目。 聽起來有點復雜,代碼如下:
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];
}
}
}
但是,我希望優化此代碼以實現更快的運行時。 我的第一個想法是切換 for 循環 - 首先是列,然后是行,但這不會顯着影響運行時。 我的第二個想法是使用更好的算法來替換條目,但不幸的是我在測試時搞砸了。 有沒有更好的方法呢?
謝謝!
我認為您的循環非常適合多線程解決方案,例如,使用 OpenMP,並且憑借其功能,您可以期待性能顯着提高。 我對您的代碼進行了一些更改,如下所示:
#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;
}
您可以注釋掉 3、14、16 和 34 行,並擁有代碼的單線程版本。 在這個 MATRIX_SIZE 為 1000 的示例中,在我只有四個內核的個人計算機上,單線程版本在 3731737 us 完成,多線程版本在 718039 us 完成。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.