Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Performance of static member function versus functor

Reply
Thread Tools

Performance of static member function versus functor

 
 
chsalvia@gmail.com
Guest
Posts: n/a
 
      08-14-2007
In generic programming, static member functions and functors seem to
be very useful. I've noticed that some libraries take the approach of
using templated static member functions to provide generic
functionality, while others use functors. Take for example the
std::string char_traits class. Here, a static member "eq" is defined
to serve as a generic binary comparison function.

My implementation defines it as;

static bool eq(const char_type& c1, const char_type& c2) { return c1
== c2; }

An alternative would be to define eq as a functor, like:

struct eq {
bool operator() (const char_type c1, const char_type c2) { return
c1 == c2; }
}

The major difference, of course, is that the second case requires you
to instantiate the object. I originally thought that there wasn't
much difference, in terms of the actual assembly code generated,
between these two options. But benchmarking tests I've conduct show
that using an instantiated object is almost always faster than using a
static member function. I would have actually thought the opposite
would be true, because with an instantiated object you have the slight
additional overhead of constructing the object, whereas a static
member function is just like calling a global function.

But it seems that instantiated objects outperform static member
functions by a significant amount. My question is, is there some
inherent reason for this, or is this likely to be different from
compiler to compiler? Perhaps static member functions can't be
inlined.

 
Reply With Quote
 
 
 
 
BobR
Guest
Posts: n/a
 
      08-14-2007

<> wrote in message...
>
> An alternative would be to define eq as a functor, like:
>
> struct eq {
> bool operator() (const char_type c1, const char_type c2) { return
> c1 == c2; }
> }
>
> The major difference, of course, is that the second case requires you
> to ** instantiate the object. **


See if this will compile for you.

// #includes here <cstdlib>, <iostream>, <vector>, <algorithm>,etc.

struct MyRand{
int operator()(){ return std::rand() % 100;}
};

int main(){
std::vector<int> vLf;
std::generate_n( std::back_inserter( vLf ), 1000, MyRand() );
// or is that what you meant by 'instantiate'

std::vector<int>::const_iterator it =
std::max_element(vLf.begin(), vLf.end());
std::cout<<"vector<int> vLf.size()"<<vLf.size()
<<" The largest element is "<<*it<<std::endl;
return 0;
} // main()

Maybe I mis-understood.
--
Bob R
POVrookie


 
Reply With Quote
 
 
 
 
Frank Birbacher
Guest
Posts: n/a
 
      08-14-2007
Hi!

schrieb:
> But it seems that instantiated objects outperform static member
> functions by a significant amount. My question is, is there some
> inherent reason for this, or is this likely to be different from
> compiler to compiler? Perhaps static member functions can't be
> inlined.


When passing a (static) function to i.e. std::for_each, you get an
instantiation of for_each, which takes a function pointer as an
argument. So each element is processed by calling a function through a
pointer.

When passing a class instance (a functor) to std::for_each, you get an
instantiation for just that specific class. Each element is process by
an inlined operator().

Usually the compiler does not optimize the (const) function pointer. It
might do so, however. That's why small functors outperform the static
function.

Frank
 
Reply With Quote
 
joe
Guest
Posts: n/a
 
      08-15-2007
On Aug 14, 1:32 pm, chsal...@gmail.com wrote:

> But it seems that instantiated objects outperform static member
> functions by a significant amount. My question is, is there some
> inherent reason for this, or is this likely to be different from
> compiler to compiler? Perhaps static member functions can't be
> inlined.


Usually small functors like that can be inlined and therefore generate
more efficient code. If your functor had state and your function used
it for something fairly complicated, you might not see such an
improvement, but a vast majority of the time, you will.

joe

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Mozilla versus IE versus Opera versus Safari Peter Potamus the Purple Hippo Firefox 0 05-08-2008 12:56 PM
Can a static member function access non-static member? dolphin C++ 3 12-05-2007 12:39 PM
What is the correct grammar to make a function call by using static member data which is a pointer to a ordinary class member function? zaeminkr@gmail.com C++ 3 07-06-2007 12:50 PM
How do you call a regular member function from a static member function? aling C++ 6 10-30-2005 04:38 AM
performance of static member function vs. instance member function 0to60 C++ 4 11-21-2003 05:25 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57