惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Webroot Blog
Webroot Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
SecWiki News
SecWiki News
S
Secure Thoughts
V2EX - 技术
V2EX - 技术
T
Tor Project blog
H
Hacker News: Front Page
P
Privacy International News Feed
Google DeepMind News
Google DeepMind News
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
V
Vulnerabilities – Threatpost
C
CERT Recently Published Vulnerability Notes
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
C
Cyber Attacks, Cyber Crime and Cyber Security
Help Net Security
Help Net Security
D
Darknet – Hacking Tools, Hacker News & Cyber Security
H
Heimdal Security Blog
AI
AI
PCI Perspectives
PCI Perspectives
Cyberwarzone
Cyberwarzone
P
Privacy & Cybersecurity Law Blog
AWS News Blog
AWS News Blog
Attack and Defense Labs
Attack and Defense Labs
The Last Watchdog
The Last Watchdog
K
Kaspersky official blog
T
The Exploit Database - CXSecurity.com
C
CXSECURITY Database RSS Feed - CXSecurity.com
Security Latest
Security Latest
Schneier on Security
Schneier on Security
Scott Helme
Scott Helme
L
Lohrmann on Cybersecurity
Cisco Talos Blog
Cisco Talos Blog
The Hacker News
The Hacker News
N
News and Events Feed by Topic
S
Schneier on Security
Simon Willison's Weblog
Simon Willison's Weblog
F
Fortinet All Blogs
T
Threatpost
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
V
V2EX
博客园 - 三生石上(FineUI控件)
WordPress大学
WordPress大学
Apple Machine Learning Research
Apple Machine Learning Research
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
云风的 BLOG
云风的 BLOG
博客园_首页
Recent Announcements
Recent Announcements
G
Google Developers Blog
Martin Fowler
Martin Fowler

博客园 - 生命体验之kevin-Y

如何让Trae IDE调用windbg来分析dump文件 Avalonia.Controls.DataGrid自动合并列 asp.net core如何实现Controller热更新 windbg无法分析dotnetframework3.5的dmp asp.net core通过配置决定AddSingleton的实现类 vscode指定python文件的执行程序 golang离线开发-windows-vscode go语言学习 - caddy配置的一些发现 go语言学习 - caddy添加nacos-gateway 学习jsp-使用IDEA2024社区版 ravendb源代码学习一:服务启动 修整程序集需要 .NET Core 3.0 或更高版本。 打开电脑后一直弹出“交互式检测服务”窗口 Idea2024-java-Maven开发配置 C#获取事件绑定的方法 java.util.zip.DataFormatException: incorrect header check Build Secure Web Services With SOAP Headers and Extensions “System.Net.Http.HttpContent”不包含“ReadAsAsync”的定义 C#入门:如何合理制定方法参数-下 C#入门:如何合理制定方法参数-上
拷贝对象的开源工具类-FastMapper-TinyMapper-Mapster
生命体验之kevin-Y · 2023-10-20 · via 博客园 - 生命体验之kevin-Y

至2023年10月,前两个项目的主要代码分别都有8年和6年历史了。Mapster最近还有修改

FastMapper
https://github.com/FastMapper/FastMapper

TinyMapper
https://github.com/TinyMapper/TinyMapper

Mapster
https://github.com/MapsterMapper/Mapster/

Mapster应该只支持net6\net7,三者的速度都号称比AutoMapper快。我好奇他们谁快。于是对比了一下。

在net6下建了命令行程序,主要测试代码如下

int max = 100000;
Type a = typeof(ClassA);
Type b = typeof(ClassB);
Type c = typeof(ClassC);

Type typeListA = typeof(List<ClassA>);
Type typeListB = typeof(List<ClassB>);
Type typeListC = typeof(List<ClassC>);

var objA = new ClassA() { Name = "kevin", Description = "Test", Age = 132, Birthday = DateTime.Now };
var lstA = new List<ClassA>();
lstA.Add(objA);

RunTest("TinyMapper", max,
    () => {
        TinyMapper.Bind(a, b);
        TinyMapper.Bind(a, c);
    },
    () =>
    {
        var to1 = TinyMapper.Map(a, b, objA);
        var to2 = TinyMapper.Map(a, c, objA);
    }
);

RunTest("FastMapper", max, () => { }, () =>
{
    var to1 = FastMapper.TypeAdapter.Adapt(objA, a, b);
    var to2 = FastMapper.TypeAdapter.Adapt(objA, a, c);
});

View Code

FastMapper有一个有意思的表现一定数量级以下比较快

执行100000次,TinyMapper用时 167毫秒
执行100000次,FastMapper用时 96毫秒
执行100000次,Mapster...用时 220毫秒
Test List<A>...
执行100000次,TinyMapper用时 96毫秒
执行100000次,FastMapper用时 146毫秒
执行100000次,Mapster...用时 77毫秒

超过一定数值就比较慢了

执行1000000次,TinyMapper用时 304毫秒
执行1000000次,FastMapper用时 747毫秒
执行1000000次,Mapster...用时 520毫秒
Test List<A>...
执行1000000次,TinyMapper用时 514毫秒
执行1000000次,FastMapper用时 1059毫秒
执行1000000次,Mapster...用时 378毫秒

因为公司有net4的项目需要维护,所以也做了一个net40下的比较。跟net6的速度对比,net4的明显慢了。因为Mapster没有net4所以没有参与net4的比较。

执行1000000次,TinyMapper用时 418毫秒
执行1000000次,FastMapper用时 915毫秒
Test List<A>...
执行1000000次,TinyMapper用时 791毫秒
执行1000000次,FastMapper用时 1427毫秒

减少数量,看看FastMapper有没有net6的怪现象

执行100000次,TinyMapper用时 101毫秒
执行100000次,FastMapper用时 104毫秒
Test List<A>...
执行100000次,TinyMapper用时 92毫秒
执行100000次,FastMapper用时 215毫秒

似乎没有了。其实我觉得这是误差范围,偶尔也有FastMapper快几毫秒的时候。好了,其实几个都很快了。

但TinyMapper的使用显然没有FastMapper方便,改造一下就好了。

public static object Adapt(Type sourceType, Type targetType, object source, object target = null)
{
    TypePair typePair = TypePair.Create(sourceType, targetType);

    Mapper mapper = GetOrBuildMapper(typePair);
    var result = mapper.Map(source, target);
    return result;
}

private static Mapper GetOrBuildMapper(TypePair typePair)
{
    Mapper mapper;            
    lock (_mappersLock)
    {
        if (_mappers.TryGetValue(typePair, out mapper) == false)
        {
            mapper = _targetMapperBuilder.Build(typePair);
            _mappers[typePair] = mapper;
        }
    }
    return mapper;
}

本文只发布在博客园,未经同意请勿转载!