相关疑难解决方法(0)

C#vs C - 性能差异很大

我发现C anc C#中类似代码之间存在巨大的性能差异.

C代码是:

#include <stdio.h>
#include <time.h>
#include <math.h>

main()
{
    int i;
    double root;

    clock_t start = clock();
    for (i = 0 ; i <= 100000000; i++){
        root = sqrt(i);
    }
    printf("Time elapsed: %f\n", ((double)clock() - start) / CLOCKS_PER_SEC);   

}
Run Code Online (Sandbox Code Playgroud)

而C#(控制台应用程序)是:

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            DateTime startTime = DateTime.Now;
            double root;
            for (int i = 0; i <= 100000000; i++)
            {
                root = Math.Sqrt(i);
            } …
Run Code Online (Sandbox Code Playgroud)

c c# performance

92
推荐指数
10
解决办法
8万
查看次数

Math.Sqrt()的时间复杂度?

我怎样才能找到这个功能的复杂性?

private double EuclideanDistance(MFCC.MFCCFrame vec1, MFCC.MFCCFrame vec2)
{
  double Distance = 0.0;
  for (int K = 0; K < 13; K++)
     Distance += (vec1.Features[K] - vec2.Features[K]) * (vec1.Features[K] - vec2.Features[K]);
  return Math.Sqrt(Distance);
}
Run Code Online (Sandbox Code Playgroud)

我知道下面的部分是O(1):

double Distance = 0.0;
for (int K = 0; K < 13; K++)
   Distance += (vec1.Features[K]-vec2.Features[K])*(vec1.Features[K]-vec2.Features[K]);
Run Code Online (Sandbox Code Playgroud)

但我无法弄清楚复杂性Math.Sqrt()是什么.

c# big-o time-complexity math.sqrt

7
推荐指数
2
解决办法
1227
查看次数

标签 统计

c# ×2

big-o ×1

c ×1

math.sqrt ×1

performance ×1

time-complexity ×1