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: Anonymous 2012-04-13 0:19

neither nands nor nors are as appropriate as or and/or and

Name: Anonymous 2012-04-13 0:22

This thead is full of idiots. Just use C++11.

Name: Anonymous 2012-04-13 0:24

>>42

s/++//

>>44

Nice dbus.

Name: Anonymous 2012-04-13 0:27

Name: Anonymous 2012-04-13 0:49

Total of elements: 200000
Heap array:
  using index:  0.001267s wall, 0.000000s user + 0.000000s system = 0.000000s CPU (n/a%)
  using std::for_each:  0.000275s wall, 0.000000s user + 0.000000s system = 0.000000s CPU (n/a%)

Stack array:
  using index:  0.000295s wall, 0.000000s user + 0.000000s system = 0.000000s CPU (n/a%)
  using std::for_each:  0.000270s wall, 0.000000s user + 0.000000s system = 0.000000s CPU (n/a%)
  using BOOST_FOREACH:  0.000995s wall, 0.015600s user + 0.000000s system = 0.015600s CPU (n/a%)

Vector:
  using index:  0.000296s wall, 0.000000s user + 0.000000s system = 0.000000s CPU (n/a%)
  using iterators:  0.000549s wall, 0.000000s user + 0.000000s system = 0.000000s CPU (n/a%)
  using std::for_each:  0.000533s wall, 0.000000s user + 0.000000s system = 0.000000s CPU (n/a%)
  using BOOST_FOREACH:  0.000582s wall, 0.000000s user + 0.000000s system = 0.000000s CPU (n/a%)

List:
  using iterators:  0.004329s wall, 0.000000s user + 0.000000s system = 0.000000s CPU (n/a%)
  using std::for_each:  0.009227s wall, 0.015600s user + 0.000000s system = 0.015600s CPU (169.1%)
  using BOOST_FOREACH:  0.007076s wall, 0.000000s user + 0.000000s system = 0.000000s CPU (n/a%)


#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
#include <boost/timer/timer.hpp>
#include <boost/random/mersenne_twister.hpp>
#include <boost/lambda/lambda.hpp>
#include <boost/foreach.hpp>

const static size_t maxsz = 2e5;

boost::lambda::placeholder1_type X;

int main() {
    std::cout << "Total of elements: " << maxsz << std::endl;
    boost::random::mt19937 rng;
    {
        unsigned int* harr = new unsigned int[maxsz];
        for (int i = 0; i < maxsz; ++i) {
            harr[i] = rng();
        }
        std::cout << "Heap array:\n  using index: ";
        {
            boost::timer::auto_cpu_timer t;
            for (int i = 0; i < maxsz; ++i) {
                harr[i] /= 100;
            }
        }
        std::cout << "  using std::for_each: ";
        {
            boost::timer::auto_cpu_timer t;
            std::for_each(harr, harr+maxsz, X = X / 100);
        }
        std::cout << std::endl;
        delete[] harr;
    }

    {
        unsigned int arr[maxsz];
        for (int i = 0; i < maxsz; ++i) {
            arr[i] = rng();
        }
        std::cout << "Stack array:\n  using index: ";
        {
            boost::timer::auto_cpu_timer t;
            for (int i = 0; i < maxsz; ++i) {
                arr[i] /= 100;
            }
        }
        std::cout << "  using std::for_each: ";
        {
            boost::timer::auto_cpu_timer t;
            std::for_each(arr, arr+maxsz, X = X / 100);
        }
        std::cout << "  using BOOST_FOREACH: ";
        {
            boost::timer::auto_cpu_timer t;
            BOOST_FOREACH(unsigned int& i, arr) {
                i /= 100;
            }
        }
        std::cout << std::endl;
    }

    {
        std::vector<unsigned int> vec(maxsz);
        for (int i = 0; i < maxsz; ++i) {
            vec.push_back(rng());
        }
        std::cout << "Vector:\n  using index: ";
        {
            boost::timer::auto_cpu_timer t;
            for (int i = 0; i < maxsz; ++i) {
                vec[i] /= 100;
            }
        }
        std::cout << "  using iterators: ";
        {
            boost::timer::auto_cpu_timer t;
            const std::vector<unsigned int>::iterator end = vec.end();
            for (std::vector<unsigned int>::iterator it = vec.begin(); it != end; ++it) {
                *it /= 100;
            }
        }
        std::cout << "  using std::for_each: ";
        {
            boost::timer::auto_cpu_timer t;
            std::for_each(vec.begin(), vec.end(), X = X / 100);
        }
        std::cout << "  using BOOST_FOREACH: ";
        {
            boost::timer::auto_cpu_timer t;
            BOOST_FOREACH(unsigned int& i, vec) {
                i /= 100;
            }
        }
        std::cout <<std::endl;
    }

    {
        std::list<unsigned int> list(maxsz);
        for (int i = 0; i < maxsz; ++i) {
            list.push_back(rng());
        }
        std::cout << "List:\n  using iterators: ";
        {
            boost::timer::auto_cpu_timer t;
            const std::list<unsigned int>::iterator end = list.end();
            for (std::list<unsigned int>::iterator it = list.begin(); it != end; ++it) {
                *it /= 100;
            }
        }
        std::cout << "  using std::for_each: ";
        {
            boost::timer::auto_cpu_timer t;
            std::for_each(list.begin(), list.end(), X = X / 100);
        }
        std::cout << "  using BOOST_FOREACH: ";
        {
            boost::timer::auto_cpu_timer t;
            BOOST_FOREACH(unsigned int& i, list) {
                i /= 100;
            }
        }
        std::cout << std::endl;
    }

    return 0;
}

Name: Anonymous 2012-04-13 1:07

Money are dopamine of the hive mind, while central banks, like IMF, are the dopamine emitters, which are controlled mostly by the Jews. Kill all the Jews and you'll kill the globalization Hydra. There should be no mercy to the Jews, because they are the neurons of our Enemy.

Remember: it's these dollars, the Jews print, make your life bad, so every Jew you kill will make your country better. Help your nation's economic and culture - kill Jewish internationalists.

Name: Anonymous 2012-04-13 2:40

>>24
>>25
Why is that wrong? On x86 gcc uses cmp to implement < and test-instruction to implement !=. What else do think causes the difference in speed if the only difference in generated loops is that one uses cmp and one uses test?

Name: Anonymous 2012-04-13 6:30

>>47
Check your facts.

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

Name: Anonymous 2012-04-13 12:29

>>49,50
Unless you're Notch.

Name: Anonymous 2012-04-13 14:03

>>35
Go scrub a midget.

Name: bampu pantsu 2012-05-29 4:20

bampu pantsu

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