javascript - 为什么 C# 中的冒泡排序对我来说比 JavaScript 慢?

标签 javascript c# sorting bubble-sort

我是 C# 新手,一直在尝试使用它。我在 C# 中实现了冒泡排序,希望它比 JavaScript 更快,因为它是一种编译语言,但速度却慢得多。

我正在对 100,000 个数字进行排序。

在 C# 中,我得到的速度约为:1 分 30 秒

C# 代码:

using System.Diagnostics;

public class Program {

    public static void Main(string[] args) {

        List<int> randoms = generateRandoms(0, 1000000, 100000);
        Console.WriteLine(randoms.Count);

        Stopwatch stopWatch = new Stopwatch();
        
        stopWatch.Start();
        bubbleSort(randoms);
        stopWatch.Stop();
        TimeSpan timeSpan = stopWatch.Elapsed;
        Console.WriteLine("Total processing time... {0:00}:{1:00}:{2:00}.{3:000}", timeSpan.Hours, timeSpan.Minutes, timeSpan.Seconds, timeSpan.Milliseconds);
        Console.Read();
    }

    static List<int> generateRandoms(int min, int max, int amount) {

        Random rnd = new Random();
        List<int> randoms = new List<int>();

        for (int i = 0; i < amount; i++) {
            int r = rnd.Next(min, max+1);
            randoms.Add(r);
        }

        return randoms;
    }


    static List<int> bubbleSort(List<int> list) {
        //Bubble sort
        for (int i = 0; i < list.Count; i++) {
            for (int j = 0; j < list.Count - i - 1; j++) {
                if (list[j] > list[j+1]) {
                    int temp = list[j];
                    list[j] = list[j+1];
                    list[j+1] = temp;
                }
            }
        }

        return list;
    }


    static void print(List<int> list) {
        for (int i = 0; i < list.Count; i++) {
            Console.WriteLine(list[i]);
        }
    }
}

在 JavaScript 中,我大约需要:30 秒

JavaScript 代码:

function bubbleSort(array) {

    for (let i = 0; i < array.length; i++) {
        for (let j = 0; j < array.length-i-1; j++) {

            if (array[j] > array[j+1]) {
                [array[j], array[j+1]] = [array[j+1], array[j]];
            }
        }
    }
}

function generateRandoms(min, max, n) {
    const arr = [];
    for (let i = 0; i < n; i++) {
        arr.push(Math.floor(Math.random() * (max-min+1) + min));
    }

    return arr;
}

const array = generateRandoms(0, 1000000, 100000);
console.log(array.length);

const start = new Date();
bubbleSort(array);
const end = new Date();

console.log(`Performance: ${end - start}ms`);

我认为这与 C# 中的“列表”数据结构有关,但在查看文档后,我在 bubbleSort 函数中使用的所有操作似乎都是 O(1)

有人知道为什么 C# 的速度对我来说要差很多吗? 我正在使用.Net v6.0.201。我还在这两个程序中使用 VSCode。

最佳答案

发布模式下构建时,C# 与 JS 一样快,甚至快一点。至少在我的电脑上是这样。

总处理时间... 00:01:00.611:调试箱

总处理时间...00:00:19.586:释放bin

关于javascript - 为什么 C# 中的冒泡排序对我来说比 JavaScript 慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71446251/

相关文章:

c++ - 在 C++11 中使用 std::sort 和 lambda 函数对动态分配的多维 C 数组进行排序

javascript - 为什么谷歌网站管理员工具看不到我网站的静态版本,而是动态版本的模板?

javascript - jQuery Mobile 多重过滤方法 - 文本/输入和选择/下拉菜单

javascript - 如何使用 Express 跨端口/域共享 cookie

c# - 什么时候应该防范null?

c# - 在 Roslyn 中自动加载 .csx

json - 将二维数组展开为树状布局

JavaScript 插件,用于查找纯文本中的图像链接并将它们转换为 Angular js 中的 HTML <img>

c# - XML 到 Excel (2007) 使用 Windows XP 和 C#.Net 的想法

作为数组和字典的对象的 JavaScript 排序运行时