您如何运行和比较两种排序算法,看看哪种算法最先完成? [英] How do you run and compare two sorting algorithms and see which one finishes first?

查看:106
本文介绍了您如何运行和比较两种排序算法,看看哪种算法最先完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否需要并行处理?

推荐答案

使用秒表类运行一种算法,然后运行另一种算法。

Use Stopwatch class to run one algorithm and then run the other one.

Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();

// Code for your algorithm    

stopWatch.Stop();
// Get the elapsed time as a TimeSpan value.
TimeSpan ts = stopWatch.Elapsed;

// Format and display the TimeSpan value.
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
    ts.Hours, ts.Minutes, ts.Seconds,
    ts.Milliseconds / 10);

请确保您使用大量输入来测试算法。在小集合上执行比其他算法更快的排序算法,不一定在大集合上执行更快。

Make sure you test your algorithm with numerous size inputs. A sorting algorithm which performs faster than another algorithm on small set may not necessarily perform faster on a larger set.

这篇关于您如何运行和比较两种排序算法,看看哪种算法最先完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