以下の内容はhttps://rksoftware.hatenablog.com/entry/2025/03/20/220000より取得しました。


C# で速度を測る その2 ~Pleasanter のテストを書く

Pleasanter で遊ぼう! ということで Pleasanter にテストを書いていきたいと思います。

■ 前回

テスト対象の速さを維持するためには計測が必要ということで、ベンチマークを設定しました。
しかし、今やりたい高尾tには大きすぎた感じです。
rksoftware.hatenablog.com

■ 今回

ちょっとのことしか測れないものの、小さく、すぐに測り終わる。そして先々は自分の欲しい機能を有実できる自分コードで測ってみます。

事項結果

BenchmarkDecimals :: TrimEndZero_ShouldReturnZero_WhenInputIsZero
BenchmarkDecimals :: TrimEndZero_ShouldReturnZero_WhenInputIsZero_G29
BenchmarkDecimals :: TrimEndZero_ShouldReturnSameNumber_WhenInputHasNoTrailingZeros
BenchmarkDecimals :: TrimEndZero_ShouldReturnSameNumber_WhenInputHasNoTrailingZeros_G29
BenchmarkDecimals :: TrimEndZero_ShouldReturnTrimmedNumber_WhenInputHasTrailingZeros
BenchmarkDecimals :: TrimEndZero_ShouldReturnTrimmedNumber_WhenInputHasTrailingZeros_G29
BenchmarkDecimals :: TrimEndZero_ShouldReturnTrimmedNumber_WhenInputHasTrailingZeros29
BenchmarkDecimals :: TrimEndZero_ShouldReturnTrimmedNumber_WhenInputHasTrailingZeros29_G29
BenchmarkDecimals :: TrimEndZero_ShouldReturnTrimmedNumber_WhenInputIsNull
BenchmarkDecimals :: TrimEndZero_ShouldReturnTrimmedNumber_WhenInputIsNull_G29
BenchmarkDecimals :: TrimEndZero_ShouldReturnTrimmedNumber_WhenInputAllowNullIsZero
BenchmarkDecimals :: TrimEndZero_ShouldReturnTrimmedNumber_WhenInputAllowNullIsZero_G29
BenchmarkDecimals :: TrimEndZero_ShouldReturnTrimmedNumber_WhenInputAllowNullHasTrailingZeros
BenchmarkDecimals :: TrimEndZero_ShouldReturnTrimmedNumber_WhenInputAllowNullHasTrailingZeros_G29
TotalSeconds: 0.2580222 -

Average:      27,224, Type: BenchmarkDecimals, Method: TrimEndZero_ShouldReturnZero_WhenInputIsZero
Average:       3,179, Type: BenchmarkDecimals, Method: TrimEndZero_ShouldReturnZero_WhenInputIsZero_G29
Average:      25,939, Type: BenchmarkDecimals, Method: TrimEndZero_ShouldReturnSameNumber_WhenInputHasNoTrailingZeros
Average:       3,227, Type: BenchmarkDecimals, Method: TrimEndZero_ShouldReturnSameNumber_WhenInputHasNoTrailingZeros_G29
Average:      22,321, Type: BenchmarkDecimals, Method: TrimEndZero_ShouldReturnTrimmedNumber_WhenInputHasTrailingZeros
Average:       2,636, Type: BenchmarkDecimals, Method: TrimEndZero_ShouldReturnTrimmedNumber_WhenInputHasTrailingZeros_G29
Average:      30,389, Type: BenchmarkDecimals, Method: TrimEndZero_ShouldReturnTrimmedNumber_WhenInputHasTrailingZeros29
Average:       6,750, Type: BenchmarkDecimals, Method: TrimEndZero_ShouldReturnTrimmedNumber_WhenInputHasTrailingZeros29_G29
Average:       3,217, Type: BenchmarkDecimals, Method: TrimEndZero_ShouldReturnTrimmedNumber_WhenInputIsNull
Average:       1,444, Type: BenchmarkDecimals, Method: TrimEndZero_ShouldReturnTrimmedNumber_WhenInputIsNull_G29
Average:      10,326, Type: BenchmarkDecimals, Method: TrimEndZero_ShouldReturnTrimmedNumber_WhenInputAllowNullIsZero
Average:       2,268, Type: BenchmarkDecimals, Method: TrimEndZero_ShouldReturnTrimmedNumber_WhenInputAllowNullIsZero_G29
Average:      20,800, Type: BenchmarkDecimals, Method: TrimEndZero_ShouldReturnTrimmedNumber_WhenInputAllowNullHasTrailingZeros
Average:       1,900, Type: BenchmarkDecimals, Method: TrimEndZero_ShouldReturnTrimmedNumber_WhenInputAllowNullHasTrailingZeros_G29

