我正在使用timeit
模块测量Radix和Counting排序的执行时间。我正在使用100组随机整数,位于区间<0;百万取代。所有整数在集合中都是唯一的。第一组由10000个整数组成,最后设置为1000000个整数。每组排序十次,记录平均时间(full time/10
)。在Radix排序的日志文件中有一些奇怪的结果,我不确定它是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
您可以看到,比以前更大的集合排序有时会花费更少的时间。如果计数排序正常时间会增加。
以下是我的Radix排序代码:
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
这是我的Counting Sort代码:
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
文件中。
答案 0 :(得分:0)
你的基数排序会对内存进行大量的分配和重新分配。我想知道这可能是问题。如果您只为数据结构分配了一次内存,并接受了需要过度分配的事实,那该怎么办呢?
除此之外,您是否检查过确定最终列表是否真正排序?您是否查看过基数排序的其他统计数据(即最小/最大/中位数)时间,也许偶尔有异常值,调查它们可以帮助您解释事情。