r/csharp • u/Staeff • May 02 '18
A small performance comparison of mono-wasm/Blazor, .NET Core, C, C-wasm and JavaScript
Taken straight from my Github repo: https://github.com/stefan-schweiger/dotWasmBenchmark
Overall I'm kind of disappointed with the performance. I know it's very early in the development cycle, but the performance is about x200x20 slower than even JavaScript. Somewhere out there they are working on a AOT variant of mono-wasm, but the last public commit was in January and it's status can be described as "experimental" at best.
Maybe I'm overlooking something to gain more performance. I would really love to hear tips how to improve performance.
Anyways here are my results:
Benchmark Information
The Benchmark is currently very simple and only does the following things:
- Generate 100,000 random list elements (from 0.0 to 1.0)
- Sort the list by their values
- Get Q1, Median, Q3 and calculate average and standard deviation
Platform Information
The Benchmark was implemented in the following languages/platforms:
- .NET Core 2.1.300 (preview2-008533)
- mono-wasm (commit a14f41c from Blazor 0.3.0)
- C (gcc 4.2.1)
- C-wasm (emcc 1.37.36)
- JavaScript (TypeScript 2.8.1)
mono-wasm with AOT was also attempted, but the project seems not to developed in the open and resulted in either compilation or JIT errors when running.
The .NET Projects where build with Release configuration and the C Projects with -O3 optimizations.
Results
| C | C.Wasm | DotNet.Console | DotNet.Wasm | JavaScript | |
|---|---|---|---|---|---|
| Generate | 1.21ms | 1.00ms | 1.00ms | 127.00ms | 7.50ms |
| Sort | 9.05ms | 12.00ms | 26.00ms | 406.00ms | 22.40ms |
| Calculate | 0.21ms | 1.00ms | 4.00ms | 474.00ms | 6.60ms |
| C | C.Wasm | DotNet.Console | DotNet.Wasm | JavaScript | |
|---|---|---|---|---|---|
| Generate | 1.21ms | 1.00ms | 1.00ms | 84.00ms | 4.00ms |
| Sort | 9.05ms | 12.00ms | 26.00ms | 297.00ms | 16.00ms |
| Calculate | 0.21ms | 1.00ms | 4.00ms | 321.00ms | 4.00ms |
EDIT: I've updated to Blazor 0.3.0, made a few code changes based on some suggestions in the comments and added Firefox benchmarks. Overall performance improved, but it's still much slower than even Javascript.
15
u/migueldeicaza May 03 '18
Blazor is currently using a .NET interpreter running on top of WebAssembly, it is what we envision to be used for quick iteration.
We are hard at work on the static compiler that will turn .NET code into WebAssembly code, rather than running on an interpreter running on top of WebAssembly.
7
u/wllmsaccnt May 03 '18
In your calc for .NET you are using an 'Average' call. That would enumerate the collection again. The equivalent JavaScript code isn't doing that, its just doing a division operation against the collection count.
I would get rid of all of the IList usages. In performance code using arrays directly is preferred, especially for fixed size collections. At the very least, at least use List instead of IList. It looks like on WASM the call lookups are much more expensive.
Also, it looks like you have some console hookup in your index in JavaScript. If that is listening for Blazor console writes, then you need to comment out all of your console writelines that occur during the test unless you are certain that the console piping can't happen asynchronously and accidentally overlap with testing code (its doing an element lookup and writing to the DOM, that can be very expensive, and if it is happening async, it is most likely occurring in the middle of your performance test). Collecting timings and avoiding all writing until the end may also work.
What version of which browser are you using to run the Blazor test?
5
u/Enlogen May 02 '18
Is this using today's 0.3.0 release of Blazor?
3
u/geoffreymcgill May 03 '18 edited May 03 '18
I was interested in how Bridge would benchmark against your Wasm and JavaScript tests. Looks like Bridge is running about 10x faster than Wasm/Blazor, which is pretty much exactly what we're seeing in our local performance tests.
I combined your perf files into one Deck so it can be shared easily...
https://deck.net/90d5f88199753f64b37b5e2256bdb4bc
Can you run the Deck above and respond with the results? Run on the same machine/browser as you're running the Wasm and JavaScript tests.
I'm getting similar results in Firefox.
There are some config options in Bridge that could likely speed this up even more. Maybe squeeze another 20-30% faster.
2
u/BezierPatch May 03 '18
What if you use a built-in sort function instead of rolling your own?
3
u/FizixMan May 02 '18 edited May 02 '18
Did you try running it multiple times within the same program? Perhaps the initial JIT compile and first run takes a long time and that's blowing out the results. Although I would have expected that to maybe provide a constant time added.
EDIT: Maybe try minimizing virtual calls. Try using a
List<T>instead ofIList<T>and see if that helps. Maybe the runtime is doing a lot of work routing the virtual calls aiming for correctness/proof-of-concept right now and not optimizing it.6
u/devperez May 03 '18
IIRC, didn't they say that the Mono WASM implementation was with 0 optimizations? I thought they were just focusing on implementation at the moment.
-3
u/puppy2016 May 02 '18
The underlying JavaScript interpreter will be always terrible compared to mature runtimes like CLR or JVM. We're going seriously backward.