473,379 Members | 1,216 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,379 software developers and data experts.

Functionoids vs Member Function Pointer?

I had a lot of research to see how function pointer works. Sometimes,
programmers choose switch keyword and function in each case block can
be called. Sometimes, they choose ordinary function pointer array
instead of switch.

They believe that ordinary function pointer is much faster than
switch. Think of one variable called selectFunction. selectFunction
variable can be the range of 0 through 1,000 or more.

Load selectFunction variable into a register. Load memory address of
ordinary function pointer array into a register. Both registers can
be added together to create indirection before one of these 1,000
functions can be called.

Member function pointer array is slower than ordinary function pointer
because first step is to load "this" pointer to locate memory address
of member function pointer array. It is likely that member function
pointer array needs to execute indirction twice before one of these
mmber function is called. Only ordinary function pionter needs one
indirection.

How can you find a way how member function pointer array can execute
one indirection instead of two indirection? A vtable has a memory
address where constructor function and deconstructor function are the
top. You can use selectFunction variable to be added to "this" pinter
in vtable memory address. Then, one of these 1,000 member functions
in the vtable can be called.

I did read C++ Lite FAQ and it does mention functionoids. They claim
that functionoids are much faster than member function poitner array.
How can it be possible to be true?

I would prefer to use static memory where member function pointer
array is stored at compile-time. I may do not need virtual functions
becaus I choose not to use dynamic binding.

Please advise which I should choose to use ordinary function pointer,
member function pointerarray, or functionoids.

Short example of code looks like this below.

int selectFunction = 0;

.....
do something ....
.....
selectFnction may be modified to have a value of 100.

.....
.....
FuncPtr [ selectFuntion ] ();
.....
100th member function is called...

Nephi
Sep 13 '08 #1
4 4356
Im************@hotmail.com wrote:
They believe that ordinary function pointer is much faster than
switch.
Looking up a branch location can be faster but it is often much slower.
Specifically, when there are a small number of choices it is faster to use
nested branches to fixed locations.
I did read C++ Lite FAQ and it does mention functionoids. They claim
that functionoids are much faster than member function poitner array.
How can it be possible to be true?
The C++ Lite FAQ is wrong. They are describing rudimentary precursors to the
first-class functions now found in all modern languages because they are
the only workarounds available in C++.

--
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
http://www.ffconsultancy.com/?u
Sep 13 '08 #2
On Sep 13, 5:24*am, Jon Harrop <j...@ffconsultancy.comwrote:
Immortal_Ne...@hotmail.com wrote:
They believe that ordinary function pointer is much faster than
switch.

Looking up a branch location can be faster but it is often much slower.
Specifically, when there are a small number of choices it is faster to use
nested branches to fixed locations.
I did read C++ Lite FAQ and it does mention functionoids. *They claim
that functionoids are much faster than member function poitner array.
How can it be possible to be true?

The C++ Lite FAQ is wrong. They are describing rudimentary precursors to the
first-class functions now found in all modern languages because they are
the only workarounds available in C++.

--
Dr Jon D Harrop, Flying Frog Consultancy Ltd.http://www.ffconsultancy.com/?u
Dr Jon D Harrop,

I agree that nested branch like switch keyword, but sometimes, un-
nested branch with 1,000 branches in "JMP Table" is faster. It
depends on the processor specification. If you force all functions to
be inlined in un-nested branch and nested branch, then C++ Compiler
will fail to compile because it exceeds branch limit. Not all C++
compilers have exceeded limit support.

Branch misprediction is a major problems. I don't know if modern
machines like x86 32 or x86 64 at 3 GHz may solve it.

I believe that function pointer with "CALL Table" may be preferred.
You have to make sure that stack frame is not needed. Then all sub-
functions can be inlined in each function inside function pointer
table.

My question is -- do you recommend me to use ordinary function pointer
or member function pointer? Ordinary function pointer is faster
because it uses one indirection. I can use class as long as I want,
but I will define member function pointers to be static.

What do you think?

Nephi
Sep 13 '08 #3
On 2008-09-13 04:50, Im************@hotmail.com wrote:
I had a lot of research to see how function pointer works. Sometimes,
programmers choose switch keyword and function in each case block can
be called. Sometimes, they choose ordinary function pointer array
instead of switch.

They believe that ordinary function pointer is much faster than
switch. Think of one variable called selectFunction. selectFunction
variable can be the range of 0 through 1,000 or more.

Load selectFunction variable into a register. Load memory address of
ordinary function pointer array into a register. Both registers can
be added together to create indirection before one of these 1,000
functions can be called.

