Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this valid usage of inline functions?

Let's say I have this code (don't mind the fact that SecondsToMinutes and MinutesToHours are carbon copies of each other)

inline float SecondsToMinutes(float seconds)
{
    return seconds / 60.0;
}
inline float MinutesToHours(float minutes)
{
    return minutes / 60.0;
}
inline float HoursToDays(float minutes)
{
    return minutes / 24.0;
}
inline float SeconndsToHours(float seconds)
{
    return MinutesToHours(SecondsToMinutes(seconds));
}
inline float MinutesToDays(float minutes)
{
    return HoursToDays(MinutesToHours(minutes));
}
inline float SeconndsDays(float seconds)
{
    return MinutesToDays(SecondsToMinutes(seconds));
}

Is this valid usage of inline? Does it make sense? Is this good practice? After all, if I recall correctly, inline means that function calls are replaced by function bodies, so

return MinutesToDays(SecondsToMinutes(seconds))

should be equivalent to

return seconds / 60.0 / 60.0 / 24.0

Right?

Or is it better to just use macros for this?

#define EXCHANGE_SEC_MIN(x) (x / 60.0)
#define EXCHANGE_MIN_H(x) (x / 60.0)
#define EXCHANGE_H_D(x) (x / 24.0)
#define EXCHANGE_SEC_H(x) (EXCHANGE_MIN_H(EXCHANGE_SEC_MIN(x)))
#define EXCHANGE_MIN_D(x) (EXCHANGE_H_D(EXCHANGE_MIN_H(x)))
#define EXCHANGE_SEC_D(x) (EXCHANGE_MIN_D(EXCHANGE_SEC_MIN(x)))

Which one is the better practice? Or neither is? I'd like others cents on this.

like image 379
Stephanus Tavilrond Avatar asked Oct 27 '16 08:10

Stephanus Tavilrond


People also ask

When inline functions are used?

Inline functions are commonly used when the function definitions are small, and the functions are called several times in a program. Using inline functions saves time to transfer the control of the program from the calling function to the definition of the called function.

What is not valid for inline function?

5) Inline functions may not be useful for many embedded systems. Because in embedded systems code size is more important than speed. 6) Inline functions might cause thrashing because inlining might increase size of the binary executable file. Thrashing in memory causes performance of computer to degrade.

What is use of inline function in C++?

C++ inline function is powerful concept that is commonly used with classes. If a function is inline, the compiler places a copy of the code of that function at each point where the function is called at compile time.

Can inline functions be used to improve performance?

inline functions might make it faster: As shown above, procedural integration might remove a bunch of unnecessary instructions, which might make things run faster. inline functions might make it slower: Too much inlining might cause code bloat, which might cause “thrashing” on demand-paged virtual-memory systems.


3 Answers

Is this valid usage of inline? Does it make sense? Is this good practice? After all, if I recall correctly, inline means that function calls are replaced by function bodies, so

Sure it is. You make it more legible which is always good.

return seconds / 60.0 / 60.0 / 24.0

Yes that's how it turns out. Or should. inline is just a hint, the compile just might decide otherwise. But for such one liner, the compiler will inline it.

Macros? Why? If it can be done with functions, why use macros?

like image 139
themagicalyang Avatar answered Oct 16 '22 02:10

themagicalyang


Is this valid usage of inline? Does it make sense?

Well, yeah but no.

It doesn't hurt anything at this point but doesn't do what you think it does either.

In an excellent post about inline deft_code correctly says :

It is said that inline hints to the compiler that you think the function should be inlined. That may have been true in 1998, but a decade later the compiler needs no such hints. Not to mention humans are usually wrong when it comes to optimizing code, so most compilers flat out ignore the 'hint'.

So, it doesn't hurt anyone if you do it but the chance your compiler will listen to your hint is practically 0. If it sees fit to inline the code, it will do so itself.

inline nowadays is used mostly for the linker since it allows multiple definitions in multiple compilation units.

If you want to make sure your code is as fast as possible and you have access to C++11 you should use constexpr:

constexpr float SecondsToMinutes(float seconds)
{
    return seconds / 60.0;
}
//etc..
like image 21
Hatted Rooster Avatar answered Oct 16 '22 03:10

Hatted Rooster


inline does not mean that function calls are replaced by function bodies. At least it hasn't meant that for the past fifteen-something years: optimizers are now way beyond taking orders from the developer, and will perform inlining whether or not you specified inline.

inline actually means "this function may be defined multiple times, and the linker should sort it out and keep at most a single definition at the end. I'm responsible for ensuring that all definitions are identical".

If you really, really want to enforce inlining (the actual insertion of the function's body inside the caller) yourself, you'll have to use compiler-specific extensions such as __attribute__((always_inline)).

You typically need inline when the functions are defined in a header, since this header will eventually be included in several translation units, thus the definitions will be duplicated. As such, assuming your code is inside a header, this is a good use of inline.

like image 3
Quentin Avatar answered Oct 16 '22 03:10

Quentin