这是为什么F#code这么慢? [英] Why is this F# code so slow?

查看:119
本文介绍了这是为什么F#code这么慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#和F#A莱文斯坦实现。 C#的版本是约1500个字符的两个字符串快10倍。 C#:69毫秒,F#867毫秒。为什么?据我所知,他们做同样的事情?如果是发布或调试版本没有关系。

编辑:如果有人来这里专为编辑距离实现来看,它坏了。工作code是<一个href=\"https://bitbucket.org/vgrit/clrsquirrel/src/82840c2ce95b/ClrSquirrel/ClrSquirrel.fs\">here.

C#

私有静态诠释MIN3(INT A,INT B,INT C)
{
   返回Math.Min(Math.Min(A,B),C);
}公共静态INT EditDistance(串M,串N)
{
   VAR D1 =新的INT [n.Length]
   为(中间体X = 0; X&下; d1.Length; X ++)D1 [X] = X;
   VAR D0 = INT新[n.Length]
   的for(int i = 1; I&LT; m.Length;我++)
   {
      D0 [0] = I;
      VAR UI = M [];
      对于(INT J = 1; J&LT; n.Length; J ++)
      {
         D0 [J] = 1 + MIN3(D1 [J],D0 [J - 1],D1 [J - 1] +(UI == N [J] -1:0));
      }
      Array.Copy(D0,D1,d1.Length);
   }
   返回D0 [n.Length - 1];
}

F#

让MIN3(A,B,C)=分钟(分B C)让莱文斯坦(M:字符串)(N:字符串)=
   让D1 = Array.init n.Length ID
   让D0 = Array.create n.Length 0
   对于i = 1到m.Length-1做
      。D0 [0]&LT; - 我
      让UI = M。[I]
      对于j = 1至n.Length-1做
         。D0 [J]&LT; - 1 + MIN3(。。D1 [J],D0 [J-1],D1 [J-1] +如果UI = N [J]。然后-1否则为0)
      Array.blit D0 0 0 D1 n.Length
   D0 [n.Length-1]


解决方案

的问题是, MIN3 函数编译为使用通用的比较通用的功能(我想这仅使用 IComparable的,但它实际上是更为复杂 - 它会使用结构比较F#的类型,它是相当复杂的逻辑)

&GT;让MIN3(A,B,C)=分钟(分B C);;
VAL MIN3:A * A * A - &GT; 当一个'A:比较

在C#版本,功能不是一般的(它只是需要 INT )。

:您可以通过添加类型注释(以获得同样的事情在C#)提高了F#版本

让MIN3(A:INT,B,C)=分钟(分B C)

...或者通过 MIN3 在线(在这种情况下,将专门为 INT 使用时):

让内联MIN3(A,B,C)=分钟(分B C);;

对于一个随机字符串 STR 长度300的,我得到以下数字:

&GT;莱文斯坦STR(富+ STR);;
房地产:00:00:03.938,CPU:00:00:03.900,GC gen0:275,第一代:1,第2代:0
VAL它:INT = 3&GT; levenshtein_inlined STR(富+ STR);;
房地产:00:00:00.068,CPU:00:00:00.078,GC gen0:0,第1代:0,第2代:0
VAL它:INT = 3

A Levenshtein implementation in C# and F#. The C# version is 10 times faster for two strings of about 1500 chars. C#: 69 ms, F# 867 ms. Why? As far as I can tell, they do the exact same thing? Doesn't matter if it is a Release or a Debug build.

EDIT: If anyone comes here looking specifically for the Edit Distance implementation, it is broken. Working code is here.

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++ )
      {
         d0[j] = 1 + min3(d1[j], d0[j - 1], d1[j - 1] + (ui == n[j] ? -1 : 0));
      }
      Array.Copy(d0, d1, d1.Length);
   }
   return d0[n.Length - 1];
}

F#:

let min3(a, b, c) = min a (min b c)

let levenshtein (m:string) (n:string) =
   let d1 = Array.init n.Length id
   let d0 = Array.create n.Length 0
   for i=1 to m.Length-1 do
      d0.[0] <- i
      let ui = m.[i]
      for j=1 to n.Length-1 do
         d0.[j] <- 1 + min3(d1.[j], d0.[j-1], d1.[j-1] + if ui = n.[j] then -1 else 0)
      Array.blit d0 0 d1 0 n.Length
   d0.[n.Length-1]

解决方案

The problem is that the min3 function is compiled as a generic function that uses generic comparison (I thought this uses just IComparable, but it is actually more complicated - it would use structural comparison for F# types and it's fairly complex logic).

> let min3(a, b, c) = min a (min b c);;
val min3 : 'a * 'a * 'a -> 'a when 'a : comparison

In the C# version, the function is not generic (it just takes int). You can improve the F# version by adding type annotations (to get the same thing as in C#):

let min3(a:int, b, c) = min a (min b c)

...or by making min3 as inline (in which case, it will be specialized to int when used):

let inline min3(a, b, c) = min a (min b c);;

For a random string str of length 300, I get the following numbers:

> levenshtein str ("foo" + str);;
Real: 00:00:03.938, CPU: 00:00:03.900, GC gen0: 275, gen1: 1, gen2: 0
val it : int = 3

> levenshtein_inlined str ("foo" + str);;
Real: 00:00:00.068, CPU: 00:00:00.078, GC gen0: 0, gen1: 0, gen2: 0
val it : int = 3

这篇关于这是为什么F#code这么慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