0

I have an algorithm to pick up and sum up the specific data from a 2-dimensional array at a time, which was done with the following 2-fold loop

#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <vector>
#include <cmath>

using namespace std;

int main(int argc, char* argv[])
{
  double data[2000][200];
  double result[200];
  int len[200];
  vector< vector<int> > index;

  srand (time(NULL));
  // initialize data here
  for (int i=0; i<2000; i++) for (int j=0; j<200; j++) data[i][j] = rand();

  // each index element contains some indices for some elements in data, each index elements might have different length
  // len[i] tell the size of vector at index[i]
  for (int i=0; i<200; i++)
  {
    vector<int> c;
    len[i] = (int)(rand()%100 + 1);
    c.reserve(len[i]);
    for (int j=0; j<len[i]; j++) 
    {
      int coord= (int)(rand()%(200*2000));
      c.push_back(coord);
    }
    index.push_back(c);
  }

  for (int i=0; i<200; i++)
  {
    double acc=0.0;
    for (int j=0; j<index[i].size(); j++) acc += *(&data[0][0] + (int)(index[i][j]));
    result[i] = acc;
  }

  return 0;
}

Since this algorithm will be applied to a big array and the 2-fold might be executed in quite long time. I am thinking if stl algorithm will help this case but stl is so far too abstract for me and I don't know how to use that in 2-fold loop. Any suggestion or idea is more then welcomed.


Following other posts and information I found online, I am trying to use for_each to solve the issue

double sum=0.0;
void sumElements(const int &n)
{
  sum += *(&data[0][0] + n);
}

void addVector(const vector<int>& coords)
{
   for_each( coords.begin(), coords.end(), sumElements)
}

for_each( index.begin(), index.end(), addVector);

But this code have two issues. Firstly, it doesn't compile on void sumElements(const int &n), many error message comes out. Second, even it works it won't store the result to the right place. For the first for_each, my intention is to enumerate each index, so to calculate the corresponding sum and store the sum as the corresponding element of results array.

user1285419
  • 2,183
  • 7
  • 48
  • 70
  • Take a look at this http://stackoverflow.com/questions/3221812/sum-of-elements-in-a-stdvector – Antonio Jul 28 '13 at 21:52
  • `acc += data[index[i]];` doesn't make sense with `double data[2000][200];` – P0W Jul 28 '13 at 21:55
  • Also `index` vector appears to be vector of vector of `int`, but you're adding `double` matrix into it. What are you trying to achieve ? – P0W Jul 28 '13 at 22:04
  • I am so sorry for the code ... I edit the code in vi installed in the server but when I copy and paste, it doesn't really make a copy so the code is wrong. I've edited the post. – user1285419 Jul 28 '13 at 22:17
  • Hi Antonio, I am trying my best to understand the look your gave above. But to my understanding, all methods provided are a bit different from my case. There, they enumerate the vector (of int or double) by for_each or accumulate or boost FOREACH so to add the number. But in my case, the vector being added are not given directly. For each sum, the vector of data being summed are extract from the index given by another vector. So it seems that I have to use two for_each to do the sum. I try for an hour but didn't get it work (I put the code I tried in the post too) – user1285419 Jul 28 '13 at 23:31

1 Answers1

0

First off, STL is not going to give you magic performance benefits.

There already is a std::accumulate which is easier then building your own. Probably not faster, though. Similarly, there's std::generate_n which calls a generator (such as &rand) N times.

You first populate c before you call index.push_back(c);. It may be cheaper to push an empty vector, and set std::vector<int>& c = index.back().

MSalters
  • 173,980
  • 10
  • 155
  • 350