1

Recently moved from python to C#. Developing math app. Looked through many questions at SO about class vs struct, so I would like an advice from experienced people regarding performance.

DETAILS

I have a method, and during its execution 6 double variables are calculated and about 6 double[] arrays of same lenght. I want my method to return all them "packed" into one variable. Not planning to change them, I just need a storage and acces to them. During execution of the app, method will be called many times, such storages will be created many times also (up to 40).

REPRODUCING EXAMPLE

public (???) Method (params)
{ 
 double return_value1 = actions_with_params1;
 double return_value2 = actions_with_params2;
 double[] return_array1 = actions_with_paramsin_a_loop1;
 double[] return_array2 = actions_with_paramsin_a_loop2;
}

... and so on. I want to return a variable holding both doubles and double[]s. What should I use better insetafd of (???)? Class or struct, regarding performance?

Thank you!

Mikhail T.
  • 1,240
  • 12
  • 21
  • 1
    I suggest you to take a look at [this](http://stackoverflow.com/a/1951243/2324535) excellent answer. – Karel Tamayo Oct 18 '16 at 13:45
  • 2
    Honestly, in C# always use a class unless some requirement forces you to use a struct (like interop with native code) or you have run a profiler and found that GC Gen 0 is taking a unacceptable amount of time, and that Gen 0 slowness is caused by it being bloated with thousands of small short lived classes that could be treated with "value type semantics". Other than those two specific cases, just use a class. – Scott Chamberlain Oct 18 '16 at 13:47
  • Thank you all! So if during app execution i would create an array of 100 objects returned by my method, i would face performance go down due to the stack got populated, correct? – Mikhail T. Oct 18 '16 at 13:53
  • 1
    See [this answer](http://stackoverflow.com/questions/1113819), but also know that not all structs live in the stack. – D Stanley Oct 18 '16 at 14:00

1 Answers1

1

Here's a sample of how I'd do it:

class Program
{
    static void Main(string[] args)
    {
        List<Container> myValueStorage = new List<Container>();

        for (int i = 1; i < WhateverAmountOfOperations; i++)
        {
            myValueStorage.Add(YourMethod(yourParams));
        }
    }

    public static Container YourMethod(yourParams)
    {
        //Perform your calculations and store your results in the following variables
        double[] double_results; //An array of doubles
        double[][] double_array_results; //An array of double arrays

        //Create and return a class object containing the values
        return new Container(double_results, double_array_results);
    }


}
class Container
{
    double[] _doubles { get; }
    double[][] _double_arrays { get; }
    public Container (double[] doubles, double[][] double_arrays)
    {
        _doubles = doubles;
        _double_arrays = double_arrays;
    }
}
Innat3
  • 3,561
  • 2
  • 11
  • 29