Levenshtein在C#和F#中的实现.对于两个大约1500个字符的字符串,C#版本快10倍.C#:69 ms,F#867 ms.为什么?据我所知,他们完全一样吗?无论是Release还是Debug构建都无关紧要.
编辑:如果有人来这里专门寻找编辑距离实施,它就会被打破.工作代码在这里.
C#:
private static int min3(int a, int b, int c)
{
return Math.Min(Math.Min(a, b), c);
}
public static int EditDistance(string m, string n)
{
var d1 = new int[n.Length];
for (int x = 0; x < d1.Length; x++) d1[x] = x;
var d0 = new int[n.Length];
for(int i = 1; i < m.Length; i++)
{
d0[0] = i;
var ui = m[i];
for (int j = 1; j < n.Length; j++ )
{ …Run Code Online (Sandbox Code Playgroud) 我正在浏览我的一个程序集的某些IL(通过ILDasm),我注意到我的所有方法都以一条 指令开头nop .
有谁知道那是为什么?
我在VS2010 beta2中玩F#,因为我是F#的新手,所以我选择了一个常见的例子并继续实现了一个阶乘函数:
let rec factorial n =
if n <= 1 then 1 else n * factorial (n - 1);;
Run Code Online (Sandbox Code Playgroud)
如果我构建它并查看Reflector中生成的代码,我会得到相应的C#代码:
public static int Factorial(int n) {
if (n <= 1)
return 1;
return n * Factorial(n - 1);
}
Run Code Online (Sandbox Code Playgroud)
因此,如果我编译Reflector的F#代码的C#表示,我希望得到相同的IL.
但是,如果我在发布模式下编译这两个片段并比较生成的IL,它们是不同的(它们在功能上是相同的,但仍然有所不同).
C#实现编译为:
.method public hidebysig static int32 Factorial(int32 n) cil managed
{
.maxstack 8
L_0000: ldarg.0
L_0001: ldc.i4.1
L_0002: bgt.s L_0006
L_0004: ldc.i4.1
L_0005: ret
L_0006: ldarg.0
L_0007: ldarg.0
L_0008: ldc.i4.1
L_0009: sub
L_000a: call int32 TestApp.Program::Factorial(int32)
L_000f: mul …Run Code Online (Sandbox Code Playgroud)