Reputation: 14005
I neet to compare two implementations of the same functionality but done for two frameworks: .net 4.6.2 and .net 7
How do I define this benchmark?
Upvotes: 1
Views: 740
Reputation: 2072
I have created 2 library projects, one in .net 4.6.2 and one in .net 7 with the same class and function.
namespace ClassLibrary1
{
public class Class1
{
public int CalcInt32(int OperationsPerInvoke)
{
int firstDigit = 135;
int secondDigit = 145;
for (int i = 0; i < OperationsPerInvoke; i++)
{
firstDigit += secondDigit;
}
return firstDigit;
}
}
}
After that I've created a new test project .net 7 that will do the tests by invoking the method from each assembly:
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using System.Reflection;
BenchmarkRunner.Run<PerformanceTest>();
public class PerformanceTest
{
const int OperationsPerInvoke = 4096;
[Benchmark(OperationsPerInvoke = OperationsPerInvoke)]
public int TestClassLibrary7()
{
var myAssembly = Assembly.LoadFile("d:\\Teste\\.NetGeneral\\_Dlls\\ClassLibrary1\\bin\\Debug\\net7.0\\ClassLibrary1.dll");
var myType = myAssembly.GetType("ClassLibrary1");
if (myType == null)
return 0;
MethodInfo dllGetMethod = myType.GetMethod("CalcInt32", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy);
object res = dllGetMethod?.Invoke(null, new object[] { OperationsPerInvoke });
return (int)res;
}
[Benchmark(OperationsPerInvoke = OperationsPerInvoke)]
public int TestClassLibrary462()
{
var myAssembly = Assembly.LoadFile("d:\\Teste\\.NetGeneral\\_Dlls\\ClassLibrary2\\bin\\Debug\\ClassLibrary2.dll");
var myType = myAssembly.GetType("ClassLibrary1");
if (myType == null)
return 0;
MethodInfo dllGetMethod = myType.GetMethod("CalcInt32", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy);
object res = dllGetMethod?.Invoke(null, new object[] { OperationsPerInvoke });
return (int)res;
}
}
and it's working. Here are some results for my test:
Method | Mean | Error | StdDev |
---|---|---|---|
TestClassLibrary7 | 0.1374 ns | 0.0017 ns | 0.0015 ns |
TestClassLibrary462 | 0.1308 ns | 0.0019 ns | 0.0017 ns |
Upvotes: 3