1

Hopefully my question hasn't been answered yet, I have only found this link but it is not really answering my exact question.

Basically I have a class MyClass which contains a local member currentSettings of another class Settings. MyClass contains a method DoStuff which uses information that is contained in any object of a Settings-object.

Now the situation: The method will never be called from within the class itself. Some other class will contain a MyClass-object and only from there will the method be called. Now I have two possibilities:

class MyClass
{
    Settings currentSettings = new Settings();

    public void DoStuff()
    {
        //do stuff here using things stored in "currentSettings"
    }

    public void DoStuff(Settings settings)
    {
        //do stuff here using things stored in "settings"
    }
}

class SomeClass
{
    MyClass myClass = new MyClass();

    ...
    //call method somewhere, with two different options:
    myClass.DoStuff();

    myClass.DoStuff(myClass.currentSettings);
}

The second variant of course seems a bit overcomplicated, however, it leaves me the freedom to pass any settings to it which might not be the local member currentSettings and that I will need in some cases.

I am now concerned about the performance difference between these two choices and if the reference passing might be a significant amount slower than using the local settings, as this method might be called very often (up to a couple hundred times a second).

Edit: I have posted a little performance test in the answers and could not find any significant difference between the two methods.

Community
  • 1
  • 1
philkark
  • 2,417
  • 7
  • 38
  • 59
  • Try it and measure it yourself, but there should be close to zero performance difference. – D Stanley Nov 23 '13 at 16:24
  • Sorry, yes of course I could (and will) do it myself. It was rather a general question if there might be significant differneces in some random instances, which I don't know about. – philkark Nov 23 '13 at 16:27
  • 2
    Do not compromise a good software design for insignificant performance gains (assuming, and i believe so, that in this scenario there is no significant gain) – Luis Filipe Nov 23 '13 at 16:34

3 Answers3

4

There should be little to no performance difference. All you're doing is reordering a few instructions, all of which deal with local memory.

The second variant of course seems a bit overcomplicated

Passing one parameter to a method is not overcomplicated in the slightest.

it leaves me the freedom to pass any settings to it which might not be the local member currentSettings and that I will need in some cases

The second part of that statement alone tells me you should use the parameter.

D Stanley
  • 149,601
  • 11
  • 178
  • 240
  • Thank you for your answer. The main reason why I asked is, that I have the choice of simply implementing the two overloads of the method and call it depending on the choice of which settings will be used. But for that I would have to reprogram quite a bit and I wanted to know if that might be worth it performance wise. – philkark Nov 23 '13 at 16:31
  • If you're wondering, the correct overloaded method is calculated on compile-time so there is no difference in having many overloads in runtime. – Luis Filipe Nov 23 '13 at 16:32
1

The best answer is to tell you to benchmark it and see the results for yourself.

Upfront i would say you'll notice no significant difference between both. You're passing a reference which is just a C# pointer behind the scenes. I guess most of your cpu-time will be inside the method itself and not on calling it.

I'll be waiting for your feedback, even better your benchmark results.

Luis Filipe
  • 8,488
  • 7
  • 48
  • 76
1

Okay, I have done a quick dirty performance test and it turns out the difference is completely not noticable:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        MyClass mc = new MyClass();
        Stopwatch sw = new Stopwatch();
        Settings set = new Settings();
        sw.Start();
        for (int i = 0; i < 1000000; i++)
        {
            mc.DoStuff();
        }
        sw.Stop();
        tb1.Text = sw.ElapsedTicks.ToString();

        sw.Restart();
        for (int i = 0; i < 1000000; i++)
        {
            mc.DoStuff(set);
        }
        sw.Stop();
        tb2.Text = sw.ElapsedTicks.ToString();

    }
}

class MyClass
{
    Settings currentSettings = new Settings();

    public void DoStuff()
    {
        int test = 0;
        for(int k = 0; k < 10 ; k++)
        {
            test += currentSettings.i + currentSettings.j;
        }

    }

    public void DoStuff(Settings settings)
    {
        int test = 0;
        for (int k = 0; k < 10; k++)
        {
            test += settings.i + settings.j;
        }
    }
}

class Settings
{
    public int i = 1, j = 2;
}

Though I have noticed that setting the number of repeats in the loops down to 10000, the reference passing is almost always faster, which however can be for many reasons as it only takes a few thousand ticks.

Luis Filipe
  • 8,488
  • 7
  • 48
  • 76
philkark
  • 2,417
  • 7
  • 38
  • 59