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 4083316
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:17:51+00:00 2026-05-20T18:17:51+00:00

I have a function that has a large number if individual input parameters, the

  • 0

I have a function that has a large number if individual input parameters, the function is also run hundreds of millions of times during the program.

If i wanted to optimize this function, should I create a new data structure that holds all the input parameters and pass it to the function by reference instead of passing each parameter individually to the function? Or would it not matter because the compiler is smart enough to deal wit this in an even more efficient manner?

  • 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-05-20T18:17:51+00:00Added an answer on May 20, 2026 at 6:17 pm

    In general, it’s much better to pass a data structure that contains your variables. This isn’t pretty to look at or use:

    void f(int a, int b, int c, int d, int e, int f)
    {
       // do stuff
    }
    

    This is much nicer:

    void f(Params p)
    {
      // do stuff with p
    }
    

    You may want to do pass by reference, so the compiler can just pass a reference to the object, and not copy the entire data structure.
    As a real example:

    double distance(double x1, double y1, double z1, double x2, double y2, double z2)
    {
        double dx = x1 - x2;
        double dy = y1 - y2;
        double dz = z1 - z2;
    
        return sqrt(dx*dx + dy*dy + dz*dz);
    }
    

    It would be better if encapsulated our (x, y, z) into a data structure though:

    struct Point
    {
        double x;
        double y;
        double z;
    };
    
    double distance(const Point &p1, const Point &p2)
    {
        double dx = p1.x - p2.x;
        double dy = p1.y - p2.y;
        double dz = p1.z - p2.z;
    
        return sqrt(dx*dx + dy*dy + dz*dz);
    }
    

    Much cleaner code, and you get the added bonus that it could* perform better (*Depending on how smart your compiler is at optimizing either version).

    Obviously this could vary wildly depending on what you’re actually trying to accomplish, but if you have several (4+) variables that have a similar usage in a certain context, it may be better to just pass it in a data structure.

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

Sidebar

Related Questions

gcc 4.4.2 c89 I have a function that has to run (config_relays). It make
I have a code that has a large number of malloc s and device-specific
I have a function that has an array pointer passed it to modify stuff
Now I have a function that has to return a string. I saw a
I have a function that generates a key of 4 characters that has to
If I have a function foo() that windows has implemented in kernel32.dll and it
I have a control that has a Filter property that expects a function that
I have a member function in a class that has a callback, but the
I have a base class Foo that has an Update() function, which I want
I have a program in Octave that has a loop - running a function

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.