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

推荐订阅源

freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
腾讯CDC
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
L
LINUX DO - 热门话题
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Project Zero
Project Zero
V
Vulnerabilities – Threatpost
Cisco Talos Blog
Cisco Talos Blog
P
Palo Alto Networks Blog
C
Cisco Blogs
A
Arctic Wolf
月光博客
月光博客
The GitHub Blog
The GitHub Blog
T
The Blog of Author Tim Ferriss
量子位
小众软件
小众软件
Latest news
Latest news
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Microsoft Security Blog
Microsoft Security Blog
T
The Exploit Database - CXSecurity.com
Security Latest
Security Latest
N
Netflix TechBlog - Medium
K
Kaspersky official blog
人人都是产品经理
人人都是产品经理
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园_首页
Y
Y Combinator Blog
P
Proofpoint News Feed
H
Hackread – Cybersecurity News, Data Breaches, AI and More
M
MIT News - Artificial intelligence
T
Threat Research - Cisco Blogs
S
Schneier on Security
D
Docker
Scott Helme
Scott Helme
MyScale Blog
MyScale Blog
Spread Privacy
Spread Privacy
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
GbyAI
GbyAI
有赞技术团队
有赞技术团队
Google DeepMind News
Google DeepMind News
The Hacker News
The Hacker News
H
Help Net Security
Simon Willison's Weblog
Simon Willison's Weblog
J
Java Code Geeks
C
Cyber Attacks, Cyber Crime and Cyber Security
T
Tenable Blog
B
Blog
Know Your Adversary
Know Your Adversary
IT之家
IT之家

博客园 - Bob-wei

Visual Studio 配置额外工具 Windows Terminal 等 CMakeList.txt Windows Terminal 配置文件 JavaScript 的 parseInt(x), parseFloat(x), Number(x), +x, ~~x, x>>>0, isNaN(x) 区别和结果 Windows Terminal 配置 git-bash,集成右键菜单,集成VSCode 恢复WIn10 2004的“要使用本计算机,用户必须输入用户名和密码”选项 plantuml server with math docker image aspnetcore singleton service in app.use [bash] 编写7zz函数替换7z压缩命令 Chrome 不跟随 macOS 系统主题改变深色模式 VSCode的浅色主题(Default Light+)侧边活动栏改为浅色 VSCode_Extensions C++ in VSCode 用户中心 - 博客园 C# 私有字段前缀 _ 的设置(VS2019, .editorconfig) dotnet 跨平台编译发布 重新调整动态vhdx占用的空间 通过git-bash一句话获得当前目录的全部csproj文件绝对路径 devdocs
C# 7.2 中 In参数( in parameter )的性能比较
Bob-wei · 2020-07-29 · via 博客园 - Bob-wei

结论:in参数的性能要优于普通参数非常多。

如下图所示,分别创建 40 和 10000 个结构体,进行下面的测试:

- InParameters: 使用in参数调用方法

- InParameters2: 使用in参数调用方法(调用方没有使用in关键字,因为是可选的,因此和InParameters结果一样)

- Normal: 普通调用方法

测试代码:

 1 using BenchmarkDotNet.Attributes;
 2 
 3 namespace RectangleGameCore.Benchmarks
 4 {
 5     /// <summary>
 6     /// 测试CSharp的In参数性能
 7     /// </summary>
 8     [MemoryDiagnoser]
 9     [RPlotExporter]
10     [BenchmarkCategory("CSharp", "InParameter")]
11     public class CSharpInParameter
12     {
13         [Params(40, 10_000)]
14         public int N;
15 
16         private MyStruct[] src;
17         private int[] dst;
18 
19         [GlobalSetup]
20         public void Setup()
21         {
22             src = new MyStruct[N];
23             dst = new int[N];
24         }
25 
26         [Benchmark(Baseline = true)]
27         public void Normal()
28         {
29             for (var i = N - 1; i >= 0; i--)
30             {
31                 dst[i] = DoSomeThing(src[i]);
32             }
33         }
34 
35         [Benchmark]
36         public void InParameter()
37         {
38             for (var i = N - 1; i >= 0; i--)
39             {
40                 dst[i] = DoSomeThing(in src[i]);
41             }
42         }
43 
44         [Benchmark]
45         public void InParameter2()
46         {
47             for (var i = N - 1; i >= 0; i--)
48             {
49                 dst[i] = DoSomeThing2(src[i]);
50             }
51         }
52 
53         private static int DoSomeThing(MyStruct s) => s.X + s.Y + s.Z;
54         private static int DoSomeThing(in MyStruct s) => s.X + s.Y + s.Z;
55         private static int DoSomeThing2(in MyStruct s) => s.X + s.Y + s.Z;
56 
57 
58         private readonly struct MyStruct
59         {
60             public int X { get; }
61             public int Y { get; }
62             public int Z { get; }
63 
64             public MyStruct(int N)
65             {
66                 X = N;
67                 Y = N;
68                 Z = N;
69             }
70         }
71     }
72 }