def test_amortization(self): test_stack = StackConstTimeMaxEl() for element in get_random_array_of_ints(100000, -10000, 10000): test_stack.push(element) _, time_hundred_thousand = get_execution_time(test_stack.get_max) for element in get_random_array_of_ints(100, -10000, 10000): test_stack.push(element) _, time_hundred = get_execution_time(test_stack.get_max) self.assertTrue(time_hundred_thousand / time_hundred < 30.0)
def test_amortization(self): q = QueueConstTimeMaxEl() for element in get_random_array_of_ints(100000, -10000, 10000): q.enqueue(element) _, time_hundred_thousand = get_execution_time(q.get_max) for element in get_random_array_of_ints(100, -10000, 10000): q.enqueue(element) _, time_hundred = get_execution_time(q.get_max) self.assertTrue(time_hundred_thousand/time_hundred < 30.0)
def test_find_median_of_two_sorted_lists_random(self): test_list_1 = sorted(get_random_array_of_ints(1000, -1000, 1000)) test_list_2 = sorted(get_random_array_of_ints(1000, -10000, 10000)) self.assertEqual( nlogn_function(test_list_1, test_list_2), find_median_of_two_sorted_lists(test_list_1, test_list_2)) test_list_1 = sorted(get_random_array_of_ints(1000, 0, 1000)) test_list_2 = sorted(get_random_array_of_ints(1001, -10000, 1000)) self.assertEqual( nlogn_function(test_list_1, test_list_2), find_median_of_two_sorted_lists(test_list_1, test_list_2))
def test_performance(self): perf_test_array_1 = sorted(get_random_array_of_ints( 100, -10000, 10000)) perf_test_array_2 = sorted(get_random_array_of_ints( 100, -10000, 10000)) _, time_logn_1 = get_execution_time(find_median_of_two_sorted_lists, perf_test_array_1, perf_test_array_2) perf_test_array_1 = sorted( get_random_array_of_ints(1000000, -100000, 100000)) perf_test_array_2 = sorted( get_random_array_of_ints(1000000, -100000, 100000)) _, time_logn_2 = get_execution_time(find_median_of_two_sorted_lists, perf_test_array_1, perf_test_array_2) # confirm that it is at least < O(n) self.assertLess(time_logn_2 / time_logn_1, 5000)
def setUp(self) -> None: self.random_test_array = get_random_array_of_ints(100, -100, 100) self.duplicates_array = [1, 1, 1, 1] self.duplicates_of_two_types = [-1, 0, -1, 0, -1] self.empty_array = [] self.test_set = ( self.duplicates_array, self.duplicates_of_two_types, self.empty_array, self.random_test_array ) self.sorting_functions = ( quick_sort, shell_sort, merge_sort, counting_sort, radix_sort, heap_sort, merge_sort_multi_adapted )
def compete(): sorting_functions = (quick_sort, shell_sort, merge_sort, counting_sort, radix_sort, heap_sort) def run(func, args): nonlocal number func(args) with lock: print(f'Sorting function {func.__name__} comes on place {number}') number += 1 number = 1 lock = Lock() performance_test_array = get_random_array_of_ints(100000, -10000, 10000) test_array_pool = [ performance_test_array.copy() for _ in range(len(sorting_functions)) ] with concurrent_futures.ThreadPoolExecutor( max_workers=len(sorting_functions)) as executor: sorted_list_futures = [ executor.submit(run, sorting_functions[i], test_array_pool[i]) for i in range(len(sorting_functions)) ]
def test_performance(self): performance_test_array = get_random_array_of_ints(10000, -10000, 10000) results = compare_functions(performance_test_array, check_array_sorted, *self.sorting_functions) for func_name in results: print(f'{func_name}: {results[func_name]}')
Lists with less, than 100K elements perform slower on MP version because of time consumed by process spawn Args: array: list to sort Returns: sorted list """ cpu_number = cpu_count() process_pool = Pool(processes=cpu_number) sub_array_size = len(array) // (cpu_number - 1) sorting_arrays = [ array[i:i + sub_array_size] for i in range(0, len(array), sub_array_size) ] sorting_arrays = process_pool.map(merge_sort_multi_adapted, sorting_arrays) while len(sorting_arrays) > 1: sorting_arrays = zip(sorting_arrays[::2], sorting_arrays[1::2]) sorting_arrays = process_pool.starmap(merge, sorting_arrays) return sorting_arrays[0] if __name__ == '__main__': performance_test_array = get_random_array_of_ints(2000000, -100000, 100000) report = compare_functions(performance_test_array, check_array_sorted, merge_sort_multi_adapted, multiprocess_merge_sort) print(report)