**Explain yourself**with reStructured Text, embedded code, graphs, charts, and LaTeX–style math. Join Siafoo Now or Learn More

# Bogosort Implementation Comparison Testing 0

In Brief | 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.... more |

Language | NumPy |

` 1# Importing some sample bogosort implementations downloaded from around Siafoo ###`

2from vanilla_bogosort import bogo_sort as vanilla_bogosort

3from numpy_bogosort import bogosort as numpy_bogosort

4

5# Regular imports

6import numpy

7from time import time

8from random import shuffle

9from pprint import pprint

10from random import shuffle

11

12def test_bogos(sequence_of_bogosort_functions,number_of_tests,maximum_array_size):

13 ''' Times a sequences of bogosort functions for a series of increasingly large

14 arrays, randomly scrambled, repeated a set number of times.

15

16 Returns a 3d numpy array containing the time-to-run data for all functions

17 Rows are test iterations, columns are array-sizes, and depth corresponds to

18 function used.

19 '''

20

21 num_functions = len(sequence_of_bogosort_functions)

22 times_array = numpy.atleast_3d(numpy.zeros((number_of_tests,maximum_array_size,num_functions)))

23

24 for (t_num,s_num,f_num), x in numpy.ndenumerate(times_array):

25

26 fun = sequence_of_bogosort_functions[f_num]

27 a_list = range(s_num)

28 shuffle(a_list)

29

30 start_time = time()

31 sorted_array = fun(a_list)

32 end_time = time()

33

34 times_array[t_num,s_num,f_num] = end_time - start_time

35

36 return times_array

37

38if __name__ == '__main__':

39

40 fun_seq = [vanilla_bogosort,numpy_bogosort]

41 num_tests = 1000

42 max_size = 9

43 times_arr = test_bogos_02(fun_seq,num_tests,max_size)

44 print '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.

`1`

- Average Times
[[ 5.05232811e-06 2.64606476e-05]

[ 3.69405746e-06 1.90448761e-05]

[ 1.17156506e-05 3.12585831e-05]

[ 4.38842773e-05 6.61971569e-05]

[ 2.15771198e-04 2.37778425e-04]

[ 1.27175498e-03 1.23753619e-03]

[ 8.11699867e-03 7.92551494e-03]

[ 6.16261077e-02 5.91173282e-02]

[ 5.82711076e-01 5.04583259e-01]]

## Comments