C# 和python性能比较(排序算法为例)

C# 和python性能比较(排序算法为例)

1,环境

C#: .net core6.0
python: python3.7

2,任务

排序长度为200的整数列

3, C#代码及时间

// sort.cs
using System;
using System.Collections.Generic;
using System.Linq;


using System.Text;
using System.Threading.Tasks;


namespace Learn3
{
    public class Solution
    {
        public static List<int> Solve1(List<int> l)
        {
            List<int> li = new();
            li.Add(l[0]);
            for(int i=1; i<l.Count;i++)
            {

                Insert_List(l[i], li); 
            }
            return li;
        }
        public static int Find_index(int a, List<int> l) // 寻找索引, 前提是l 有序.
        {
            
            if (l[0] >= a) { return 0; }
            int x = -1, y = l.Count();
            int z;
            while ((y-x)>1)
            {
                z = (int)(x + y) / 2;
                if( a > l[z]) { x = z; }
                else if ( a < l[z]) { y = z; } 
                else
                {
                    return z;
                }
            }
            
            return (int)(x+y)/2 + 1;
        }
        
        public static List<int> Random_List(int n, int m=100)
        {
            Random r = new();
            List<int> l = new();
            while(n !=0)
            {
                l.Add(r.Next(0, m));
                n -= 1;
            }
            return l;
        }
        public static void Insert_List(int n, List<int> l)
        {
            l.Insert(Solution.Find_index(n, l), n);
        }
    }
}

// program.cs

using System.Diagnostics;

namespace Learn3
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Stopwatch s = new();
            
            var l = Solution.Random_List(200, 500);
        
            l.ForEach(Console.WriteLine);
            Console.WriteLine();
            
            Console.WriteLine();
            
            
            s.Start();
            Solution.Solve1(l).ForEach(Console.WriteLine);
            // l.Sort();
            // l.ForEach(Console.WriteLine);
            // 大家可以试一下直接用l.sort()的时间,其实差不多.
            s.Stop();
            Console.WriteLine($"time:  {s.Elapsed.Milliseconds}  ms");
        }
        
    }
}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
平均大概237毫秒

python代码及时间

import random
import time


def index(n:int, l:list):
    a = -1; b = len(l);
    while (b-a)>1:
        c = int((a+b)/2);
        if (l[c] > n):
            b = c
        elif (l[c]<n):
            a = c
        else:
            return c
    return int((a+b)/2)+1

def solve(l:list):
    
    li = [-1, ];
    i = 0;
    while (i<len(l)):
        #print(index(l[i], li))
        li.insert(index(l[i], li), l[i])
        i+=1
    li.remove(-1)
    return li

def randomlist(n, m):
    l= [];
    for i in range(0,n):
        l.append(random.randint(0, m))
    return l;

l = randomlist(200, 500)
print(l)
t = time.time()
print(solve(l))
print(f"time:{time.time() - t}")


时间情况:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
平均 535毫秒.

结论

C# “略胜一筹”.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值