import re
import io
import mmap


def count_frames(lmp):
    """Count the number of frames in a lammpstrj file."""
    pattern = re.compile(b'ITEM: TIMESTEP')
    with io.open(lmp, 'r', encoding='utf-8') as infile:
        match = pattern.finditer(
            mmap.mmap(infile.fileno(), 0, access=mmap.ACCESS_READ)
        )
        return sum(1 for _ in match)
    return 0