|
|
|
|
using System;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
|
|
|
|
|
namespace BenchMarkDotnetStudy.Core
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 计数器
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class Counter
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 总次数
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static int TotalCounter = 0;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 每方法执行次数
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static readonly int LoopNumber = 1000;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 累加方法
|
|
|
|
|
/// </summary>
|
|
|
|
|
public int Increment()
|
|
|
|
|
{
|
|
|
|
|
for (int i = 1; i <= LoopNumber; i++)
|
|
|
|
|
{
|
|
|
|
|
++TotalCounter;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return TotalCounter;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 累加方法
|
|
|
|
|
/// </summary>
|
|
|
|
|
public int IncrementWithInterlocked()
|
|
|
|
|
{
|
|
|
|
|
for (int i = 1; i <= LoopNumber; i++)
|
|
|
|
|
{
|
|
|
|
|
Interlocked.Increment(ref TotalCounter);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return TotalCounter;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|