Login  Register

Re: 2D Arrays?

Posted by Luigi Ballabio on May 12, 2008; 4:35pm
URL: http://quantlib.414.s1.nabble.com/2D-Arrays-tp5941p5946.html

On Mon, 2008-05-12 at 08:49 -0700, caffeine wrote:
> I see that I was too slow on the "it's too slow" response.   :-)
>
> Do you have any proof that a compiler will inline std::vector calls into raw
> C array accesses?  If it were true, I would be ecstatic, but I don't believe
> it for a second.

Not "proof" as such---results will probably depend on context. But on my
machine, compiling with g++ 4.2.3 and with full optimization turned on
(i.e., -O3), the two following programs run in about the same time. It
seems pretty good evidence that, at least in this case, operator[] is
inlined.

However, if allocation is moved inside the outer loop, the vector
program runs slower. Either vector creation is slower, or adding the
allocation inside the loop changes the inlining policy---your guess is
as good as mine.

Luigi


-------------------------
test1.cpp
-------------------------
#include <vector>

int main() {
    std::vector<double> v(10000);
    for (int j=0; j<10000; ++j) {
        for (int i=0; i<10000; ++i)
            v[i] = 42.0;
        double x;
        for (int i=0; i<10000; ++i)
            x = v[i];
    }
    return 0;
}

-------------------------
test2.cpp
-------------------------
int main() {
    double* v = new double[10000];
    for (int j=0; j<10000; ++j) {
        for (int i=0; i<10000; ++i)
            v[i] = 42.0;
        double x;
        for (int i=0; i<10000; ++i)
            x = v[i];
    }
    delete[] v;
    return 0;
}



--

There are no rules of architecture for a castle in the clouds.
-- Gilbert K. Chesterton



-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference
Don't miss this year's exciting event. There's still time to save $100.
Use priority code J8TL2D2.
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users