best way to run through loop and perform operation

This is a discussion on best way to run through loop and perform operation within the C++ Programming forums, part of the General Programming Boards category; I have a vector of bools that looks like vector<bool> vals position: 0 1 2 3 4 5 bool val: ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    24

    best way to run through loop and perform operation

    I have a vector of bools that looks like

    vector<bool> vals
    position: 0 1 2 3 4 5
    bool val: 0 1 0 0 1 1

    the bool val indexes to a 6x6 matrix A where i have random values stored.
    in my following vector, I would want to sum the values of A(1,4),A(4,5),A(5,5)

    if i had
    position: 0 1 2 3 4 5
    bool val: 0 1 1 0 1 0

    I would want to sum A(1,2), A(2,4),A(4,4)

    what is the fastest way to loop through the bool vector and calculate the values i want?

    Code:
    double sum = 0;
    int j = 0;
    int k = 0;
    for(int i =0; i<vals.size(); ++i)
    {
    k += -(vals[i]-1);
    j= i*vals[i];
    //sum +=A[j-k][j]
    }
    i was thinking something along the lines of the above, but there are problems when my bool vector vals[0]=1, and my matrix A might have non-zero values for A[row][col] that i sum.

    thoughts?

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,511
    Quote Originally Posted by lawrenced View Post
    vector<bool> vals
    position: 0 1 2 3 4 5
    bool val: 0 1 0 0 1 1

    the bool val indexes to a 6x6 matrix A where i have random values stored.
    in my following vector, I would want to sum the values of A(1,4),A(4,5),A(5,5)

    if i had
    position: 0 1 2 3 4 5
    bool val: 0 1 1 0 1 0

    I would want to sum A(1,2), A(2,4),A(4,4)
    I'm not sure I understand the relationship you are establishing between the vector and your matrix A.

    How come the vector[1] means A(1,2) and vector[2] means A(2,4)?
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Are you saying (and this seems like a huuuuuuuuuuge stretch, but it's the best pattern that I can find) that you will always have three "true" values in your vector (call them a, b, and c) and you will want to add A(a,b) + A(b,c) + A(c,c)?

  4. #4
    Registered User
    Join Date
    Jul 2009
    Posts
    24
    yes, i will have a series of true values (not necessarily just 3) and i want to pull the 1st true value and then the next true value to give A(1st,2nd) on the last true value in the list, I want to repeate A(last,last) but this can probably be done after the loop has stopped.

  5. #5
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    unless your sparse matrix is populated according to some very odd pattern, you will need atleast M*N bools to record the 'valued' positions of an MxN matrix

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by lawrenced View Post
    yes, i will have a series of true values (not necessarily just 3) and i want to pull the 1st true value and then the next true value to give A(1st,2nd) on the last true value in the list, I want to repeate A(last,last) but this can probably be done after the loop has stopped.
    Then in your loop you will need to keep track of not only the current value but the most recent true value.

  7. #7
    Registered User
    Join Date
    Jul 2009
    Posts
    24
    Quote Originally Posted by tabstop View Post
    Then in your loop you will need to keep track of not only the current value but the most recent true value.
    can anyone think of the best way to do that while running the loop? i think the code:

    Code:
    double sum = 0;
    int j = 0;
    int k = 0;
    for(int i =0; i<vals.size(); ++i)
    {
    k += -(vals[i]-1);
    j= i*vals[i];
    sum +=vals[i]*A[j-k][j] //will only sum the value if true
    }
    will get rid of the problems in my A matrix - but how can I record the value of the most recent true without some container?

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by lawrenced View Post
    can anyone think of the best way to do that while running the loop? i think the code:

    Code:
    double sum = 0;
    int j = 0;
    int k = 0;
    for(int i =0; i<vals.size(); ++i)
    {
    k += -(vals[i]-1);
    j= i*vals[i];
    sum +=vals[i]*A[j-k][j] //will only sum the value if true
    }
    will get rid of the problems in my A matrix - but how can I record the value of the most recent true without some container?
    Code:
    if (vals[i])
        most_recent_true_value = i;
    I don't think "variable" has quite the same connotations as "container".

  9. #9
    Registered User
    Join Date
    Jul 2009
    Posts
    24
    Quote Originally Posted by tabstop View Post
    Code:
    if (vals[i])
        most_recent_true_value = i;
    I don't think "variable" has quite the same connotations as "container".
    tabstop, i am able to perform the above loop without any if statements - this should run signifcantly faster than using if statements correct?

  10. #10
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Quote Originally Posted by lawrenced View Post
    tabstop, i am able to perform the above loop without any if statements - this should run signifcantly faster than using if statements correct?
    Probably not "significantly". A conditional statement is something that has pretty low overhead.
    bit∙hub [bit-huhb] n. A source and destination for information.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by lawrenced View Post
    tabstop, i am able to perform the above loop without any if statements - this should run signifcantly faster than using if statements correct?
    I haven't done any tests, but I'm pretty sure the answer is "definitely not". You have a subtraction, a negation, two multiplies, two additions, and three variable assignments on every pass through the loop. Contrast that with an if-comparison every time through the loop, plus one add and two variable assignments only when necessary.

  12. #12
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    to actually venture an answer to your question, you should store the non-zero positions in the vector, and not use the bitmask technique. somethign like this:

    Code:
      std::vector<unsigned int>i; 
      i.push_back(1);
      i.push_back(4);
      i.push_back(5);
      int a[6][6]={0};
      unsigned int* index = &i[0];
      unsigned int* const end = (&i[0])+i.size();
      unsigned int sum=0;
      while(++index<end)
      {
        sum+=a[*(index-1)][*index];
      }
      std::cout<< sum;

  13. #13
    Registered User
    Join Date
    Jul 2009
    Posts
    24
    all, have figured out how to do this with out containers or vectorizations is significantly faster than with vectors~ x2.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Possible to run while Loop Every 5 Seconds?
    By Paul22000 in forum C++ Programming
    Replies: 2
    Last Post: 12-06-2008, 01:42 PM
  2. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 12:38 PM
  3. Can a "switch" be inside a loop?
    By gmk0351 in forum C Programming
    Replies: 5
    Last Post: 03-28-2008, 05:47 PM
  4. Replies: 16
    Last Post: 11-23-2007, 12:48 PM
  5. syntax question
    By cyph1e in forum C Programming
    Replies: 19
    Last Post: 03-30-2006, 11:59 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21