def test_hamming_dist_timing(self): list_of_strings = [string_generator() for i in range(10)] print(list_of_strings) string_result = [] string_start = time.process_time_ns() for s1 in list_of_strings: for s2 in list_of_strings: string_result.append(naive_hamming_distance(s1, s2)) string_end = time.process_time_ns() print(f'no of mismatches = {string_result}') print("string hamming: {:,}".format(string_end - string_start)) binary_result = [] binary_start = time.process_time_ns() for s1 in list_of_strings: for s2 in list_of_strings: binary_result.append(hamming_distance(s1, s2)) binary_end = time.process_time_ns() print("binary hamming: {:,}".format(binary_end - binary_start)) list_of_preprocessed_binaries = [] for s in list_of_strings: list_of_preprocessed_binaries.append(string_to_hamming_binary(s)) binary_result_pre = [] binary_start_pre = time.process_time_ns() for s1 in list_of_preprocessed_binaries: for s2 in list_of_preprocessed_binaries: binary_result_pre.append(binary_hamming_dist_calc(s1, s2)) binary_end_pre = time.process_time_ns() print("prepro hamming: {:,}".format(binary_end_pre - binary_start_pre)) self.assertEqual(string_result, binary_result) self.assertEqual(binary_result_pre, binary_result)
def test_hamming_dist_all_different(self): s1 = 'CAT' s2 = 'GGG' expected_dist = 3 actual_dist = naive_hamming_distance(s1, s2) self.assertEqual(expected_dist, actual_dist) actual_dist = hamming_distance(s1, s2) self.assertEqual(expected_dist, actual_dist)
def test_hamming_dist_same(self): s1 = 'CAT' s2 = 'CAT' expected_dist = 0 actual_dist = naive_hamming_distance(s1, s2) self.assertEqual(expected_dist, actual_dist) actual_dist = hamming_distance(s1, s2) self.assertEqual(expected_dist, actual_dist)
def test_haystack_generator(self): string_length = 6 expected_dist = 3 haystack = string_generator(string_length) needle = string_mutator(haystack, expected_dist) actual_dist = naive_hamming_distance(needle, haystack) self.assertEqual(expected_dist, actual_dist) actual_dist = hamming_distance(needle, haystack) self.assertEqual(expected_dist, actual_dist)
def string_time(): SETUP_CODE = ''' from hamming import naive_hamming_distance from random import choice from __main__ import string_generator list_of_strings1 = [string_generator() for i in range(10)] list_of_strings2 = [string_generator() for i in range(10)]''' TEST_CODE = ''' s1 = choice(list_of_strings1) s2 = choice(list_of_strings2) naive_hamming_distance(s1, s2) ''' # timeit.repeat statement times = timeit.repeat(setup=SETUP_CODE, stmt=TEST_CODE, repeat=3, number=10000) # printing minimum exec. time print('Standard hamming string search time: {}'.format(min(times)))
repeat=10, number=10000) # printing minimum exec. time print( 'Binary hamming string search time (not including preprocessing): {}'. format(min(times))) # Validate that my alternative approach gives the same result as naive_hamming_distance(s1,s2) from timming import timming_distance list_of_strings1 = [string_generator() for i in range(10)] list_of_strings2 = [string_generator() for i in range(10)] for s1 in list_of_strings1: for s2 in list_of_strings2: assert timming_distance(s1, s2) == naive_hamming_distance(s1, s2) # compute binary hamming time (using the Tim-hamming method) def timming_preprocessed_time(): SETUP_CODE_PREPROCESSED_BINARY = ''' from timming import string_to_bin, hamming_dist_calc from random import choice from __main__ import string_generator, length_of_strings, number_of_strings list_of_binary_strings1 = [string_to_bin(string_generator(length_of_strings)) for i in range(number_of_strings)] list_of_binary_strings2 = [string_to_bin(string_generator(length_of_strings)) for i in range(number_of_strings)]''' TEST_CODE = ''' s1 = choice(list_of_binary_strings1) s2 = choice(list_of_binary_strings2) hamming_dist_calc(s1, s2)