473,320 Members | 2,161 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Performance of static member function versus functor

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.

Aug 14 '07 #1
3 4415

<ch******@gmail.comwrote 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<intvLf;
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<intvLf.size()"<<vLf.size()
<<" The largest element is "<<*it<<std::endl;
return 0;
} // main()

Maybe I mis-understood.
--
Bob R
POVrookie
Aug 14 '07 #2
Hi!

ch******@gmail.com 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
Aug 14 '07 #3
joe
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

Aug 15 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Alex Vinokur | last post by:
=================================== ------------- Sorting ------------- Comparative performance measurement =================================== Testsuite : Comparing Function Objects to...
2
by: Andreas Mueller | last post by:
Hi All, the following Situation is going through my mind: class Xox { public Xox(){} public object Foo(){ return null; } }
13
by: Bern McCarty | last post by:
I have run an experiment to try to learn some things about floating point performance in managed C++. I am using Visual Studio 2003. I was hoping to get a feel for whether or not it would make...
4
by: Agoston Bejo | last post by:
Hi, I would like to do something like this: struct A { int f(float f); }; .... int g(int(*f1)(float)) { return f1(6.5); }
2
by: Lionel B | last post by:
I have a function which takes a functor argument. I which to call it for a functor which is actually a class member; this works fine, using the mem_fun_ref and bind1st functions (see listing 1...
2
by: aaragon | last post by:
Hi everyone, Can someone point me out why I can't declare the operator() of a functor as static? The reason behind this is that I want to be able to call to the function without instantiating...
12
by: mathieu | last post by:
Hi, Consider the following (*). Is there a way to rewrite it so that it remains convenient (N is being recomputed when array v is modified) *and* compiles :) Thanks, -Mathieu (*)
2
by: mathieu | last post by:
Hi there, I was recently help for an issue with the following code: template <typename T, unsigned int N> struct Functor { T values; };
62
by: Generic Usenet Account | last post by:
A lot of research has been done to prove that the contention that C code is more efficient and more compact than equivalent C++ code is a myth. My posting pertains to a slightly different aspect...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.