Please HELP! performance in C#

PB 1 Reputation point
2021-12-03T23:39:09.96+00:00

HI All, I've migrated a VB.Net code to C# and will working very well, the performance of the C# code is really awful, about 5 times slower than its VB.Net counterpart. Granted, I've made some changes, from Modules to Classes, and from ByRef to ByVal, and made SUBS into FUNCTIONS and refactored a Structure into a Class. Other than those changes, it's the same code.

Here is an example (chopped off) of the code that takes forever in C#

VB.Net

Module Module_Main

Public Track(5) As Tracks
Public Structure Tracks
Dim ALTITUDE As Double
Dim ANGLE_OF_FALL() As Double
Dim CANT_X_LINEAR() As Double
End Structure

Public Sub Compute_Values()
Convert_Units(Track(0))
----etc
End Sub

End Module

C#

public class Module_Main
{
public static Tracks[] Track = new Tracks[5];

    public class Tracks
    {
        public double ALTITUDE = 0.0;
        public double[] ANGLE_OF_FALL = new double[2000+ 1];
        public double[] CANT_X_LINEAR = new double[2000+ 1];
     }

    public static Tracks Compute_Values(Tracks Track)
    {
      Track = Convert_Units(Track);
       //etc----
     }
 }

and the call is

Track[0] = Compute_Values(Track[0]);


In short, here is the code and while I've tried different ways to mimic the VB.Net code (using voids instead of calling functions, passing by reference, etc) and zero improvement in performance.

Please any HELP is greatly appreciated. Thanks in advance!

Developer technologies | C#
{count} votes

14 answers

Sort by: Most helpful
  1. WayneAKing 4,931 Reputation points
    2021-12-05T03:40:07.63+00:00

    There is obvious overhead related to the use of
    Classes which isn't present with Modules.

    Every module has exactly one instance and does not need
    to be created or assigned to a variable. A Class object
    must be instantiated, and when that happens within a
    piece of code that limits the object's scope/lifetime
    the class object gets created and destroyed repeatedly.
    The class object's constructor and destructor get
    invoked every time.

    The additional overhead for a class vs. a module may
    not be apparent to human perception when it occurs a
    limited number of times - such as once. But when
    that time overhead is multiplied many times over the
    disparity is magnified so that it becomes perceptible.

    • Wayne

  2. M 76 Reputation points
    2021-12-05T11:53:53.86+00:00

    Hi,
    If you think what Bruce wrote could be true "I suspect you are creating 100’s in 100’s" try using a break statement in your loop. See if you get better results.
    Maybe not a solution but just a step to take to see a possible cause.

    -M

    0 comments No comments

  3. Ken Tucker 5,861 Reputation points
    2021-12-05T13:57:17.997+00:00

    Take a look at this code

       Track[0] = Compute_Values(Track[0]);
    

    Each time the code runs in the loop it needs to destroy the old Track[0] and create a new one which is slow. It might be better to pass a reference to the object instead of replacing it

    0 comments No comments

  4. PB 1 Reputation point
    2021-12-05T18:02:35.727+00:00

    Thanks for the reply. I've already tried passing by reference and the performance is the same, no change at all. May you have another idea?


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.