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

Macros and Executionspeed

Hello!

I have read, inline functions are, exactly like macros by the
Preprocessor be implemented directly and therefore offer, due to a
smaller Overheads a certain speed advantage. Did I understand it so
correctly? Why is code implemented by the Preprocessor faster, and/or
why this does not produce so much Overhead?

I hope anyone can help me.

PS.:
I have read this in Bruce Eckel's "Thinking in C++ (9. Inline-Functions)"

So long, Ulf
Sep 14 '05 #1
8 1405
Ulf Kadner wrote:

Hello!

I have read, inline functions are, exactly like macros by the
Preprocessor be implemented directly and therefore offer, due to a
smaller Overheads a certain speed advantage. Did I understand it so
correctly? Why is code implemented by the Preprocessor faster, and/or
why this does not produce so much Overhead?


Think of the following:
You have a function:

double twice( double Argument )
{
return 2 * Argument;
}

In order to call that function, eg. like this

int main()
{
double x;
x = twice( 5.0 );
}

The code has to actually call that function. That means that information
must be stored somewhere from where the call was made, in order to give
the return a chance to find its way back. Also: A new variable 'Argument'
must be created, intialized with the value 5 and destroyed when the function
actually returns. That brings us to the return value. Somehow the return value
(the result of 2 * Argument) must find its way back to the caller. All of this
costs time.

Now the very same thing, but without a function call:

int main()
{
double x;
x = 2 * 10.0;
}

No call is made, no variable needs to be created, no return value must find it's
way back, no return address must be stored somewhere. It is easy to see, that this
is usually way faster then the first version.

Now say, that you don't want the actual computation in main. What options are there.
The old, C way, was to use a macro:

#define TWICE(X) ( 2 * (X) )
int main()
{
double x;
x = TWICE(5.0);
}

when the Preprocessor runs, it substitutes the macro with the text its stands for.
Thus after the preprocessor has done its work, the following is feed to the actual
compiler:

int main()
{
double x;
x = ( 2 * (5.0) );
}

and heureka (!), we are back to the fast version.

Another method would be to use an inline function. This is somewhat similar
in that the compiler (not the preprocessor) is able to replace the actual
call of the function with the function body:

inline double twice( double Argument )
{
return 2 * Argument;
}

int main()
{
double x;
x = twice( 5.0 );
}

Now if the compilers grants your inline request, it internally will modify
your program as if you had written:

int main()
{
double x;

{
double Argument = 5.0;
x = 2 * Argument;
}
}

a few optimizing steps later, this will reduce to:

int main()
{
double x;
x = 2 * 5.0;
}
HTH

--
Karl Heinz Buchegger
kb******@gascad.at
Sep 14 '05 #2
Karl Heinz Buchegger <kb******@gascad.at> writes:
...
Another method would be to use an inline function. This is somewhat similar
in that the compiler (not the preprocessor) is able to replace the actual
call of the function with the function body:


Except that there are no in-out register arguments for C++ functions, but
they are possible with macros. Not that it matters too much in the most
cases though.

ImRe
Sep 14 '05 #3
Karl Heinz Buchegger wrote:
Another method would be to use an inline function. This is somewhat similar
in that the compiler (not the preprocessor) is able to replace the actual
call of the function with the function body:


Servus Karl Heinz!

Thus I understood it wrongly! I thought always, the pre-processor also
runs the Inline-functions.

Thanks for your very usefull, detailed description!

so long, Ulf
Sep 14 '05 #4

Imre Palik schreef:
Karl Heinz Buchegger <kb******@gascad.at> writes:
...
Another method would be to use an inline function. This is somewhat similar
in that the compiler (not the preprocessor) is able to replace the actual
call of the function with the function body:


Except that there are no in-out register arguments for C++ functions, but
they are possible with macros. Not that it matters too much in the most
cases though.


Wrong. With inline functions, both in and out arguments can be passed
in registers. The reason is that the entire function implementation
is visible to the compiler, so it can allocate registers for any
variable (including arguments).

HTH,
Michiel Salters

Sep 14 '05 #5
"msalters" <Mi*************@logicacmg.com> writes:
Imre Palik schreef:
Karl Heinz Buchegger <kb******@gascad.at> writes:
...
Another method would be to use an inline function. This is somewhat similar
in that the compiler (not the preprocessor) is able to replace the actual
call of the function with the function body:


Except that there are no in-out register arguments for C++ functions, but
they are possible with macros. Not that it matters too much in the most
cases though.


