locked
Impact on the performance when passing parameters to a method RRS feed

  • Question

  • Hello Techies,

     

    I was just in the corridor when a developer pulled me off my collar
    and asked me a question.

    He was asking a question whether it is better to pass a single parameter as a Object or pass individual values for each required parameter

    private void InsertSQL(EmployeeClass pEmployee)
    {
     Dbhelper.SqlInsert(pEmployee.id,pEmployee.name);
    } 

    or

    private void InsertSQL(int pid,string pname)
    {
     Dbhelper.SqlInsert(pid,pname);
    } 


    Among the above 2 methods which one is better & the reason.

    In my Opinion the 2nd option would be the preferred 1 when it comes to memory management. But I don’t have any idea about performance. 

    Thanks for your feedback
    Clement

    Friday, February 26, 2010 9:50 AM

Answers

  • Stealing my answer from Martin Fowler...

    When you have a group of parameters that are commonly passed together to a number of methods (not just one), then you should replace them with an object. Once the parameters have been clumped together, you usually find common (duplicated) behavior that should be pulled out of those methods and into the new object's methods.

    In your specific case, the answer depends on the semantics of "InsertSQL" within its enclosing class. Does "InsertSQL" insert an EmployeeClass or an id+name pair? The answer to that question will determine which parameter style to use.

    In other words: it's all about design, not performance.

           -Steve
    Programming blog: http://nitoprograms.blogspot.com/
      Including my TCP/IP .NET Sockets FAQ
      and How to Implement IDisposable and Finalizers: 3 Easy Rules
    Microsoft Certified Professional Developer

    How to get to Heaven according to the Bible
    • Proposed as answer by CiaranODonnell Friday, February 26, 2010 1:41 PM
    • Marked as answer by Ji.Zhou Friday, March 5, 2010 2:39 AM
    Friday, February 26, 2010 1:40 PM

All replies

  • 1st option is better because when you pass a class as a parameter, C# sends it's reference and don't copy it in stack for returning from method.
    So the first option is better in performance because it only must keep a reference which is an integer(memory address).

    2nd option must copy pid and pname because they are by value not by reference so it's poorer than 1st option in performance.


    My Blog - MSDN Complement by providing Visual C# Walkthroughs and Sample Codes - Founded In February 24, 2010
    Friday, February 26, 2010 10:42 AM
  • 1st option is better because when you pass a class as a parameter, C# sends it's reference and don't copy it in stack for returning from method.
    So the first option is better in performance because it only must keep a reference which is an integer(memory address).

    2nd option must copy pid and pname because they are by value not by reference so it's poorer than 1st option in performance.


    My Blog - MSDN Complement by providing Visual C# Walkthroughs and Sample Codes - Founded In February 24, 2010

    This difference is absolutely crucial to understanding parameter passing in C#, and is why I believe it is highly confusing to say that objects are passed by reference by default instead of the correct statement that object references are passed by value by default.
    Thanks Mike --------Please mark as answer if it is useful----------
    Friday, February 26, 2010 11:05 AM
  • 1st option is better because when you pass a class as a parameter, C# sends it's reference and don't copy it in stack for returning from method.
    So the first option is better in performance because it only must keep a reference which is an integer(memory address).

    2nd option must copy pid and pname because they are by value not by reference so it's poorer than 1st option in performance.


    My Blog - MSDN Complement by providing Visual C# Walkthroughs and Sample Codes - Founded In February 24, 2010

    This difference is absolutely crucial to understanding parameter passing in C#, and is why I believe it is highly confusing to say that objects are passed by reference by default instead of the correct statement that object references are passed by value by default.
    Thanks Mike --------Please mark as answer if it is useful----------

    Please refer to http://msdn.microsoft.com/en-us/library/4d43ts61.aspx
    My Blog - MSDN Complement by providing Visual C# Walkthroughs and Sample Codes - Founded In February 24, 2010
    Friday, February 26, 2010 11:23 AM
  • In addition to what has been said already IMHO you should use which ever method suits your application design and not worry about performance too much until you can show it becomes an issue through profiling. In the example you provide the difference in performance will be unnoticeable.
    That said the link Yasser B Zamani provided explains the differences of which you should be aware.

    From a design point of view I would prefer the second way as this explicitly states which fields are required by the method. Also, it explicitly states that any changes to the parameters are not propogated back to the method caller. However, the first example would make it easier to extend the method in the future.

    Matt

    Friday, February 26, 2010 11:59 AM
  • Stealing my answer from Martin Fowler...

    When you have a group of parameters that are commonly passed together to a number of methods (not just one), then you should replace them with an object. Once the parameters have been clumped together, you usually find common (duplicated) behavior that should be pulled out of those methods and into the new object's methods.

    In your specific case, the answer depends on the semantics of "InsertSQL" within its enclosing class. Does "InsertSQL" insert an EmployeeClass or an id+name pair? The answer to that question will determine which parameter style to use.

    In other words: it's all about design, not performance.

           -Steve
    Programming blog: http://nitoprograms.blogspot.com/
      Including my TCP/IP .NET Sockets FAQ
      and How to Implement IDisposable and Finalizers: 3 Easy Rules
    Microsoft Certified Professional Developer

    How to get to Heaven according to the Bible
    • Proposed as answer by CiaranODonnell Friday, February 26, 2010 1:41 PM
    • Marked as answer by Ji.Zhou Friday, March 5, 2010 2:39 AM
    Friday, February 26, 2010 1:40 PM