C# 秒表只有0毫秒

C# 秒表只有0毫秒,c#,.net,stopwatch,C#,.net,Stopwatch,我有这类的快速排序和测试类。 秒表不工作,总是0毫秒 任务是实现指定的算法-将程序设计为控制台应用程序。我需要根据源数据的长度估计算法的执行时间 快速排序 public static void Sorting(int[] array, int first, int last) { int x = array[(last - first) / 2 + first]; int temp; int i = first; int j = last; while

我有这类的快速排序和测试类。 秒表不工作,总是0毫秒

任务是实现指定的算法-将程序设计为控制台应用程序。我需要根据源数据的长度估计算法的执行时间

快速排序

public static void Sorting(int[] array, int first, int last)
{
    int x = array[(last - first) / 2 + first];
    int temp;

    int i = first;
    int j = last;

    while (i <= j)
    {
        while (array[i] < x && i <= last) ++i;
        while (array[j] > x && j >= first) --j;

        if (i<=j)
        {
            temp = array[i];
            array[i] = array[j];
            array[j] = temp;
            ++i;
            --j;
        }
    }

    if (j > first)
    {
        Sorting(array, first, j);
    }

    if (i < last)
    {
        Sorting(array, i, last);
    }
}
class Program
{
    static void Main(string[] args)
    {
        Stopwatch stopwatch = new Stopwatch();
        int[] array = new int[20];

        Random random = new Random();

        for (int i=0; i<array.Length; i++)
        {
            array[i] = random.Next(1, 20);
        }

        Console.WriteLine("Sorting...");

        stopwatch.Start();

        for (int i=0; i < array.Length; i++)
        {
            QuickSort.Sorting(array, 0, array.Length - 1);
        }           

        stopwatch.Stop();            

        Console.WriteLine("\nCheck:");
        foreach (int x in array)
        {
            Console.WriteLine(x + "");
        }
        Console.WriteLine("Time: {0}ms", stopwatch.ElapsedMilliseconds);
        stopwatch.Reset();

        Console.ReadKey();

    }
}
公共静态无效排序(int[]数组,int first,int last)
{
int x=数组[(最后一个-第一个)/2+第一个];
内部温度;
int i=第一;
int j=最后一个;
而(i=first)-j;
如果(我先)
{
排序(数组,第一,j);
}
如果(i
测试

public static void Sorting(int[] array, int first, int last)
{
    int x = array[(last - first) / 2 + first];
    int temp;

    int i = first;
    int j = last;

    while (i <= j)
    {
        while (array[i] < x && i <= last) ++i;
        while (array[j] > x && j >= first) --j;

        if (i<=j)
        {
            temp = array[i];
            array[i] = array[j];
            array[j] = temp;
            ++i;
            --j;
        }
    }

    if (j > first)
    {
        Sorting(array, first, j);
    }

    if (i < last)
    {
        Sorting(array, i, last);
    }
}
class Program
{
    static void Main(string[] args)
    {
        Stopwatch stopwatch = new Stopwatch();
        int[] array = new int[20];

        Random random = new Random();

        for (int i=0; i<array.Length; i++)
        {
            array[i] = random.Next(1, 20);
        }

        Console.WriteLine("Sorting...");

        stopwatch.Start();

        for (int i=0; i < array.Length; i++)
        {
            QuickSort.Sorting(array, 0, array.Length - 1);
        }           

        stopwatch.Stop();            

        Console.WriteLine("\nCheck:");
        foreach (int x in array)
        {
            Console.WriteLine(x + "");
        }
        Console.WriteLine("Time: {0}ms", stopwatch.ElapsedMilliseconds);
        stopwatch.Reset();

        Console.ReadKey();

    }
}
类程序
{
静态void Main(字符串[]参数)
{
秒表秒表=新秒表();
int[]数组=新的int[20];
随机=新随机();

对于(int i=0;i,由于您的输入长度只有20个元素,因此几乎不需要时间对其进行排序。请尝试使用更大的输入,或者尝试查找刻度而不是ms。

如果您使用
已用时间
而不是
已用时间毫秒
,您将得到如下结果:

Time: 00:00:00.0004201ms
对这么小的数组进行排序甚至不需要1毫秒。事实上,我怀疑写入控制台或可能的线程切换会对时间产生更大的影响

使用200个项目返回:

Time: 00:00:00.0023507ms

每次执行都会产生不同的结果,因为快速排序对元素的相对顺序很敏感。线程切换、垃圾收集和其他正在运行的进程也会影响您获得的值

选择2000个项目会产生大约210-220毫秒的结果。更为一致,但5%的偏差仍然太大

如果您真的想对代码进行基准测试,至少需要对其进行多次测试,并对结果进行平均


一个更好的方法是使用BenchmarkDotNet,让它运行代码足够长,直到它得到稳定的结果。

你是否认为你的代码是在不到1毫秒的范围内执行的?对于使用BaskMax DOTNET考虑的基准:秒表是非常原始的基准?我必须使用更大的数组吗?大得多。试试100000 E之类的东西。元素。