Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- from random import choice
- from collections import defaultdict
- from datetime import datetime
- from dataclasses import dataclass
- ALL_CATEGORIES = [f'category-{i}' for i in range(5000)]
- # z requesta
- REQUIRED = [f'category-{i}' for i in range(500)]
- @dataclass(frozen = True)
- class Product:
- name: str
- id: int
- category: str
- def generate_product(idx: int) -> Product:
- return Product(f'product-{idx}', idx, choice(ALL_CATEGORIES))
- DATABASE_ITEMS_COUNT = 5000000
- def rozwiazanie_pieprzyk():
- print('rozwiazanie pieprzyk')
- required_categories = set(REQUIRED)
- products_by_category = {}
- for product in [generate_product(i) for i in range(DATABASE_ITEMS_COUNT)]:
- if product.category not in required_categories:
- continue
- products_by_category.setdefault(product.category, [])
- products_by_category[product.category].append(product)
- len_all = DATABASE_ITEMS_COUNT
- len_found = 0
- for required_category in required_categories:
- len_found += len(products_by_category[required_category])
- print(f'znaleziono {len_found}/{len_all} produktów ({(len_found / len_all) * 100}%)')
- def rozwiazanie_niedzwiedz():
- print('rozwiazanie niedzwiedz')
- required_categories = set(REQUIRED)
- database_products = (generate_product(i) for i in range(DATABASE_ITEMS_COUNT))
- len_found = sum((
- 1 for product in database_products if product.category in required_categories
- ))
- len_all = DATABASE_ITEMS_COUNT
- print(
- f'znaleziono {len_found}/{len_all} produktów ({(len_found / len_all) * 100}%)'
- )
- start = datetime.now()
- rozwiazanie_pieprzyk()
- print('koniec:', datetime.now() - start)
- start = datetime.now()
- rozwiazanie_niedzwiedz()
- print('koniec:', datetime.now() - start)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement