Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7961721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:07:41+00:00 2026-06-04T05:07:41+00:00

If I have two structs: struct A { float x, y; inline A operator*(A

  • 0

If I have two structs:

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 an equivalent struct

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;
} 

Would you know of any reason for B’s operator* to compile any differently, or run any slower or faster than A’s operator* (the actual actions that go on inside the functions should be irrelevant)?

What I mean is… would declaring the inline operator as a member, vs not as a member, have any generic effect on the speed of the actual function, whatsoever?

I’ve got a number of different structs that currently follow the inline member operator style… But I was wanting to modify it to be valid C code, instead; so before I do that I wanted to know if there would be any changes to performance/compilation.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T05:07:43+00:00Added an answer on June 4, 2026 at 5:07 am

    The way you have it written, I’d expect B::operator* to run slightly slower. This is because the “under the hood” implementation of A::operator* is like:

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

    So A passes a pointer to its left-hand-side argument to the function, while B has to make a copy of that parameter before calling the function. Both have to make copies of their right-hand-side parameters.

    Your code would be much better off, and probably would implement the same for A and B, if you wrote it using references and made it const correct:

    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;
    }
    

    You still want to return objects, not references, since the results are effectively temporaries (you’re not returning a modified existing object).


    Addendum

    However, with the const pass-by-reference for both arguments, in B, would it make it effectively faster than A, due to the dereferencing?

    First off, both involve the same dereferencing when you spell out all the code. (Remember, accessing members of this implies a pointer dereference.)

    But even then, it depends on how smart your compiler is. In this case, let’s say it looks at your structure and decides it can’t stuff it in a register because it’s two floats, so it will use pointers to access them. So the dereferenced pointer case (which is what references get implemented as) is the best you’ll get. The assembly is going to look something like this (this is pseudo-assembly-code):

    // 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
    

    This is assuming a RISC-like architecture (say, ARM). x86 probably uses less steps but it gets expanded to about this level of detail by the instruction decoder anyway. The point being that it’s all fixed-offset dereferences of pointers in registers, which is about as fast as it will get. The optimizer can try to be smarter and implement the objects across several registers, but that kind of optimizer is a lot harder to write. (Though I have a sneaking suspicion that an LLVM-type compiler/optimizer could do that optimization easily if result were merely a temporary object that is not preserved.)

    So, since you’re using this, you have an implicit pointer dereference. But what if the object were on the stack? Doesn’t help; stack variables turn into fixed-offset dereferences of the stack pointer (or frame pointer, if used). So you’re dereferencing a pointer somewhere in the end, unless your compiler is bright enough to take your object and spread it across multiple registers.

    Feel free to pass the -S option to gcc to get a disassembly of the final code to see what’s really happening in your case.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So, I have two structs: struct coordinate { float x; float y; } struct
I have two vector classes: typedef struct D3DXVECTOR3 { FLOAT x; FLOAT y; FLOAT
I have a simple struct Wrapper , distinguished by two templated assignment operator overloads:
I have two structs like so: public struct KeyLog { Keys key; DateTime time;
I have two structs containing some fields: struct MyNodeData, and struct MyEdgeData. When I
So i have two structs in the global section typedef struct stack_1 { short
I have read that if you declare two structs like this: struct Node {
Suppose we have two structs: typedef struct Struct1 { short a_short; int id; }
I have a program that compares variables from two structs and sets a bit
I have two window form applications written in C, one holds a struct consisting

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.