Closed koaning closed 4 years ago
It's a really minor change and the effect is not super huge, but still noticeable.
If you consider this example.py file.
example.py
import random from evol import Population def create_candidate(): return random.random() - 0.5 def func_to_optimise(x): return x*2 random.seed(42) @profile def foo(): return Population(chromosomes=[create_candidate() for _ in range(0000)], eval_function=func_to_optimise, maximize=True) if __name__ == "__main__": foo()
Here's the results without __slots__
__slots__
(venv) ➜ evol git:(e212796) ✗ python -m memory_profiler examples/population_demo.py Filename: examples/population_demo.py Line # Mem usage Increment Line Contents ================================================ 19 53.949 MiB 53.949 MiB @profile 20 def foo(): 21 54.176 MiB 0.012 MiB return Population(chromosomes=[create_candidate() for _ in range(10000)], 22 56.332 MiB 2.156 MiB eval_function=func_to_optimise, maximize=True)
Here it is with slots
(venv) ➜ evol git:(e212796) ✗ python -m memory_profiler examples/population_demo.py Filename: examples/population_demo.py Line # Mem usage Increment Line Contents ================================================ 19 54.051 MiB 54.051 MiB @profile 20 def foo(): 21 54.262 MiB 0.004 MiB return Population(chromosomes=[create_candidate() for _ in range(10000)], 22 55.516 MiB 1.254 MiB eval_function=func_to_optimise, maximize=True)
Can't hurt to have tho, so I added it.
ah nevermind this.
we use assert all(x.__dict__ == y.__dict__ for x, y in zip(simple_population, pop)) in a few places. the .__dict__ stuff is kind of blocked by slots.
assert all(x.__dict__ == y.__dict__ for x, y in zip(simple_population, pop))
.__dict__
It's a really minor change and the effect is not super huge, but still noticeable.
If you consider this
example.py
file.Here's the results without
__slots__
Here it is with slots
Can't hurt to have tho, so I added it.