refactoring

This commit is contained in:
Richard Feistenauer 2019-02-10 11:00:57 +01:00
parent b0a8fb05a4
commit 3e3af78ccd
5 changed files with 21 additions and 16 deletions

View File

@ -1,3 +1,16 @@
# ~Goal
def time_test(s, l):
first = time.time()
for i in range(100000):
s = l[i % 2]
last = time.time()
print((last-first)/100000)
time_test(1, [2, 1])
1.401543617248535e-07
heist auf einem processor ohne synchronized müsste ich ca 10 mal schneller sein? oder 3.3 mal weil 3 prozesse weniger?
# With 100x100 10 times # With 100x100 10 times
Try One Try One

View File

@ -1,10 +1,8 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from cellular_automaton.ca_cell_state import CellState import random
from cellular_automaton.ca_rule import Rule from multiprocessing import freeze_support
from cellular_automaton import *
from cellular_automaton.cellular_automaton import CellularAutomaton, CellularAutomatonProcessor
from cellular_automaton.ca_factory import CAFactory
class TestRule(Rule): class TestRule(Rule):
@ -40,18 +38,13 @@ def make_cellular_automaton(dimension, neighborhood, rule, state_class):
if __name__ == "__main__": if __name__ == "__main__":
import random
from multiprocessing import freeze_support
from cellular_automaton.ca_neighborhood import MooreNeighborhood, EdgeRule
from cellular_automaton.ca_display import PyGameFor2D
freeze_support() freeze_support()
random.seed(1000) random.seed(1000)
# best single is 400/400 with 0,2 ca speed and 0,09 redraw / multi is 300/300 with 0.083 # best single is 400/400 with 0,2 ca speed and 0,09 redraw / multi is 300/300 with 0.083
neighborhood = MooreNeighborhood(EdgeRule.FIRST_AND_LAST_CELL_OF_DIMENSION_ARE_NEIGHBORS) neighborhood = MooreNeighborhood(EdgeRule.FIRST_AND_LAST_CELL_OF_DIMENSION_ARE_NEIGHBORS)
ca = make_cellular_automaton(dimension=[100, 100], neighborhood=neighborhood, rule=TestRule(), state_class=MyState) ca = make_cellular_automaton(dimension=[100, 100], neighborhood=neighborhood, rule=TestRule(), state_class=MyState)
ca_window = PyGameFor2D(window_size=[1000, 800], cellular_automaton=ca)
ca_processor = CellularAutomatonProcessor(process_count=4, cellular_automaton=ca) ca_processor = CellularAutomatonProcessor(process_count=4, cellular_automaton=ca)
ca_window.main_loop(cellular_automaton_processor=ca_processor,
ca_iterations_per_draw=5) ca_window = PyGameFor2D(window_size=[1000, 800], cellular_automaton=ca)
ca_window.main_loop(cellular_automaton_processor=ca_processor, ca_iterations_per_draw=10)

Binary file not shown.

View File

@ -42,7 +42,7 @@ class CellState:
def set_state_of_iteration(self, new_state, iteration): def set_state_of_iteration(self, new_state, iteration):
""" Will set the new state for the iteration modulo number of saved states. """ Will set the new state for the iteration modulo number of saved states.
:param new_state: The new state to set. :param new_state: The new state to set.
:param iteration: Uses the iteration index, to differ between concurrent states. :param iteration: Uses the iteration index, to differ between concurrent states.
:return True if state has changed. :return True if state has changed.
""" """

View File

@ -38,7 +38,6 @@ class PyGameFor2D:
def __init__(self, window_size: list, cellular_automaton: CellularAutomaton): def __init__(self, window_size: list, cellular_automaton: CellularAutomaton):
self._window_size = window_size self._window_size = window_size
self._cellular_automaton = cellular_automaton self._cellular_automaton = cellular_automaton
self._cellular_automaton_proocessor = None
pygame.init() pygame.init()
pygame.display.set_caption("Cellular Automaton") pygame.display.set_caption("Cellular Automaton")
self._screen = pygame.display.set_mode(self._window_size) self._screen = pygame.display.set_mode(self._window_size)
@ -68,7 +67,7 @@ class PyGameFor2D:
self._evolve_with_performance(cellular_automaton_processor) self._evolve_with_performance(cellular_automaton_processor)
first = False first = False
else: else:
cellular_automaton_processor.evolve() cellular_automaton_processor.evolve_x_times(ca_iterations_per_draw)
time_ca_end = time.time() time_ca_end = time.time()
self.ca_display.redraw_cellular_automaton() self.ca_display.redraw_cellular_automaton()
time_ds_end = time.time() time_ds_end = time.time()