import mmap
import hashlib


def hash_file(filename):
    """
    Return the hexadecimal MD5 hash of the given file
    """
    # Memory mapping appears to be the easiest and most efficient way to hash
    # large files in Python
    with open(filename, "rb") as f:
        mmapped_file = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
        try:
            hashobj = hashlib.md5(mmapped_file)
        finally:
            mmapped_file.close()
    return hashobj.hexdigest()