0

The following code finds min/max and its location in a given array:

 //Finding the array index//
 #include "stdafx.h"
 #include <iostream>
 #include <iterator>
 #include <list>
 #include <algorithm>
 using namespace std;
 int main () {
 int A[4] = {0, 2, 1, 1};
 const int N = sizeof(A) / sizeof(int);
 cout << "Index of max element: "
 << distance(A, max_element(A, A + N))
 << endl;
 return 0;
 }

I want to improve this code for 2D arrays and take advantage of pre-fetching

So my data is now something like this:

     A[3][10] = { {3,7,2,9,39,4,9,2,19,20}, 
                  {3,7,2,9,33,4,22, 2,19,21}, 
                  {3,7,2,36,33,4,9,2,19,22} 
                };

In actual case the data will be much more. Will I really get any adavantage of prefetching here? If so how do I go about this? Also is there any compiler directive that can instruct the compiler to prefetch data in A?

Edit:

I need the maximum value of the whole 2D array and also the corresponding index. Will be running on x86, Intel i3 , Windows 7.

In this code as you can see I am first finding the maximum value and then finding the location of the maximum value. Is there a way I can simplify this two step process to single and thus speed up the process?

Update: I modified this code so its processing the data in one step unlike earlier when it was first finding the maximum value and then finding the index. The question is how do I use prefetching to improve the performance?

gpuguy
  • 4,607
  • 17
  • 67
  • 125

0 Answers0