English 中文(简体)
网上功能(C# F#)VS C++的性能
原标题:Performance of .Net function calling (C# F#) VS C++

Since F# 2.0 has become a part of VS2010 I take an interest in F#. I wondered what s the point of using it. I d read a bit and I made a benchmark to measure functions calling. I have used Ackermann s function :)

C#

sealed class Program
{
    public static int ackermann(int m, int n)
    {
        if (m == 0)
            return n + 1;
        if (m > 0 && n == 0)
        {
            return ackermann(m - 1, 1);
        }
        if (m > 0 && n > 0)
        {
            return ackermann(m - 1, ackermann(m, n - 1));
        }
        return 0;
    }

    static void Main(string[] args)
    {

        Stopwatch stopWatch = new Stopwatch();

        stopWatch.Start();
        Console.WriteLine("C# ackermann(3,10) = " + Program.ackermann(3, 10));
        stopWatch.Stop();

        Console.WriteLine("Time required for execution: " + stopWatch.ElapsedMilliseconds + "ms");
        Console.ReadLine();
    }
}

C++

class Program{
public:
static inline int ackermann(int m, int n)
{
  if(m == 0)
       return n + 1;
  if (m > 0 && n == 0)
  {
      return ackermann(m - 1, 1);
  }
  if (m > 0 && n > 0)
  {
      return ackermann(m - 1, ackermann(m, n - 1));
  }
  return 0;
 }
};

int _tmain(int argc, _TCHAR* argv[])
{
 clock_t start, end;

  start = clock();
 std::cout << "CPP: ackermann(3,10) = " << Program::ackermann(3, 10) << std::endl;
 end = clock();
 std::cout << "Time required for execution: " << (end-start) << " ms." << "

";
 int i;
 std::cin >> i;
 return 0;
}

F#

// Ackermann
let rec ackermann m n  =
  if m = 0 then n + 1
  elif m > 0 && n = 0 then ackermann (m - 1) 1
  elif m > 0 && n > 0 then ackermann (m - 1)  (ackermann m (n - 1))
  else 0

open System.Diagnostics;
let stopWatch = Stopwatch.StartNew()
let x = ackermann 3 10 
stopWatch.Stop();

printfn "F# ackermann(3,10) = %d"  x
printfn "Time required for execution: %f"  stopWatch.Elapsed.TotalMilliseconds

Java

public class Main 
{
 public static int ackermann(int m, int n)
 {
 if (m==0) 
   return n + 1;
if (m>0 && n==0)
{
 return ackermann(m - 1,1);
}
if (m>0 && n>0)
{
  return ackermann(m - 1,ackermann(m,n - 1));
 }
 return 0;
}

  public static void main(String[] args)
  { 
   System.out.println(Main.ackermann(3,10));
  }
}

An then
C# = 510ms
c++ = 130ms
F# = 185ms
Java = Stackoverflow :)

是F#(只有少量代码)的力量。 如果我们想要使用的话。 净执行额和得更快执行额? Can 优化这些法典(特别是F#)?

<>UPDATE。 我离开科索尔。 书写和操作C#代码,但无改动:C#=400ms

最佳回答

我认为,在这种情况下,C#与F#之间的区别是由于细调优化。

详细情况是,当你有回馈呼吁时,这种呼吁是“最后一件事”。 在本案中,F#的确产生了一项呼吁指示,但将守则编纂成一种循环(因为这一呼吁是“最后的东西”,我们可以重新利用目前的轮).框架,而只是跳跃到方法的开始。

在你执行<代码>ackermann时,其中2个是尾声,其中只有1个不是(结果作为向另一个<代码>ackermann<>>/code>的争论通过)。 这意味着F#版本实际上援引的是“召唤”指示(情况?)较少。

总而言之,业绩概况与C#的业绩表现大致相同。 与F# vs有关的几个员额。 C# 业绩:

问题回答

这一问题并不直接相关,但引人注意:尝试这一版本

let rec ackermann2 m n  =
  match m,n with
  | 0,0 -> 0
  | 0,n -> n+1
  | m,0 -> ackermann2 (m-1) 1
  | m,n -> ackermann2 (m-1) (ackermann2 m (n-1))

在我的个人计算机(VS2010年,F#互动)中,它比你在计算cker人312时的版本高出近50%。

我很难解释为什么会出现这种业绩差异。 我猜测,它与F#汇编者做些什么,把配对的表述转化为开关声明,而不是发表一系列声明。 先进行(m=0,n=0)检测,也可能有所帮助。

就F#而言,你可能希望尝试一条关键词。

此外,正如前面提到的,C#和F#版本因科索尔不同。 书面发言。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签