案例
using BenchmarkDotNet.Attributes;
using BenchmarkDotNetDemo.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace BenchmarkDotNetDemo
{
[MemoryDiagnoser, RankColumn]
public class Test
{
IList<HTEAndStudent> stringslist = new List<HTEAndStudent>();
/* List<HTEAndStudent> stringslist2 = new List<HTEAndStudent>();
List<HTEAndStudentDto> hTEAndStudentDtosOut = new();*/
public Test()
{
Random random = new Random();
for (int i = 0; i < 10000000; i++)
{
stringslist.Add(new HTEAndStudent
{
IsSubmit = true,
StudentId = i,
Score = i + random.Next(1, 99),
Scorerate = 666,
StudyClassId = i + random.Next(1, 11),
});
/* stringslist2.Add(new HTEAndStudent
{
IsSubmit = true,
StudentId = i.ToString(),
Score = i + random.Next(1, 99),
Scorerate = 666,
StudyClassId = i + random.Next(1, 11),
});*/
}
/* for (int i = 0; i < 1000; i++)
{
hTEAndStudentDtosOut.Add(new()
{
IsSubmit = true,
StudentId = i,
Score = i + random.Next(1, 99),
Scorerate = 666,
StudyClassId = i + random.Next(1, 81),
});
}*/
}
/* [Benchmark]
public void KK()
{
var aaaa = stringslist2.Where(r => r.StudentId < 1800).ToList();
foreach (var item in aaaa)
{
var bbbb = hTEAndStudentDtosOut.Where(r => r.StudyClassId == item.StudyClassId && r.Score > 50).ToList();
var aadd = bbbb.Count;
}
}
[Benchmark]
public void KK1()
{
var aaaa = stringslist2.Where(r => r.StudentId < 1800).ToList();
var bbbbdic = hTEAndStudentDtosOut.GroupBy(r=>r.StudyClassId).ToDictionary(r => r.Key,v=>v.Where(r=>r.Score > 50).ToList());
foreach (var item in aaaa)
{
if(bbbbdic.ContainsKey(item.StudyClassId))
{
var bbbb = bbbbdic[item.StudyClassId];
var aadd = bbbb.Count;
}
}
}*/
/*
[Benchmark]
public void Count_Method()
{
if (stringslist.Count() == 0)
{
Console.WriteLine("5555");
}
}
[Benchmark]
public void Count_Property()
{
if (stringslist.Count == 0)
{
Console.WriteLine("5555");
}
}
[Benchmark]
public void Any()
{
if (!stringslist.Any())
{
Console.WriteLine("5555");
}
}
[Benchmark]
public void Count_Method_Where()
{
if (stringslist.Count(r => r.StudentId == 5) == 0)
{
Console.WriteLine("5555");
}
}
[Benchmark]
public void Any_Method_Where()
{
if (!stringslist.Any(r => r.StudentId == 5))
{
Console.WriteLine("5555");
}
}
[Benchmark]
public void IListForeEachList()
{
List<HTEAndStudentDto> hTEAndStudentDtos = new();
stringslist.ToList().ForEach(r =>
{
hTEAndStudentDtos.Add(new()
{
StudyClassId = r.StudyClassId,
Scorerate = r.Scorerate,
IsSubmit = r.IsSubmit,
Score = r.Score,
StudentId = r.StudentId,
});
});
}*/
[Benchmark]
public void ForeEachList()
{
List<HTEAndStudentDto> hTEAndStudentDtos = new();
stringslist.ToList().ForEach(r =>
{
hTEAndStudentDtos.Add(new()
{
StudyClassId = r.StudyClassId,
Scorerate = r.Scorerate,
IsSubmit = r.IsSubmit,
Score = r.Score,
StudentId = r.StudentId,
});
});
}
[Benchmark]
public void Forech()
{
List<HTEAndStudentDto> hTEAndStudentDtos = new();
foreach (var r in stringslist)
{
hTEAndStudentDtos.Add(new()
{
StudyClassId = r.StudyClassId,
Scorerate = r.Scorerate,
IsSubmit = r.IsSubmit,
Score = r.Score,
StudentId = r.StudentId,
});
}
}
[Benchmark]
public void For()
{
List<HTEAndStudentDto> hTEAndStudentDtos = new();
for (int i = 0; i < stringslist.Count; i++)
{
hTEAndStudentDtos.Add(new()
{
StudyClassId = stringslist[i].StudyClassId,
Scorerate = stringslist[i].Scorerate,
IsSubmit = stringslist[i].IsSubmit,
Score = stringslist[i].Score,
StudentId = stringslist[i].StudentId,
});
}
}
/* [Benchmark]
public void ForechIntoDto()
{
List<HTEAndStudentDto> hTEAndStudentDtos = new();
foreach (var r in stringslist)
{
hTEAndStudentDtos.Add(new()
{
StudyClassId = r.StudyClassId,
Scorerate = r.Scorerate,
IsSubmit = r.IsSubmit,
Score = r.Score,
StudentId = r.StudentId,
});
}
}
[Benchmark]
public void LinqSelectIntoDto()
{
List<HTEAndStudentDto> hTEAndStudentDtos = stringslist.Select(r => new HTEAndStudentDto()
{
StudyClassId = r.StudyClassId,
Scorerate = r.Scorerate,
IsSubmit = r.IsSubmit,
Score = r.Score,
StudentId = r.StudentId,
}
).ToList();
}
[Benchmark]
public void foreachHasWhereOuter()
{
List<HTEAndStudentDto> hTEAndStudentDtos = new();
var data = stringslist.Where(r => r.Score > 80);
foreach (var r in data)
{
hTEAndStudentDtos.Add(new()
{
StudyClassId = r.StudyClassId,
Scorerate = r.Scorerate,
IsSubmit = r.IsSubmit,
Score = r.Score,
StudentId = r.StudentId,
});
}
}
[Benchmark]
public void foreachHasWhereInner()
{
List<HTEAndStudentDto> hTEAndStudentDtos = new();
foreach (var r in stringslist.Where(r => r.Score > 80))
{
hTEAndStudentDtos.Add(new()
{
StudyClassId = r.StudyClassId,
Scorerate = r.Scorerate,
IsSubmit = r.IsSubmit,
Score = r.Score,
StudentId = r.StudentId,
});
}
}*/
}
}
Program
using BenchmarkDotNet.Running;
namespace BenchmarkDotNetDemo
{
internal class Program
{
static void Main(string[] args)
{
var summary = BenchmarkRunner.Run<Test>();
Console.WriteLine("Hello, World!");
}
}
}
标签:StudyClassId,StudentId,stringslist,性能,NetBenchmarkDotNet,Score,测试,Scorerate,new
From: https://www.cnblogs.com/kkbk/p/17702057.html