Александър Гьонов
Здрасти, аз съм Сашо
Контакти
 
[ БГ EN ]

My personal comparison of basic performance in .NET 7

Dec 4, 2022

My personal comparison of basic performance in .NET 7

I was excited on general availability of .NET 7, announced on the annual Build 2022 event. As you probably know it comes with many new and improved features in number of dotnet areas. In the presentations and announcements, one of the emphases was on performance of the new version of the framework. And there are some very good and in detail blog posts about particular bits in the platform that made for this performance improvement. What I mean are these:

Also, there were also some discussions over the Internet, as how correct were the announcements during the Build event in regard of comparing, particularly ASP .NET Core with other web frameworks.

And in addition to the above, I personally was interested in how these tiny pieces of improvements, here and there across the framework, are going to show themselves in few simple programing tasks. So, I decided to do a couple of small comparison projects in:

  • 🔻C# .NET 7
  • 🔻TypeScript NodeJS
  • 🔻RUST

1. Some basic performance

The general and simple idea for this test is to:

  1. Generate some random numbers (bytes) - 268Mb
  2. Do some basic math calculation for each one of the numbers. Something as:
    double d = 0.0;
    int a;
    for (int intr = 0; intr < arr.Length; intr++) {
     a = arr[intr] * 2;
     d = d + a / 313.0;
    }
    

I've done simple programs for this basic logic in C#, TypeScript and RUST. And here are the results of the execution of these programs on my computer:

Language Time
C# .NET 7 262ms
NodeJS 340ms
RUST 261ms

Comment on test 1

Clearly, .NET 7 is really fast. At least compared to Rust for this kind of basic calculations.

As for NodeJS, well it looks like it is ~ 1/3 slower, on this kind of work.

NB Please DO NOT extrapolate the above result on some comparisons like ASP .NET Core vs Express or something else - this is just basic calculation performance. In reality, for example if we look at a Web Framework it very much depends what you are doing in your Web APIs - if it is only simple async processing of request/result with data transfer of data stored in some background storage service then I guess the times will be pretty much equal between these frameworks. But, If the Web APIs are doing some more complex calculations - images, PDFs and etc., then it is quite possible that the difference between NodeJS and the other two become even bigger.

2. A little bit more complex work - just for .NET and RUST. JSON Serialization and De-serialization

After the basic test it looks like the performance of .NET 7 and RUST is quite equal? But this is just a basic test. Let's do something little bit more complex and let us play with some JSON de-serialization / serialization.

As a side note - handling JSON data is quite important now-a-days and there were also statements from Microsoft that in recent versions of .NET there has been a lot of improvement in JSON handling area with so-called Source generators.

The task is to have a simple program that:

  1. Gets a file of JSON data - 25Mb
  2. De-serialize this data in memory
  3. Serialize this data back to JSON
 // Parse JSON
let jdata: Vec<JData> = match serde_json::from_str(contents.as_str()) {
    Ok(parsed) => parsed,
    Err(err) => {
        println!("Can not parse JSON data. Err: {}", err.to_string());
        return;
    }
};

// Serialize
let serialized: String = match serde_json::to_string(&jdata) {
    Ok(res) => res,
    Err(err) => {
        println!("Can not serialize JSON data. Err: {}", err.to_string());
        return;
    }
};

And here are the results of the execution:

Language What Time
C# .NET 7 Serialize 44 ms
C# .NET 7 Deserialize 128 ms
RUST Serialize 36 ms
RUST Deserialize 62 ms

Comment on test 2

Well, .NET 7 is not that fast compared to Rust for JSON handling.

Especially in De-serialization it is like ~ 1/2 slower. In Serialization it is ~ 1/4 slower. Great job from .NET community so far, but clearly there is more work to be done 😊 ...

And, I guess this is not related only to pure handling of the JSON data, but more or less with memory allocation - because of differences between serialization and de-serialization performance gaps. Actually, it is quite normal as RUST does not rely on Garbage collection, whereas .NET does.

NB As I have mention memory - The above tests measure (for now) pure performance in time, they do not measure memory used by the different languages for performing the test tasks.

References

All the test code can be seen and accessed in GitHub at: agyonov/dotnetcompare