1# Importing some sample bogosort implementations downloaded from around Siafoo ### 2fromvanilla_bogosortimportbogo_sortasvanilla_bogosort 3fromnumpy_bogosortimportbogosortasnumpy_bogosort 4 5# Regular imports 6importnumpy 7fromtimeimporttime 8fromrandomimportshuffle 9frompprintimportpprint10fromrandomimportshuffle1112deftest_bogos(sequence_of_bogosort_functions,number_of_tests,maximum_array_size):13''' Times a sequences of bogosort functions for a series of increasingly large14 arrays, randomly scrambled, repeated a set number of times.1516 Returns a 3d numpy array containing the time-to-run data for all functions17 Rows are test iterations, columns are array-sizes, and depth corresponds to18 function used.19 '''2021num_functions=len(sequence_of_bogosort_functions)22times_array=numpy.atleast_3d(numpy.zeros((number_of_tests,maximum_array_size,num_functions)))2324for(t_num,s_num,f_num),xinnumpy.ndenumerate(times_array):2526fun=sequence_of_bogosort_functions[f_num]27a_list=range(s_num)28shuffle(a_list)2930start_time=time()31sorted_array=fun(a_list)32end_time=time()3334times_array[t_num,s_num,f_num]=end_time-start_time3536returntimes_array3738if__name__=='__main__':3940fun_seq=[vanilla_bogosort,numpy_bogosort]41num_tests=100042max_size=943times_arr=test_bogos_02(fun_seq,num_tests,max_size)44print'Average Times',times_arr.mean(axis=0)

Test a sequence of bogosort functions on a series of scrambled linear arrays of ascending maximum size. Repeat the tests a given number of times and return a 3-d array of timing information.

For the main example shown above, I got the following results. They generally demonstrate that the numpy code is faster than the generic python for larger arrays. Surprisingly, the difference was not terribly great. I'd wager that they become more distinct at larger array sizes.

Below, the first column is the vanilla python, and the second column is the numpy implementation. The rows correspond to array size, ranging from 0 to 8.