7
Directives and macros influence program performance?
7
Directives and macros influence program performance?
10
They in themselves do not influence anything in performance. The specific use of some technique with them may even bring some result.
At first there was only the preprocessor to accomplish a few things, but today compilers have constant values in various ways, functions inline, and much of what was needed no longer needs to be used.
Often the feature was used to get more performance, but the current compilers allow to do the same by the language itself, which gives more allowance to do properly.
Today the recommendation is to use this as little as possible. This is a resource seen today more as a disadvantage. Still necessary because of deficiencies in the language itself.
It’s very easy to make mistakes using directives. There are a number of rules that need to be followed in order for everything to work. Even though the programmer dominates all - and there are many - it’s still not easy to always get it right. The compiler does not help because they are not part of the language. It’s just a decision to exchange or use text without understanding what’s in the code, without scope, without context. It’s very easy to imagine you’re doing one thing and actually doing another. The mechanism is very rudimentary.
An obvious example of a problem occurs in macros:
#define MAX(x,y) (x) > (y) ? (x) : (y)
It is well done, followed the recommended rules (I won’t even make the naive example that causes more error). There uses so:
int x = MAX(y++, 1);
That is so transformed:
int x = (y++) > (1) ? (y++) : (1);
I put in the Github for future reference.
Have you noticed that it will not produce the result you want? It is full of traps of this type, even in more innocent situations. And there are many situations that are less innocent than these.
Has tricks to avoid problems, but when you need to fix tricks, in general the mechanism is wrong.
I won’t even mention the difficulty of thresh code like this.
Some directives serve to facilitate the construction and organization of the code. Others help in complex constructions allowing code flexibility and reuse.
That’s usually it. Some specific directives can have their own specific advantages. Of course #include
and #ifdef
are still more useful, but they do not influence performance directly. What you do with them can influence performance since you can decide for a better alternative to a certain circumstance. It may no longer include a required code in a given build, so less code helps keep the cache only with what is needed and also avoids a decision at runtime.
Some cases it’s better to use anyway.
I do not give a more complete answer because it is very broad. Each directive has its own peculiarity. More specific questions are needed.
In C++ the use is less advisable even because it has more mechanism that can replace this.
"Today the recommendation is to use this as little as possible." Why? - Look, I’m not questioning the conclusions (I agree with them), but I think the answer could be improved if the proper explanations (perhaps even with some examples) were also given(the). :)
Thanks! So let me know I’ll be back to leave my +1. :)
It got a lot better. Thanks! + 1. And I don’t think there’s a book chapter. The question might be innocent, but it generated important content (imho).
There is! Today I came across a problem of using macros when porting a Linux code to Windows (and need to include the infamous windows.h
): http://stackoverflow.com/q/5004858/2896619. I naturally remembered this post. :)
Browser other questions tagged c c++
You are not signed in. Login or sign up in order to post.
Compilers optimize functions, do not think it is necessary to create macros, particularly prefer functions even, I find easier to maintain. The use of directives can be useful when you need to compile the same code for several different platforms, an example is the function
fflush
what works on windows and the__fpurge
unique to linux systems. You can specify a directive for them for the compiler to choose the platform, they serve to clean the buffer, there are other uses as well.– gato
Did the answer solve the problem? Do you think you can accept one of them? See [tour] how to do this. You’d be helping the community by identifying the best solution. You can only accept one of them, but you can vote for anything on the entire site.
– Maniero