On Sun, May 14, 2006 at 08:54:19AM -0700, Eric Blossom wrote:
> On Sun, May 14, 2006 at 09:40:16AM -0400, Achilleas Anastasopoulos wrote:
> >
> > I run the following simple test, compiled with
> > g++ test.cc -o test
> > and I got the following results.
> > I see a 4-fold speed reduction using STL.
> > What am I doing wrong?
>
> You're not using the "standard level of optimization" -O2
If you look at the object code it's pretty clear why the STL version
is so much slower. Given this code (which I assume is okay; I'm not
normally a C++ programmer) and g++ 3.4.2:
#include <vector>
void foo(int const n,int const m,std::vector<int> &x)
{
for(int i = 0;i < n;i++)
for(int j = 0;j < m;j++)
x[j] = i / (j + 1);
}
void foo(int const n,int const m,int x[])
{
for(int i = 0;i < n;i++)
for(int j = 0;j < m;j++)
x[j] = i / (j + 1);
}
Without any -O option, the template version of foo() actually calls
another function. That function calls three other functions, and one
of them calls yet another function. I think this happens on every
loop iteration, presumably as part of accessing x[j]. These extra 5
functions are generated as a side effect of the templates. The plain
array version of foo(), on the other hand, doesn't need to call
anything and just accesses the memory directly.
When you add -O2, the extra functions go away and both versions of
foo() end up producing nearly identical object code.
As an extreme example of how a small amount of STL can produce lots of
little functions, try this:
#include <map>
#include <string>
using namespace std;
int main() { map<string,string> x; x["y"] = "z"; return 0; }
With g++ 3.4.2 and no -O option, that produces 63 functions. With
-O2, it drops down to 6 functions and about 1/3 less assembly code.
When I encountered this example in a co-worker's code several years
ago the situation was much worse: their version of gcc was generating
around 100 extra functions, and some of them had names so long that
the system's assembler was actually refusing to accept gcc's output.
-Dave Dodge
_______________________________________________
Discuss-gnuradio mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/discuss-gnuradio