Memoize hamming_weight to optimize runtime

This commit is contained in:
kris 2019-01-02 22:25:16 +00:00
parent 6de5f1797d
commit a8688a6a7e

View File

@ -1,12 +1,13 @@
"""Screen module represents Apple II video display."""
from collections import defaultdict
import functools
import enum
from typing import Set, Iterator, Union, Tuple
import numpy as np
@functools.lru_cache(None)
def hamming_weight(n: int) -> int:
"""Compute hamming weight of 8-bit int"""
n = (n & 0x55) + ((n & 0xAA) >> 1)