Note that there are some explanatory texts on larger screens.

plurals
  1. POshould I use the algorithm or hand-code it in this case?
    text
    copied!<p>Ok, someone tell me which would be better. I need to |= the elements of one vector with another. That is, I want to</p> <pre><code>void orTogether(vector&lt;char&gt;&amp; v1, const vector&lt;char&gt;&amp; v2) { typedef vector&lt;char&gt;::iterator iter; for (iter i = v1.begin(), iter j = v2.begin() ; i != v1.end(); ++i, ++j) *i |= *j; } </code></pre> <p>I can't use for_each due to needing to process 2 collections. I suppose I could do something like</p> <pre><code>struct BitWiseOr { char operator()(const char&amp; a, const char&amp; b) {return a | b;} }; void orTogether2(vector&lt;char&gt;&amp; v1, const vector&lt;char&gt;&amp; v2) { transform(v1.begin(), v1.end(), v2.begin(), v1.begin(), BitwiseOr()); } </code></pre> <p>Is this a more efficient solution even though the top one is in place, but the bottom is an assign? This is right in the middle of a processing loop and I need the fastest code possible.</p> <p>Edit: Added (obvious?) code for BitwiseOr. Also, I'm getting a lot of comments on non-related things like checking the lengths of v2 and changing the names. This is just an example, the real code is more complicated.</p> <p><strong>Well, I profiled both. orTogether2 is much faster than orTogether, so I'll be going with the transform method.</strong> I was surprised, orTogether2 was about 4 times faster in MSVC9 release mode. I ran it twice, changing the order the second time to make sure it wasn't some sort of cache issue, but same results. Thanks for the help everyone.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload