gpt4 book ai didi

c# - 在 C# 中解构元组的性能损失?

转载 作者:行者123 更新时间:2023-12-03 20:04:19 25 4
gpt4 key购买 nike

如果我们这样做

var (hello, world) = GetHelloAndWorldStrings();
if (hello == "hello" && world == "world")
Environment.Exit(0);
除了仅执行以下操作外,这是否会产生任何额外费用:
var helloAndWorld = GetHelloAndWorldStrings();
if (helloAndWorld.Hello == "hello" && helloAndWorld.World == "world")
Environment.Exit(0);
或者这都是语法糖 - 最终生成的代码总是使用 Item1 和 Item2。

最佳答案

它们实际上是相同的生成 IL,只有很小的发射差异,然而 ...它们很可能会根据完全相同的指令进行抖动和优化。
给定

private (String hello,string world) GetHelloAndWorldStrings() 
{
return ("asdsd","sadfsdf");
}

...


public int Test1()
{
var asd = GetHelloAndWorldStrings();
if (asd.hello == "hello" && asd.world == "world")
return 1;
return 0;
}

public int Test2()
{
var (hello, world) = GetHelloAndWorldStrings();
if (hello == "hello" && world == "world")
return 1;
return 0;
}
基本上会被发射为
public int Test1()
{
ValueTuple<string, string> helloAndWorldStrings = GetHelloAndWorldStrings();
if (helloAndWorldStrings.Item1 == "hello" && helloAndWorldStrings.Item2 == "world")
{
return 1;
}
return 0;
}

public int Test2()
{
ValueTuple<string, string> helloAndWorldStrings = GetHelloAndWorldStrings();
string item = helloAndWorldStrings.Item1;
string item2 = helloAndWorldStrings.Item2;
if (item == "hello" && item2 == "world")
{
return 1;
}
return 0;
}
You can check the IL out here
这是发布中的 JIT ASM 示例
C.Test1()
L0000: push ebp
L0001: mov ebp, esp
L0003: push esi
L0004: mov ecx, [0x11198648]
L000a: mov esi, [0x1119864c]
L0010: mov edx, [0x11198650]
L0016: call System.String.Equals(System.String, System.String)
L001b: test eax, eax
L001d: je short L0038
L001f: mov edx, [0x11198654]
L0025: mov ecx, esi
L0027: call System.String.Equals(System.String, System.String)
L002c: test eax, eax
L002e: je short L0038
L0030: mov eax, 1
L0035: pop esi
L0036: pop ebp
L0037: ret
L0038: xor eax, eax
L003a: pop esi
L003b: pop ebp
L003c: ret
对比
C.Test2()
L0000: push ebp
L0001: mov ebp, esp
L0003: push esi
L0004: mov ecx, [0x11198648]
L000a: mov esi, [0x1119864c]
L0010: mov edx, [0x11198650]
L0016: call System.String.Equals(System.String, System.String)
L001b: test eax, eax
L001d: je short L0038
L001f: mov edx, [0x11198654]
L0025: mov ecx, esi
L0027: call System.String.Equals(System.String, System.String)
L002c: test eax, eax
L002e: je short L0038
L0030: mov eax, 1
L0035: pop esi
L0036: pop ebp
L0037: ret
L0038: xor eax, eax
L003a: pop esi
L003b: pop ebp
L003c: ret
简而言之,担心这个的净 yield ……减去你生命中的 5 分钟,你将永远不会回来

关于c# - 在 C# 中解构元组的性能损失?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63482191/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com