example-code-2e/11-pythonic-obj/mem_test.py

30 lines
831 B
Python
Raw Normal View History

2020-06-09 06:16:38 +02:00
import importlib
import sys
import resource
NUM_VECTORS = 10**7
2021-08-07 05:44:01 +02:00
module = None
2020-06-09 06:16:38 +02:00
if len(sys.argv) == 2:
module_name = sys.argv[1].replace('.py', '')
module = importlib.import_module(module_name)
else:
print(f'Usage: {sys.argv[0]} <vector-module-to-test>')
2020-06-09 06:16:38 +02:00
2021-08-07 05:44:01 +02:00
if module is None:
print('Running test with built-in `complex`')
cls = complex
else:
fmt = 'Selected Vector2d type: {.__name__}.{.__name__}'
print(fmt.format(module, module.Vector2d))
cls = module.Vector2d
2020-06-09 06:16:38 +02:00
mem_init = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
2021-08-07 05:44:01 +02:00
print(f'Creating {NUM_VECTORS:,} {cls.__qualname__!r} instances')
2020-06-09 06:16:38 +02:00
2021-08-07 05:44:01 +02:00
vectors = [cls(3.0, 4.0) for i in range(NUM_VECTORS)]
2020-06-09 06:16:38 +02:00
mem_final = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
print(f'Initial RAM usage: {mem_init:14,}')
print(f' Final RAM usage: {mem_final:14,}')