- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
以下 F# 程序定义了一个函数,该函数返回表示为结构元组的两对整数中较小的一个,运行时间为 1.4 秒:
let [<EntryPoint>] main _ =
let min a b : int = if a < b then a else b
let min (struct(a1, b1)) (struct(a2, b2)) = struct(min a1 a2, min b1 b2)
let mutable x = struct(0, 0)
for i in 1..100000000 do
x <- min x (struct(i, i))
0
如果我将 CIL 反编译为 C#,我会得到以下代码:
public static int MinInt(int a, int b)
{
if (a < b)
{
return a;
}
return b;
}
public static System.ValueTuple<int, int> MinPair(System.ValueTuple<int, int> _arg2, System.ValueTuple<int, int> _arg1)
{
int b = _arg2.Item2;
int a = _arg2.Item1;
int b2 = _arg1.Item2;
int a2 = _arg1.Item1;
return new System.ValueTuple<int, int>(MinInt(a, a2), MinInt(b, b2));
}
public static void Main(string[] args)
{
System.ValueTuple<int, int> x = new System.ValueTuple<int, int>(0, 0);
for (int i = 1; i <= 100000000; i++)
{
x = MinPair(x, new System.ValueTuple<int, int>(i, i));
}
}
使用 C# 编译器重新编译只需 0.3 秒,比原始 F# 快 4 倍多。
我不明白为什么一个程序比另一个程序快得多。我什至将两个版本都反编译为 CIL,但看不出任何明显的原因。从 F# 调用 C# Min
函数会产生相同(较差)的性能。调用者内部循环的 CIL 完全相同。
谁能解释这种显着的性能差异?
最佳答案
您是否在同一架构中运行这两个示例。对于 F# 和 C# 代码,我在 x64 上得到了大约 1.4 秒,对于 F#,在 x86 上得到了大约 0.6 秒,对于 C#,在 x86 上得到了大约 0.3 秒。
正如您在反编译程序集时所说,代码看起来非常相似,但在检查 IL 代码时会出现一些差异:
F# - let min (struct(a1, b1)) (struct(a2, b2)) ...
.maxstack 5
.locals init (
[0] int32 b1,
[1] int32 a1,
[2] int32 b2,
[3] int32 a2
)
IL_0000: ldarga.s _arg2
IL_0002: ldfld !1 valuetype [System.ValueTuple]System.ValueTuple`2<int32, int32>::Item2
IL_0007: stloc.0
IL_0008: ldarga.s _arg2
IL_000a: ldfld !0 valuetype [System.ValueTuple]System.ValueTuple`2<int32, int32>::Item1
IL_000f: stloc.1
IL_0010: ldarga.s _arg1
IL_0012: ldfld !1 valuetype [System.ValueTuple]System.ValueTuple`2<int32, int32>::Item2
IL_0017: stloc.2
IL_0018: ldarga.s _arg1
IL_001a: ldfld !0 valuetype [System.ValueTuple]System.ValueTuple`2<int32, int32>::Item1
IL_001f: stloc.3
IL_0020: nop
IL_0021: ldloc.1
IL_0022: ldloc.3
IL_0023: call int32 Program::min@8(int32, int32)
IL_0028: ldloc.0
IL_0029: ldloc.2
IL_002a: call int32 Program::min@8(int32, int32)
IL_002f: newobj instance void valuetype [System.ValueTuple]System.ValueTuple`2<int32, int32>::.ctor(!0, !1)
IL_0034: ret
C# - MinPair
.maxstack 3
.locals init (
[0] int32 b,
[1] int32 b2,
[2] int32 a2
)
IL_0000: ldarg.0
IL_0001: ldfld !1 valuetype [System.ValueTuple]System.ValueTuple`2<int32, int32>::Item2
IL_0006: stloc.0
IL_0007: ldarg.0
IL_0008: ldfld !0 valuetype [System.ValueTuple]System.ValueTuple`2<int32, int32>::Item1
IL_000d: ldarg.1
IL_000e: ldfld !1 valuetype [System.ValueTuple]System.ValueTuple`2<int32, int32>::Item2
IL_0013: stloc.1
IL_0014: ldarg.1
IL_0015: ldfld !0 valuetype [System.ValueTuple]System.ValueTuple`2<int32, int32>::Item1
IL_001a: stloc.2
IL_001b: ldloc.2
IL_001c: call int32 PerfItCs.Program::MinInt(int32, int32)
IL_0021: ldloc.0
IL_0022: ldloc.1
IL_0023: call int32 PerfItCs.Program::MinInt(int32, int32)
IL_0028: newobj instance void valuetype [System.ValueTuple]System.ValueTuple`2<int32, int32>::.ctor(!0, !1)
IL_002d: ret
这里的区别在于,C# 编译器通过将中间结果压入堆栈来避免引入一些局部变量。由于无论如何都会在堆栈上分配局部变量,因此很难理解为什么这会导致更高效的代码。
其他功能非常相似。
反汇编 x86 会产生这样的结果:
F# - 循环
; F#
; struct (i, i)
01690a7e 8bce mov ecx,esi
01690a80 8bd6 mov edx,esi
; Loads x (pair) onto stack
01690a82 8d45f0 lea eax,[ebp-10h]
01690a85 83ec08 sub esp,8
01690a88 f30f7e00 movq xmm0,mmword ptr [eax]
01690a8c 660fd60424 movq mmword ptr [esp],xmm0
; Push new tuple on stack
01690a91 52 push edx
01690a92 51 push ecx
; Loads pointer to x into ecx (result will be written here)
01690a93 8d4df0 lea ecx,[ebp-10h]
; Call min
01690a96 ff15744dfe00 call dword ptr ds:[0FE4D74h]
; Increase i
01690a9c 46 inc esi
01690a9d 81fe01e1f505 cmp esi,offset FSharp_Core_ni+0x6be101 (05f5e101)
; Reached the end?
01690aa3 7cd9 jl 01690a7e
C# - 循环
; C#
; Loads x (pair) into ecx, eax
02c2057b 8d55ec lea edx,[ebp-14h]
02c2057e 8b0a mov ecx,dword ptr [edx]
02c20580 8b4204 mov eax,dword ptr [edx+4]
; new System.ValueTuple<int, int>(i, i)
02c20583 8bfe mov edi,esi
02c20585 8bd6 mov edx,esi
; Push x on stack
02c20587 50 push eax
02c20588 51 push ecx
; Push new tuple on stack
02c20589 52 push edx
02c2058a 57 push edi
; Loads pointer to x into ecx (result will be written here)
02c2058b 8d4dec lea ecx,[ebp-14h]
; Call MinPair
02c2058e ff15104d2401 call dword ptr ds:[1244D10h]
; Increase i
02c20594 46 inc esi
; Reached the end?
02c20595 81fe00e1f505 cmp esi,5F5E100h
02c2059b 7ede jle 02c2057b
很难理解为什么 F# 代码在这里的性能要差得多。除了 x
是如何加载到堆栈上的异常(exception)情况,代码看起来大致相同。直到有人想出一个很好的解释为什么我要推测它是因为 movq
比 push
有更糟糕的延迟并且因为所有指令都操纵堆栈,CPU 不能重新排序指令以减少 movq
的延迟。
为什么抖音选择movq
作为F#代码而不是C#代码我目前还不知道。
对于 x64,性能似乎变差了,因为方法前奏的开销更大,而且由于别名而造成的停顿更多。这主要是我的猜测,但很难从汇编代码中看出除了停止之外还有什么会使 x64 的性能降低 4 倍。
通过将 min
标记为内联,x64 和 x86 都在大约 0.15 秒内运行。毫不奇怪,因为这消除了方法前奏和大量读取和写入堆栈的所有开销。
将 F# 方法标记为积极内联(使用 [MethodImpl (MethodImplOptions.AggressiveInlining)]
)不起作用,因为 F# 编译器删除了所有此类属性,这意味着抖动永远不会看到它,而是标记 C# 方法积极内联使 C# 代码在 ~0.15 秒内运行。
所以最终 x86 抖动出于某种原因选择了不同的 jit 代码,即使 IL 代码看起来非常相似。方法的属性可能会影响抖动,因为它们有些不同。
x64 抖动可能可以更好地以更有效的方式将参数插入堆栈。我猜想使用 push
因为 x86 抖动比 mov
更可取,因为 push
的语义更受限制,但这只是我的猜测。
在这种情况下,当方法成本较低时,将它们标记为内联可能会很好。
老实说,我不确定这对 OP 有帮助,但希望它有点有趣。
附言。我在 i5 3570K 上的 .NET 4.6.2 上运行代码
关于c# - 结构元组的性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46354373/
#include using namespace std; class C{ private: int value; public: C(){ value = 0;
这个问题已经有答案了: What is the difference between char a[] = ?string?; and char *p = ?string?;? (8 个回答) 已关闭
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 此帖子已于 8 个月
除了调试之外,是否有任何针对 c、c++ 或 c# 的测试工具,其工作原理类似于将独立函数复制粘贴到某个文本框,然后在其他文本框中输入参数? 最佳答案 也许您会考虑单元测试。我推荐你谷歌测试和谷歌模拟
我想在第二台显示器中移动一个窗口 (HWND)。问题是我尝试了很多方法,例如将分辨率加倍或输入负值,但它永远无法将窗口放在我的第二台显示器上。 关于如何在 C/C++/c# 中执行此操作的任何线索 最
我正在寻找 C/C++/C## 中不同类型 DES 的现有实现。我的运行平台是Windows XP/Vista/7。 我正在尝试编写一个 C# 程序,它将使用 DES 算法进行加密和解密。我需要一些实
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
有没有办法强制将另一个 窗口置于顶部? 不是应用程序的窗口,而是另一个已经在系统上运行的窗口。 (Windows, C/C++/C#) 最佳答案 SetWindowPos(that_window_ha
假设您可以在 C/C++ 或 Csharp 之间做出选择,并且您打算在 Windows 和 Linux 服务器上运行同一服务器的多个实例,那么构建套接字服务器应用程序的最明智选择是什么? 最佳答案 如
你们能告诉我它们之间的区别吗? 顺便问一下,有什么叫C++库或C库的吗? 最佳答案 C++ 标准库 和 C 标准库 是 C++ 和 C 标准定义的库,提供给 C++ 和 C 程序使用。那是那些词的共同
下面的测试代码,我将输出信息放在注释中。我使用的是 gcc 4.8.5 和 Centos 7.2。 #include #include class C { public:
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我的客户将使用名为 annoucement 的结构/类与客户通信。我想我会用 C++ 编写服务器。会有很多不同的类继承annoucement。我的问题是通过网络将这些类发送给客户端 我想也许我应该使用
我在 C# 中有以下函数: public Matrix ConcatDescriptors(IList> descriptors) { int cols = descriptors[0].Co
我有一个项目要编写一个函数来对某些数据执行某些操作。我可以用 C/C++ 编写代码,但我不想与雇主共享该函数的代码。相反,我只想让他有权在他自己的代码中调用该函数。是否可以?我想到了这两种方法 - 在
我使用的是编写糟糕的第 3 方 (C/C++) Api。我从托管代码(C++/CLI)中使用它。有时会出现“访问冲突错误”。这使整个应用程序崩溃。我知道我无法处理这些错误[如果指针访问非法内存位置等,
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我有一些 C 代码,将使用 P/Invoke 从 C# 调用。我正在尝试为这个 C 函数定义一个 C# 等效项。 SomeData* DoSomething(); struct SomeData {
这个问题已经有答案了: Why are these constructs using pre and post-increment undefined behavior? (14 个回答) 已关闭 6
我是一名优秀的程序员,十分优秀!