如何在时间和空间的所有排序方法中使用Time()函数 [英] How Do I Use Time() Function In Comparision Of All Sorting Mehods In Terms Of Time And Space

查看:92
本文介绍了如何在时间和空间的所有排序方法中使用Time()函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用不同的函数进行排序,比如冒泡排序..现在如何比较所有方法的时间复杂度。我必须在每个函数中包含time()函数?

I have use different function for sorting like bubble sort..etc now how compare time complexity for all method.i have to include time()function in every function?

推荐答案

我不想这么说,但这里没有简单的解决方案 - 执行测量是一个充满挑战的过程! :笑:



首先要注意的是Time可能无法帮助你 - 它会返回一个以秒为单位的值 - 除非你有一个非常非常慢的PC不会准确到足以给你带来任何有意义的结果。你需要查看特定系统的非标准时序库,找到一个更精细的ghranularity - 时钟,毫秒或更好的微秒。



其次,你将遇到问题,因为为了获得任何准确的测量,你需要多次运行函数 - 至少数千个 - 以消除由缓存,其他线程,内存分配,等等。这是一个排序函数的问题,因为它的目的是重新排列数据 - 所以你需要每次都重新生成数据,并消除你的时间的开销。



第三,你仍然会遇到问题,因为对于已经有一些订单的数据,一些排序方法会更加高效。具有排序数据的冒泡排序将尽可能快,而具有反向排序数据的气泡将尽可能慢!所以你可能需要考虑一系列数据集来展示这些功能。



祝你好运!
I hate to say it, but there is no simple solution here to give you - execution measurement is a fraught process! :laugh:

The first thing to note is that Time is probably not going to help you - it returns a value which is measured in seconds - which unless you have a very, very slow PC is not going to be anywhere near accurate enough to give you any meaningful results. You need to look at the non-standard timing libraries for your specific system to find one that works to a much finer ghranularity - ticks, milliseconds, or better microseconds.

Secondly, you are going to have problems here, because in order to get any accurate measurements, you need to run the function(s) many times - thousands at the least - to eliminate any variations caused by caching, other threads, memory allocation, and so forth. And that's a problem with a sort function, because it's purpose is to rearrange the data - so you will need to regenerate the data each time, and eliminate the overhead of that from your timings.

Thirdly, you are still going to have problems because some sorting methods are much, much more efficient with data which has some order already. A bubble sort with sorted data will be about as quick as it is possible to be, while a bubble with reverse sorted data will be about as slow as possible! So you probably need to think about a range of datasets to present the functions as well.

Good luck!


OriginalGriff说关于这个问题的明智之举。



BTW在windows下你可以通过使用
OriginalGriff said wise words about the issue.

BTW under windows you can give a try to the high resolution time stamps through the use of the
QueryPerformanceCounter (QPC)





你可以在这里找到样本其中 [ ^ ]


这篇关于如何在时间和空间的所有排序方法中使用Time()函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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