Ayoub.A
Ayoub.A

Reputation: 2093

.Net Core vs .NET Framework performance issue

I have a .NET Core class library (let's call it Core) and I want to use WPF GUI to use some of it's functionalities. But it seems .NET is very slow compared to .NET Core. For instance I have the following method that I would like to run:

private static void LoadData()
{
    Stopwatch w = new Stopwatch();


    List<BookingRecord> records = new List<BookingRecord>();
    w.Start();
    string csv = File.ReadAllText("/BookingData/Booking_Data - Copy.csv");

    Console.WriteLine(w.Elapsed);
    w.Restart();

    var lines = csv.Split('\n');

    foreach(var line in lines.Skip(1))
    {
        var data = line.Split(';');

        records.Add(new BookingRecord()
        {
            Origin = data[2],
            Destination = data[3],
            FlightDate = DateTime.Parse(data[4], new CultureInfo("De-de").DateTimeFormat),
            PassengersNumber = int.Parse(data[9])
        });
    }

    Console.WriteLine(w.Elapsed);
    w.Stop();
}

For this I used two console apps, one under .NET Core, and one under .NET Framework. the time of constructing the list of BookingData under .NET Core is 0.4s, while in .NET Framework case is around 10s, which is problematic since I will be loading larger files of about 2 million lines and beyond (client requirement).

So is there any workaround this issue? The objective is to provide a GUI displaying results from Core library.

EDIT

The snippet above is just to provide MCVE, otherwise, I'm reading the file using StreamReader with the help of CsvHelper, and the construction is done while reading.

EDIT 2

The loading and construction of data is built inside the Core library, in other words using .NET Core, the WPF or .NET Framework project in general, is just referencing the Core library and using its methods, it's just a display tool. Why does it have to produce the same result as when the code is moved to .NET project?

Upvotes: 0

Views: 1105

Answers (2)

gjvdkamp
gjvdkamp

Reputation: 10516

Try to move the list creation after you ave read the lines:

var lines = csv.Split('\n');
var records = new List<BookingRecord>(lines.Length);

This should create a huge difference. In your verison it will start with a underlying array of 8 items, when that is full create a new one of 16 items and copy the old ones over, when that is full create a underlying array of 32 items and copy the 16 over, etc.

Initializing it to the right size prevents this. Maybe Core is able to do this in a smarter way already. It is supposed to be significantly faster although a factor of 25x seems to indicate there's something else at play here.

Even faster will be if you make the record a struct instead of a class as it can reserve the entire chunk of memory in one go instead of small bits per record.

Upvotes: 1

Mark Wagoner
Mark Wagoner

Reputation: 1769

One of the main reasons MS says you should use .Net Core is the performance gains. A Google search seems to verify this.

However, regardless of which platform you use you will likely see better performance, and definitely better memory utilization, if you read the file one row at at a time using some sort of buffered reader. Using ReadAllText is going to try to load all 2 millions lines into memory at once. This isn't necessary if all you are doing is processing each line in the order they appear in the file.

Upvotes: 1

Related Questions