.NET函数调用(C#F#)VS C的性能

前端之家收集整理的这篇文章主要介绍了.NET函数调用(C#F#)VS C的性能前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
由于F#2.0已经成为VS2010的一部分,我对F#感兴趣.我想知道使用它的意义是什么.我读了一下,我做了一个基准测量函数调用.
我使用了Ackermann的功能:)

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,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." << "\n\n";
 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,n - 1));
 }
 return 0;
}

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

那时
C#= 510ms
c = 130ms
F#= 185ms
Java = Stackoverflow

猜你在找的C&C++相关文章