我正在使用timeit模块测量基数和计数排序的执行时间。我使用的是位于<0;1000000>区间上的100组随机整数。集合中的所有整数都是唯一的。第一组由10000个整数组成,最后一组包含1000000个整数。每组排序10次,记录平均时间(作为full time/10)。在基排序的日志文件中有一些奇怪的结果,我不确定这是timeit模块的问题还是我的排序算法的问题:
基排序日志
integers count, average time
......,.............
760000,1.51444417528
770000,1.31519716697
780000,1.33663102559
790000,1.3484539343
800000,1.37114722616
810000,1.61706798722
820000,1.4034960851
830000,1.65582925635
840000,1.68017826977
850000,1.69828582262
860000,1.47601140561
870000,1.73875506661
880000,1.75641094733
890000,1.54894320189
900000,1.80121665926
910000,1.56070168632
920000,1.8451221867
930000,1.8612749805
940000,1.61202779665
950000,1.63757506657
960000,1.64939744866
970000,1.66534313097
980000,1.68155078196
990000,1.69781920007
1000000,2.00389959994您可以看到,比以前更大的集合排序有时花费更少的时间。在计数排序的情况下,时间通常在增加。
这是我的基类代码:
from __future__ import division
def sortIntegerList (listToSort, base):
maxkey = len(str(max(listToSort)))
for i in range(maxkey):
bucketList = [[] for x in range(base)]
for number in listToSort:
bucketList[(number//base**i) % base].append(number)
listToSort = []
for l in bucketList:
listToSort.extend(l)
return listToSort这是我的计数排序代码:
def sortIntegerList (listToSort):
maxkey = max(listToSort)
countingList = [0 for x in range(maxkey + 1)]
for i in listToSort:
countingList[i] += 1
for i in range(1, len(countingList)):
countingList[i] += countingList[i-1]
sortedList = [0 for x in range(len(listToSort) + 1)]
for i in listToSort:
sortedList[countingList[i]] = i
countingList[i] -= 1
del sortedList[0]
return sortedList下面是用于测量执行时间的代码:
import timeit
outputFileCounting = "count,time\n"
outputFileRadix = "count,time\n"
# Counting Sort
for x in range(10, 1001, 10):
setup_counting = """
from sorters import counting_sort
import cPickle
with open("ri_0-1000k_{0}k.pickle", mode="rb") as f:
integerList = cPickle.load(f)
""".format(x)
time_counting = timeit.timeit("""counting_sort.sortIntegerList(integerList)""",
setup = setup_counting, number=10)
outputFileCounting += "{0},{1}\n".format(str(x*1000), time_counting/10)
with open("sort_integer_counting_results.csv", mode="w") as f:
f.write(outputFileCounting)
# Radix Sort
for x in range(10, 1001, 10):
setup_radix = """
from sorters import radix_sort
import cPickle
with open("ri_0-1000k_{0}k.pickle", mode="rb") as f:
integerList = cPickle.load(f)
""".format(x)
time_radix = timeit.timeit("""radix_sort.sortIntegerList(integerList, 10)""",
setup = setup_radix, number=10)
outputFileRadix += "{0},{1}\n".format(str(x*1000), time_radix/10)
with open("sort_integer_radix_results.csv", mode="w") as f:
f.write(outputFileRadix)每个整数集都作为一个列表存储在pickle文件中。
发布于 2014-12-28 15:53:31
你的基数排序做了很多分配和重新分配内存。我想知道,也许这就是问题所在。如果您只为数据结构分配了一次内存,并且接受了需要过度分配的事实,该怎么办?
除此之外,您是否检查过是否确定最终列表是真正排序的?你有没有看过你的基数排序(即min/max/中位数)的其他统计数据,也许偶尔会有异常值,调查它们可以帮助你解释事情。
https://stackoverflow.com/questions/27677448
复制相似问题