discuss-gnuradio
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Discuss-gnuradio] ANCI-C vs Gnuradio/C++ speeeed


From: al davis
Subject: Re: [Discuss-gnuradio] ANCI-C vs Gnuradio/C++ speeeed
Date: Fri, 12 May 2006 20:43:29 -0400
User-agent: KMail/1.9.1

Responding to my own post ...

Here's why the difference is so important:

On Friday 12 May 2006 18:34, al davis wrote:
> You are returning a vector by value:
> from fsm.h:
> //========
>   std::vector<int> NS () const { return d_NS; }
>   std::vector<int> OS () const { return d_OS; }
>   std::vector<int> PS () const { return d_PS; }
>   std::vector<int> PI () const { return d_PI; }
> //========

Returning by value means that the copy constructor is invoked to 
make the copy.  It is in the .cc file, forcing it to be 
explicitly called, complete with stack frame overhead, but this 
is not what is important.  Copying a vector is done by a loop, 
so it is an O(n) operation.

To return by reference, add a &....
std::vector<int> & PI () const { return d_PI; }

Now it won't make a copy, but the vector is modifyable.  You 
might want:
const std::vector<int> & PI () const { return d_PI; }
to protect it.

A double nested loop takes time O(n^2).  By putting O(n) inside, 
it is now O(n^3).  For n large, you will certainly see a 
difference.


C++ optimization has different rules than C.  In some ways it is 
a lower level language.

For example, take the statement:
        int i;
It has different meanings
In C:  There exists an object of type int called i.
In C++:  Construct an object of type int called i now.

The language is full of these subtleties.  They make a 
difference.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]