You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
3.0 KiB
C#

This file contains invisible Unicode characters!

This file contains invisible Unicode characters that may be processed differently from what appears below. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to reveal hidden characters.

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BenchmarkDotNet;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Jobs;
using BenchMarkDotnetStudy.Core;
namespace BenchMarkDotnetStudy.BenchmarkStudy
{
/// <summary>
/// Counter 基准测试
/// </summary>
[SimpleJob(runtimeMoniker:RuntimeMoniker.Net70)]
[SimpleJob(runtimeMoniker:RuntimeMoniker.NativeAot70)]
[MemoryDiagnoser]
[ThreadingDiagnoser]
public class CounterTest3
{
[Benchmark]
public void Thread2_Test()
{
Counter counter = new Counter();
List<Thread> threads = new List<Thread>()
{
new Thread(() => counter.Increment() ),
new Thread(() => counter.Increment() ),
};
threads.ForEach(t => t.Start());
threads.ForEach(t => t.Join());
//Console.WriteLine($"方法结束时TotalCounter = {Counter.TotalCounter}");
}
[Benchmark]
public void Thread2_Test2()
{
Counter counter = new Counter();
List<Thread> threads = new List<Thread>()
{
new Thread(() => counter.IncrementWithInterlocked() ),
new Thread(() => counter.IncrementWithInterlocked() ),
};
threads.ForEach(t => t.Start());
threads.ForEach(t => t.Join());
}
}
}