def count_lines(filename):
    """
    Count the number of lines in filename.

    :param str filename: Path to the filename.
    :return: The number of lines in the file.
    :rtype: int
    """
    lines = 0
    buf_size = 1024 * 1024

    with open(filename, 'rb') as fd:
        read_f = fd.read  # loop optimization

        buf = read_f(buf_size)
        while buf:
            lines += buf.count(b'\n')
            buf = read_f(buf_size)

    return lines