Open ForkBug opened 2 years ago
It is expected that C++ would perform better than C#. Another big reason in this difference arises from usage of hardware acceleration. As far as I know, libsodium uses SSE/AVX instruction sets to vectorize operations and also has CPU intrinsic operations like AES instruction.
A benchmark of C and C# has been ported from https://benchmarksgame-team.pages.debian.net/benchmarksgame/performance/simple.html to https://github.com/ForkBug/CAndCSharpBenchmark The result on my machine is:
BenchmarkDotNet=v0.13.1, .NET SDK=6.0.300
[Host] : .NET 6.0.5 (6.0.522.21309), X64 RyuJIT
DefaultJob : .NET 6.0.5 (6.0.522.21309), X64 RyuJIT
Method | Mean | Error | StdDev |
---|---|---|---|
DebianSimpleCpp | 13.20 s | 0.013 s | 0.011 s |
DebianSimpleCSharp | 13.28 s | 0.011 s | 0.010 s |
C++ would perform better than C#. But there shouldn't be a perceivable difference for most CPU-intensive tasks.
C# fx provides AVX and Aes intrinsics. I think the usage is easy than in C. https://docs.microsoft.com/en-us/dotnet/api/system.runtime.intrinsics.x86.aes?view=net-6.0&viewFallbackFrom=netstandard-2.0
I has done a little benchmark testing about BouncyCastle and Libsodium The result is
Libsodium performances too well. C/C++ couldn't get the result over C#. Did I doing something wrong?
Testing code is