テストコード

前回のコードをそのまま使えるように使い方を合わせています。

using BenchmarkDotNet.Running;
using RkSoftware.TinyBenchmark.Formatter;
using RkSoftware.TinyBenchmark.Runners;
using System.Diagnostics;


var sw = Stopwatch.StartNew();
//var _ = BenchmarkRunner.Run<PleasanterBenchmark.Libraries.Utilities.BenchmarkDecimals>();
var results = TinyBenchmarkRunner.Run<PleasanterBenchmark.Libraries.Utilities.BenchmarkDecimals>();
sw.Stop();
Console.WriteLine($"TotalSeconds: {sw.Elapsed.TotalSeconds} -");
Console.WriteLine();

TinyBenchmarkFormatter.Write(results, System.Console.WriteLine);
using BenchmarkDotNet.Attributes;
using RkSoftware.TinyBenchmark.Attributes;

namespace PleasanterBenchmark.Libraries.Utilities
{
    public class BenchmarkDecimals
    {
        [TinyBenchmark]
        [Benchmark]
        public static void TrimEndZero_ShouldReturnZero_WhenInputIsZero() => Implem.Libraries.Utilities.Decimals.TrimEndZero(0m);
        [TinyBenchmark]
        [Benchmark]
        public static void TrimEndZero_ShouldReturnZero_WhenInputIsZero_G29() => M(0m);

        [TinyBenchmark]
        [Benchmark]
        public static void TrimEndZero_ShouldReturnSameNumber_WhenInputHasNoTrailingZeros() => Implem.Libraries.Utilities.Decimals.TrimEndZero(123.45m);
        [TinyBenchmark]
        [Benchmark]
        public static void TrimEndZero_ShouldReturnSameNumber_WhenInputHasNoTrailingZeros_G29() => M(123.45m);

        [TinyBenchmark]
        [Benchmark]
        public static void TrimEndZero_ShouldReturnTrimmedNumber_WhenInputHasTrailingZeros() => Implem.Libraries.Utilities.Decimals.TrimEndZero(123.4500m);
        [TinyBenchmark]
        [Benchmark]
        public static void TrimEndZero_ShouldReturnTrimmedNumber_WhenInputHasTrailingZeros_G29() => M(123.4500m);

        [TinyBenchmark]
        [Benchmark]
        public static void TrimEndZero_ShouldReturnTrimmedNumber_WhenInputHasTrailingZeros29() => Implem.Libraries.Utilities.Decimals.TrimEndZero(9.999_000_000_000_000_000_000_000_000m);
        [TinyBenchmark]
        [Benchmark]
        public static void TrimEndZero_ShouldReturnTrimmedNumber_WhenInputHasTrailingZeros29_G29() => M(9.999_000_000_000_000_000_000_000_000m);

        [TinyBenchmark]
        [Benchmark]
        public static void TrimEndZero_ShouldReturnTrimmedNumber_WhenInputIsNull() => Implem.Libraries.Utilities.Decimals.TrimEndZero(null);
        [TinyBenchmark]
        [Benchmark]
        public static void TrimEndZero_ShouldReturnTrimmedNumber_WhenInputIsNull_G29() => M(null);

        [TinyBenchmark]
        [Benchmark]
        public static void TrimEndZero_ShouldReturnTrimmedNumber_WhenInputAllowNullIsZero() => Implem.Libraries.Utilities.Decimals.TrimEndZero((decimal?)0m);
        [TinyBenchmark]
        [Benchmark]
        public static void TrimEndZero_ShouldReturnTrimmedNumber_WhenInputAllowNullIsZero_G29() => M((decimal?)0m);

        [TinyBenchmark]
        [Benchmark]
        public static void TrimEndZero_ShouldReturnTrimmedNumber_WhenInputAllowNullHasTrailingZeros() => Implem.Libraries.Utilities.Decimals.TrimEndZero((decimal?)123.4500m);
        [TinyBenchmark]
        [Benchmark]
        public static void TrimEndZero_ShouldReturnTrimmedNumber_WhenInputAllowNullHasTrailingZeros_G29() => M((decimal?)123.4500m);


        private static string M(decimal input) => input.ToString("G29");
        private static string M(decimal? input) => input?.ToString("G29") ?? "0";

    }
}

■ 今回ン作った計測のクラスライブラリ

コードはこちら github.com

■ テストを書いているリポジトリ

github.com




以上の内容はhttps://rksoftware.hatenablog.com/entry/2025/03/20/220000より取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14