Member function pointer array is slower than ordinary function pointer
because first step is to load "this" pointer to locate memory address
of member function pointer array. It is likely that member function
pointer array needs to execute indirction twice before one of these
mmber function is called. Only ordinary function pionter needs one
indirection.

How can you find a way how member function pointer array can execute
one indirection instead of two indirection? A vtable has a memory
address where constructor function and deconstructor function are the
top. You can use selectFunction variable to be added to "this" pinter
in vtable memory address. Then, one of these 1,000 member functions
in the vtable can be called.

I did read C++ Lite FAQ and it does mention functionoids. They claim
that functionoids are much faster than member function poitner array.
How can it be possible to be true?

I would prefer to use static memory where member function pointer
array is stored at compile-time. I may do not need virtual functions
becaus I choose not to use dynamic binding.

Please advise which I should choose to use ordinary function pointer,
member function pointerarray, or functionoids.
Have you carefully profiled your code and analysed the results and from
that decided that the calling mechanism is the best place to optimise
your application? Unless you have you should do so before doing
something that will further obscure your code.
Short example of code looks like this below.

int selectFunction = 0;

....
do something ....
....
selectFnction may be modified to have a value of 100.

....
....
FuncPtr [ selectFuntion ] ();
....
Can you reorganise the code to call the correct function instead of
setting selectFunction to a value? If possible that should be the most
performant way to do things since the compiler knows which function to
call and can do stuff like inlining etc.

Another possibility is to replace selectFunction with a function-pointer
and assign the correct function to it where where you today assigns a
value to selectFunction.

--
Erik Wikström
Sep 13 '08 #4
On Sep 13, 4:50 am, Immortal_Ne...@hotmail.com wrote:
I had a lot of research to see how function pointer works.
Sometimes, programmers choose switch keyword and function in
each case block can be called. Sometimes, they choose
ordinary function pointer array instead of switch.
They believe that ordinary function pointer is much faster
than switch.
Who'd be stupid enough to believe something like that. If an
indirect jump is the fastest method on a given machine, that's
what the compiler will generate for a switch. In the distant
past (before extensive pipelining), it was usual for compilers
to break switches down into different categories: a dense switch
would be implemented by means of bounds checking and a jump
table, a scattered switch would use a lookup table to determine
the index, and then the jump table. And small switches would be
implemented as a hardwired binary search, using code more or
less equivalent to nested if's. The cut-off point where the use
of the jump table became a win depends on the processor; on
older machines, without extensive pipelining, it's often as
little as four cases. On a modern machine, with extensive
pipelining, branch prediction (which is often invalidated by an
indirect call), etc., the cut-off is often a couple of hundred,
or even a couple of thousand; some compilers probably don't even
bother with even considering the jump table, since the cut-off
is high enough that it will never be reached in reasonable code.
(Which is debattable: in hand written code, of course, you
probably will never have more than a couple of dozen cases, but
in machine generated code, switches with several hundred cases
or more are not unreasonable.)

At any rate, it's the compiler's problem to get this right, not
yours.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Sep 14 '08 #5

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

Similar topics

5
by: Newsgroup - Ann | last post by:
Gurus, I have the following implementation of a member function: class A { // ... virtual double func(double v); void caller(int i, int j, double (* callee)(double)); void foo() {caller(1,...
3
by: Roy Yao | last post by:
Hello, I need to pass a pointer to a callback function to the lower level modules. But the function is thought to be a virtual member one. How can I get the real address of the virtual...
2
by: joe | last post by:
hi, after reading some articles and faq, i want to clarify myself what's correct(conform to standard) and what's not? or what should be correct but it isn't simply because compilers don't...
37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
7
by: jon wayne | last post by:
Hi I'm a little confused here about the lifetime of a static pointer to member function, Say, I declare,define & initialize a static ptr to mem function in the header file of a class(the class...
13
by: JohnQ | last post by:
The implementation of classes with virtual functions is conceptually easy to understand: they use vtables. Which begs the question about POD structs: how are they associated with their member...
7
by: WaterWalk | last post by:
Hello. I thought I understood member function pointers, but in fact I don't. Consider the following example: class Base { public: virtual ~Base() {} }; class Derived : public Base {
5
by: Tim Frink | last post by:
Hi, I'm experimenting with function pointers and found two questions. Let's assume this code: 1 #include <iostream> 2 class A; 3 4 //////////////////////////////////////////// 5 class B
7
by: ghulands | last post by:
I am having trouble implementing some function pointer stuff in c++ An object can register itself for many events void addEventListener(CFObject *target, CFEventHandler callback, uint8_t...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.