0

i thought to replace std::vector with std::array in my program, so i went on testing:

template<typename T> class A{
    public:
    void sub_run(T w){
        w[0] = 0.5;
        w[1] = 1.5;
        w[2] = 2.5;
        w[3] = 0.0;

        for (int i = 0; i < 100000000; i++)
        {
            w[0] = i *0.5;
            w[1] = i *1.5;
            w[2] = i *2.5;

            w[3] = w[0] + w[1]*w[2];
        }
    }
};

int main(){

    // Vectors    
    /*
    A<vector<double> > inst_a;
    vector<double> w(4);
    inst_a.sub_run(w);
    */
    // 1.71 sec

    // C-array
    /*
    A<double *> inst_a;
    double w[4];
    inst_a.sub_run(w);
    */
    // 1.03 sec

    // std::array

    A<array<double,4> > inst_a;
    array<double,4> w;
    inst_a.sub_run(w);

    // 3.31 sec

    return 0;
 }

Well, the output is odd enough - the std::array is slower than C-array in 3 times and even slower than vector. No optimization was applied, the only flag is -std=c++11. What could be wrong?

4
  • 5
    No optimization was applied, the only flag is -std=c++11. What could be wrong?. Did you check when optimization was applied? Unoptimized C++ is not trying to beat "C-like" performance in any way shape or form, so if you're going to be talking about performance of the final product you must allow the C++ compiler leeway to optimize...because otherwise, it's going to leave in a lot of things that assist in debugging/etc. Not a fair comparison. Commented Oct 20, 2014 at 11:49
  • 5
    "No optimization was applied" is exactly what's wrong. Commented Oct 20, 2014 at 11:50
  • ""No optimization was applied" is exactly what's wrong." - Well this is somehow unapparent, since the cppreference.com just says "...combines the performance and accessibility of a C-style array with the benefits of a standard container, such as knowing its own size, supporting assignment, random access iterators, etc." Commented Oct 20, 2014 at 11:59
  • 1
    @stkubr it may be unapparent from the documentation, but it is a piece of general knowledge. They won't repeat this every time. That would be akin to warning someone to "not forget the integration constant" each time he performs an integration. Commented Oct 20, 2014 at 12:09

1 Answer 1

3

From the GCC documentation about Optimization:

Without any optimization option, the compiler's goal is to reduce the cost of compilation and to make debugging produce the expected results. Statements are independent: if you stop the program with a breakpoint between statements, you can then assign a new value to any variable or change the program counter to any other statement in the function and get exactly the results you expect from the source code.

Without the -O option, GCC does not care about performance. It cares about debugging using gdb or other similar tool. To actually compile programs that runs as fast as possible you should use the -O options. For example, -O2.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.