Wrong. With inline functions, both in and out arguments can be passed
in registers.


I had something like this in mind

#define FOOBAR(a, b) a++, b++

vs.

inline void foobar(int &a, int &b) {a++; b++}

I know that in and out parameters can be pased in registers. But in c++,
if a function parameter is both in and out than it is a reference. AFAIK
passing a register parameter as reference tends to make the compiler
disregard the register hint.

Best regards

ImRe
Sep 20 '05 #6

Imre Palik skrev:

I had something like this in mind

#define FOOBAR(a, b) a++, b++

vs.

inline void foobar(int &a, int &b) {a++; b++}

I know that in and out parameters can be pased in registers. But in c++,
if a function parameter is both in and out than it is a reference. AFAIK
passing a register parameter as reference tends to make the compiler
disregard the register hint.
What gave you that idea? What compiler will create not optimal code for
your foobar above?

/Peter
Best regards

ImRe


Sep 20 '05 #7

Imre Palik wrote:
"msalters" <Mi*************@logicacmg.com> writes:
Imre Palik wrote:
.....
Except that there are no in-out register arguments for C++ functions, but
they are possible with macros. Not that it matters too much in the most
cases though.


Wrong. With inline functions, both in and out arguments can be passed
in registers.


I had something like this in mind

#define FOOBAR(a, b) a++, b++

vs.

inline void foobar(int &a, int &b) {a++; b++}

I know that in and out parameters can be pased in registers. But in c++,
if a function parameter is both in and out than it is a reference. AFAIK
passing a register parameter as reference tends to make the compiler
disregard the register hint.


AFAIK (and that is confirmed by multiple sources) any modern compiler
will disregard the "register" keyword anyway. They'll look at the
actual variables instead. Futrhermore, "passing a parameter" doesn't
make a lot of sense when inlining. Modern compilers simply alias
reference arguments. I.e. when inlinig foobar() above, a is made an
alias of the corresponding argument of the outer scope. This can
actually happen before the register allocation stage.

HTH,
Michiel Salters

Sep 21 '05 #8
On 21 Sep 2005 05:41:11 -0700, "msalters" <Mi*************@logicacmg.com>
wrote:
I know that in and out parameters can be pased in registers. But in c++,
if a function parameter is both in and out than it is a reference. AFAIK
passing a register parameter as reference tends to make the compiler
disregard the register hint.


AFAIK (and that is confirmed by multiple sources) any modern compiler
will disregard the "register" keyword anyway.


Speak for yourself ;-) I use a modern embedded C++ compiler that respects the
"register" keyword to the point of de-optimizing the function call (i.e.
stuffing parameters on the stack instead of using registers) if you declare
enough variables with the register keyword.
Sep 21 '05 #9

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

Similar topics

21
by: Chris Reedy | last post by:
For everyone - Apologies for the length of this message. If you don't want to look at the long example, you can skip to the end of the message. And for the Python gurus among you, if you can...
699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
16
by: mike420 | last post by:
Tayss wrote: > > app = wxPySimpleApp() > frame = MainWindow(None, -1, "A window") > frame.Show(True) > app.MainLoop() > Why do you need a macro for that? Why don't you just write
37
by: michele.simionato | last post by:
Paul Rubin wrote: > How about macros? Some pretty horrible things have been done in C > programs with the C preprocessor. But there's a movememnt afloat to > add hygienic macros to Python. Got any...
8
by: Michael Winter | last post by:
In a recent post ("About C error" by Victor, 21 Sep 2003), comments were made about the poster's use of macros. What I would like to know is why they are considered bad? I'm not referring to...
3
by: Stephen Sprunk | last post by:
On a project I'm working on, I ran across the following macros: /* assume s is struct stream *, s->p is char, v is unit16_t or uint32_t */ #define in_uint16_le(s,v) { v = *((s)->p++); v +=...
47
by: Emil | last post by:
Is there any hope that new versions of PHP will support macros similar to C or C++? I've searched manual and didn't find anything except define directive, but it can be used to define constant...
11
by: San | last post by:
hi there, I am new to c++ and tryig to learn the basics of the c++ concepts. While I was reading the templates, I realize that the templates are a syntax that the compilar expands pased upon the...
33
by: Robert Seacord | last post by:
When writing C99 code is a reasonable recommendation to use inline functions instead of macros? What sort of things is it still reasonable to do using macros? For example, is it reasonable to...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.