def benchmark_access_time(): # Get average access time for the symmetric matrix. sm_array = SM(N, create_array) time_sm_array_start = time.time() for experiment in range(EXPERIMENTS): for i in range(N): for j in range(N): sm_array[i, j] = experiment time_sm_array_end = time.time() time_sm_array = (time_sm_array_end - time_sm_array_start) / EXPERIMENTS # Get average access time for the numpy matrix. m_numpy = numpy.zeros((N, N), numpy.int64) time_m_numpy_start = time.time() for experiment in range(EXPERIMENTS): for i in range(N): for j in range(N): m_numpy[i, j] = experiment time_m_numpy_end = time.time() time_m_numpy = (time_m_numpy_end - time_m_numpy_start) / EXPERIMENTS # Print table with results. table = texttable.Texttable() table.set_deco(texttable.Texttable.BORDER | texttable.Texttable.HEADER | texttable.Texttable.VLINES) table.header(['Matrix Type', 'Access Time']) table.add_row([ 'Symmetric Matrix (via array)', to_str_in_sec(time_sm_array) ]) table.add_row(['Numpy Matrix', to_str_in_sec(time_m_numpy)]) print(table.draw())
def test_can_pass_custom_create_storage(self): create_storage = functools.partial( multiprocessing.RawArray, ctypes.c_int ) m = SM(3, create_storage) m[1, 2] = 5 self.assertEqual(m[2, 1], 5)
def benchmark_access_time(): python_matrix = SMP(N, create_array) cython_matrix = SM(N, create_array) numpy_matrix = numpy.zeros((N, N), numpy.int64) # Print table with results. table = texttable.Texttable() table.set_deco(texttable.Texttable.BORDER | texttable.Texttable.HEADER | texttable.Texttable.VLINES) table.header(['Matrix Type', 'Access Time']) table.add_row([ 'Symmetric Matrix (Python version)', to_str_in_sec(_experiment(python_matrix)) ]) table.add_row([ 'Symmetric Matrix (Cython version)', to_str_in_sec(_experiment(cython_matrix)) ]) table.add_row(['Numpy Matrix', to_str_in_sec(_experiment(numpy_matrix))]) print(table.draw())
def benchmark_memory(): # Get size of symmetric matrix that stores 64bit integers. sm_array = SM(N, create_array) size_sm_array = pympler.asizeof.asizeof(sm_array) # Get size of numpy matrix that stores 64bit integers. m_numpy = numpy.zeros((N, N), numpy.int64) size_m_numpy = pympler.asizeof.asizeof(m_numpy) # Print table with results. table = texttable.Texttable() table.set_deco(texttable.Texttable.BORDER | texttable.Texttable.HEADER | texttable.Texttable.VLINES) table.header(['Matrix Type', 'Memory Usage']) table.add_row([ 'Symmetric Matrix (via array)', to_str_in_mb(size_sm_array) ]) table.add_row(['Numpy Matrix', to_str_in_mb(size_m_numpy)]) print(table.draw())
def test_non_positive_size_raises_exception(self): with self.assertRaises(ValueError): SM(0)
def test_access_out_of_range_raises_exception(self): m = SM(3) with self.assertRaises(IndexError): m[3, 0] = 5
def test_write_to_x_y_writes_also_to_y_x(self): m = SM(3) m[1, 2] = 5 self.assertEqual(m[2, 1], 5)
def test_write_above_diagonal_does_not_write_to_invalid_index(self): m = SM(3) m[1, 2] = 5 self.assertEqual(m[2, 0], 0)
def test_value_can_be_read_after_write(self): m = SM(3) m[0, 0] = 5 self.assertEqual(m[0, 0], 5)
def test_values_are_zero_after_creation(self): m = SM(3) for x in range(3): for y in range(3): self.assertEqual(m[x, y], 0)
def test_len_returns_correct_value(self): m = SM(3) self.assertEqual(len(m), 3)