Internal member statements vs C ++ built-in operators

If I have two structures:

struct A
{
    float x, y;
    inline A operator*(A b) 
    {
        A out;
        out.x = x * b.x;
        out.y = y * b.y;
        return out;
    } 
}

And equivalent structure

struct B
{
    float x, y;
}

inline B operator*(B a, B b) 
{
    B out;
    out.x = a.x * b.x;
    out.y = a.y * b.y;
    return out;
} 

Do you know why the B * operator does not compile differently or runs slower or faster than the A operator (the actual actions that are performed inside the functions must be irrelevant)?

What do I mean ... would declare the inline operator as a member, and not as a member, any general effect on the speed of the actual function?

I have several different structures that currently follow the inline member statement ... But I wanted to change it to be a valid C code; therefore, before I do this, I would like to know if there will be any changes in performance / compilation.

+3
source
3

, , B::operator* . , A::operator* :

inline A A::operator*(A* this, A b) 
{ 
    A out;
    out.x = this->x * b.x;
    out.y = this->y * b.y;
    return out;
}

, A , B . .

, , A B, const :

struct A
{
    float x, y;
    inline A operator*(const A& b) const 
    {
        A out;
        out.x = x * b.x;
        out.y = y * b.y;
        return out;
    } 
}

struct B
{
    float x, y;
}

inline B operator*(const B& a, const B& b) 
{
    B out;
    out.x = a.x * b.x;
    out.y = a.y * b.y;
    return out;
}

- , , ( ).


, const , B, , A, - ?

-, , . (, this .)

, . , , , , , . , ( , ) , . ( - ):

// Setup for the function. Usually already done by the inlining.
r1 <- this
r2 <- &result
r3 <- &b

// Actual function.
r4 <- r1[0]
r4 <- r4 * r3[0]
r2[0] <- r4
r4 <- r1[4]
r4 <- r4 * r3[4]
r2[4] <- r4

RISC- (, ARM). x86, , , . , , , . , . ( , / LLVM- , result , .)

, this, . , ? ; ( , ). , - , , .

-S gcc, , , .

+10

inline -ing .

, , ( A), inline. inline A::operator * .

, . , ( ), , .

++ FAQ 9.

+3

struct:

struct A
{
    float x, y;
    A(float ax, float ay) : x(ax), y(ay) { }
    A operator*(const A& b) const { return b(x * b.x, y * b.y); } 
}

, , - , , .

:

  • Never worry about using the inline keyword. Optimization compilers make their own decisions about what and what not to do.

  • Use initialization of constructors. Do this because they improve code readability. Sleep better, knowing that they can bring small performance.

  • Pass structures by const reference as often as possible.

  • Focus on writing code that has a good style not fast. Most of the code is fast enough, and if it is not, it is probably due to something dizzy in the algorithms or processing IO.

+2
source

All Articles