experiments/csharp/E069-IntegerToDecimalBenchmarks/Program.cs

30 lines
650 B
C#
Raw Permalink Normal View History

2024-05-04 20:16:52 +00:00
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
BenchmarkRunner.Run<IntegerToDecimalBenchmarks>();
[SimpleJob, MemoryDiagnoser(false)]
public class IntegerToDecimalBenchmarks
{
private const int Value = 69_420; // nice, and blaze it
[Benchmark]
[Arguments(Value)]
public float UsingToStringAndParse(int value)
{
return float.Parse($"0.{value}");
}
[Benchmark]
[Arguments(Value)]
public float UsingDivide(int value)
{
if (value == 0)
{
return 0;
}
return value / MathF.Pow(10, MathF.Floor(MathF.Log10(MathF.Abs(value))) + 1);
}
}