Auto Mapper、Json、手写模型转换效率测试对比
Auto Mapper、Json、手写模型转换效率测试对比
目录
使用工具及版本:
- Net Framwork4.7.2与Net Core 3.1
- Newtonsoft.Json 12.0.3
- AutoMapper 8.1.0
一.测试代码
1.定义模型:数据模型和DTO模型
/// <summary>
/// 数据模型
/// </summary>
public class TestEntity
{
public string TextInfo { get; set; }
public int Num { get; set; }
public DateTime DateTime { get; set; }
}
/// <summary>
/// DTO模型
/// </summary>
public class TestEntityDto
{
public string TextInfo { get; set; }
public int Num { get; set; }
public DateTime DateTime { get; set; }
}
2.创建随机数据模型数据
/// <summary>
/// 获取一个随机构造数据
/// </summary>
/// <returns></returns>
private static TestEntity GetEntity()
{
Random random = new Random();
return new TestEntity
{
Num = random.Next(int.MaxValue - 10000000, int.MaxValue),
TextInfo = random.Next(int.MaxValue - 10000000, int.MaxValue).ToString(),
DateTime = DateTime.Now
};
}
/// <summary>
/// 返回一定数量的数据模型的数据
/// </summary>
/// <param name="num">数量</param>
/// <returns></returns>
private static List<TestEntity> GetEntities(int num)
{
List<TestEntity> list = new List<TestEntity>();
for (int i = 0; i < num; i++)
{
list.Add(GetEntity());
}
return list;
}
3.测评代码
#region 测试Json方法
private static IEnumerable<TestEntityDto> TestJson(List<TestEntity> list)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
var data = list.Select(a => TestJson(a)).ToList();
stopwatch.Stop();
Console.WriteLine($"{list.Count}个:Json转换模式需要时间是:{stopwatch.Elapsed.TotalMilliseconds}");
return data;
}
private static TestEntityDto TestJson(TestEntity entity)
{
string jsonData = JsonConvert.SerializeObject(entity);
return JsonConvert.DeserializeObject<TestEntityDto>(jsonData);
}
#endregion
#region 自己赋值获取方法
private static IEnumerable<TestEntityDto> TestBase(List<TestEntity> list)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
var data = list.Select(a => new TestEntityDto()
{
TextInfo = a.TextInfo,
Num = a.Num,
DateTime = a.DateTime
}).ToList();
stopwatch.Stop();
Console.WriteLine($"{list.Count}个:Base转换模式需要时间是:{stopwatch.Elapsed.TotalMilliseconds}");
return data;
}
#endregion
#region 使用AutoMapper
private static IEnumerable<TestEntityDto> TestAutoMapper(List<TestEntity> list)
{
var config = new MapperConfiguration(cfg => cfg.CreateMap<TestEntity, TestEntityDto>());
var mapper = config.CreateMapper();
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
// var data = list.Select(a =>
// {
// return mapper.Map<TestEntityDto>(a);
// //return a.MapTo<TestEntity, TestEntityDto>();
// }
// ).ToList();
var data=list.MapListTo<TestEntity, TestEntityDto>();
stopwatch.Stop();
Console.WriteLine($"{list.Count}个:Map转换模式需要时间是:{stopwatch.Elapsed.TotalMilliseconds}");
return data;
}
#endregion
二.测试结果(单位ms)
1. Net Framework4.7.2
次数 | Auto Mapper | Json | 手写 |
---|---|---|---|
100 | 33.565 | 248.4642 | 0.6279 |
1000 | 31.467 | 239.796 | 0.7331 |
10000 | 34.2621 | 289.0829 | 1.1483 |
100000 | 44.8533 | 746.8191 | 11.8257 |
1000000 | 178.6784 | 5163.0549 | 120.0984 |
2.Net Core3.1
次数 | Auto Mapper | Json | 手写 |
---|---|---|---|
100 | 31.0479 | 192.2935 | 1.1125 |
1000 | 27.181 | 194.1254 | 1.1135 |
10000 | 28.3492 | 338.893 | 1.7902 |
100000 | 44.0803 | 871.5923 | 11.509 |
1000000 | 139.8299 | 4821.7951 | 97.6601 |
三.结论
在模型转换的时候手写效率是最高的,但是数据量越大,Auto Mapper与手写的效率越接近。
C#感觉是最漂亮的