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

  • SEARCH
  • Home
  • 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 7963467
In Process

The Archive Base Latest Questions

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

I recently asked a question about structs, and optimizing some overloaded operators. The original

  • 0

I recently asked a question about structs, and optimizing some overloaded operators.

The original question is here

Now, I have taken those improvements to heart (or some/most of them), and I return with the following functions (nonmember functions, as I wish for them to be C compliant if possible).

inline Vector2& operator+=(Vector2 &a, const Vector2 &b)
{
    a.x += b.x;
    a.y += b.y;
    return a;
}

inline Vector2 operator+(Vector2 a, const Vector2 &b) 
{
    a += b;
    return a;
}

inline Vector2& operator*=(Vector2 &a, const float &n)
{
    a.x *= n;
    a.y *= n;
    return a;
}

inline Vector2 operator*(Vector2 a, const float &n) 
{
    a *= n;
    return a;
}

inline float operator*(const Vector2 &a, const Vector2 &b)
{
    return (a.x * b.x) + (a.y * b.y);
}

inline Vector2 rotate(const Vector2 &a, const float &angle)
{
    Vector2 out = a;
    out *= cos(angle);
    out.x -= sin(angle) * a.y;
    out.y += sin(angle) * a.x;
    return out;
}

(Please note, I omitted subtraction, and another multiplication operator, as they were equivalent to other operators listed here).

I am currently unable to notice any other potential improvements.
Have I missed anything, that will (potentially) make these functions, as they currently stand, inefficient?

  • 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:36:46+00:00Added an answer on June 4, 2026 at 5:36 am

    It is pointless to speak of improvements without profiling. However, there could be some room for improvement here:

    inline Vector2 rotate(const Vector2& a, const float &angle) {
        Vector2 out = a;
        out *= cos(angle);
        const float sinA + sin(angle);
        out.x -= sinA * a.y;
        out.y += sinA * a.x;
        return out;
    }
    

    here, you cache the result of sin(angle) instead of calling the function twice. But I really have to stress that you should have a profiling system in place before trying any changes, so you can see whether they make a difference or not, and gauge whether any improvements are actually worthwhile. The compiler might well optimize away things that seem to be inefficient when looking at the source code.

    it is worth knowing about RVO and NRVO, copy ellision and, in C++11, move semantics. Also, see this relevant article.

    EDIT: My original answer was badly broken and this one is significantly different.

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

Sidebar

Related Questions

Recently I have asked a question about what I should use to create self-contained
I recently asked a question about some problems I was having getting MIT Kerberos
Quite recently, I asked a question about debugging a seg fault: What are some
I recently asked a question here on Stack Overflow about how to cast my
I recently asked a question about committing a new file but now I am
I recently asked a question about formatting JavaScript code in Vim. And I've also
I've recently asked a question about clipping an image via path at view's drawRect
I asked a question [ here ] recently and it's just not providing me
Here one more basic question asked in MS interview recently class A { public
Recently I have been asked an interview question What are the events order in

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.