Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C++

Name: Snake4D 2012-04-11 17:38

A simple question...

is it better to traverse a std::vector like this:

std::vector<int> myvec;
//populate the vector
for(int i = 0; i < myvec.size(); ++i){
    myvec[i] += 1;
}


or is better like this?

std::vector<int> myvec;
typedef std::vector<int>::iterator IT_int;
for(IT_int it = myvec.begin(); it != myvec.end(); ++it){
     *it += 1;
}

Name: Cudder !MhMRSATORI!FBeUS42x4uM+kgp 2012-04-13 6:41

>>47
Caches, alignment, other instructions, etc.

cmp and test are both single-cycle.

Name: Anonymous 2012-04-13 7:48

>>47
Not who you are replying to, but:

Equality could be tested for with n XOR gates and a dumb population count of OR gates (lg n)-1 levels deep. This gives you a time complexity of (lg n) to do a comparison.

Less than could be tested for by doing a simple subtraction and looking at the sign bit. How would you do this? Put one number through a line of NOT gates, then feed it into a CPA (Carry Propagating Adder) with a '1' set on the carry input. The CPA used will be, with almost 100% certainty, be one with a prefix network, giving a time complexity of (lg n).

So both of these operations are equally fast, but here's the fun part:

CPUs are CLOCKED DIGITAL CIRCUITS and these operations, sometimes even including multiplication, are all done in ONE CLOCK CYCLE

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List