For an assignment, we're supposed to write two methods for handling outputs. One for outputting strings, and one for integers.
Basically we have two methods calling another method:
void TheClass::displayString(string str){ cout << str; }
void TheClass::displayNumber(int n) { cout << n; }
Will inline speed things up by saving some overhead by not calling yet another function or will it create more in regards to namespaces and such for cout开发者_如何学JAVA?
inline void TheClass::displayString(string str) { cout << str; }
inline void TheClass::displayNumber(int n) { cout << n; }
Namespaces don't have anything to do with it.
You probably won't see any benefit here for one simple reason: the function is so small that I'd expect the compiler to be inlining it anyway.
Remember, the keyword inline
is a hint not a directive, and usually you can just let your toolchain decide when to inline. It's good at that.
I agree with Tomalak, inline is only a hint, and you hardly get any benefit of inlineing a function. Today's processors are so fast that one more function call does not matter at all.
I used inline
functions in opengl programming. OpenGL programs are infinite loops. To make the loop run as fast as possible you have to program very efficiently, so it will remain responsive to the user and will produce image smoothly. I believed declaring some functions inline
would make the program run faster and smoother.
After all, don't expect huge performance upgrade from this technique, only 1-2 % less run time in extreme cases probably.
With those two functions, you probably won't see any noteworthy difference whether the compiler inlines the functions or not.
The reason is, that the stream insertion (<<
) is rather slow. Much slower than one simple (non-inlined) function call.
And, as others have already pointed out, you should really get accustomed to passing strings by reference-to-const (and any other objects that are not really really cheap to copy).
There are some situations where it's preferable to pass by value, even (or especially) if the copy is expensive (e.g. when implementing operator =
with copy & swap). But those are rare, and as long as you can't decide what's appropriate on your own, you're far better off always passing by reference-to-const then always passing by value.
The inline
keyword has very little to do with inlining of generated code. The inline
keyword indicates that the function is being declared inline (that is, defined at its declaration) and so could have duplicate definitions in multiple compilation units. It is needed when you want to define a function in a header, as this function definition could be included in multiple compilation units, and without the inline
keyword this would be a violation of the One Definition Rule.
If you are concerned about performance then profile your code while it is running, rather than asking people who have no idea what your code is doing to guess where your bottlenecks are.
If you aren't spending a lot of time producing output, then this won't matter anyway. If you have profiled your program and determined that you are in the unusual situation that your program is spending most of its time producing output, then you can start considering how to make this code faster. You probably won't make it faster by adding the inline keyword, since your compiler is probably already inlining the function anyway, and if it isn't it probably won't do it even if you ask it to (though it might).
An easy first step to actually make your code faster would be to make your string parameter be a const string& instead. That way you won't copy the string. A more complicated second step would be to use the C output API instead of the C++ streams. The C API has been much faster in my experience, and that is backed up here and here. However I don't recommend moving to the C API unless you are sure you actually really need the performance based on profiling.
One way to optimize to have const-reference:
void TheClass::displayString(cosnt string& str){ cout << str; }
Softnux, why don't you simply create a benchmark? Create a for
loop with one million repetitions and see how much time it takes for each of the options. Then perform the benchmark separately for Debug and Release builds. Modern compilers should figure out when inlining or loop unrolling are the better option.
Edit: try with a string stream. Outputting to a console is going to be the bottleneck here, not the function call itself.
精彩评论