哈希大型任意对象的最快方法是什么? [英] What is the quickest way to hash a large arbitrary object?

查看:55
本文介绍了哈希大型任意对象的最快方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一种生成用于缓存函数结果的缓存密钥的方法,该密钥基于函数名称和参数的哈希值的组合.

I am writing a method to generate cache keys for caching function results, the key is based on a combination of function name and hash value of parameters.

当前,我正在使用hashlib来哈希参数的序列化版本,但是序列化大型对象的操作非常昂贵,那么还有什么选择呢?

Currently I am using hashlib to hash the serialized version of parameters, however the operation is very expensive to serialize large objects, so what's the alternative?

#get the cache key for storage
def cache_get_key(*args):
    import hashlib
    serialise = []
    for arg in args:
        serialise.append(str(arg))
    key = hashlib.md5("".join(serialise)).hexdigest()
    return key

更新:我尝试使用hash(str(args)),但是如果args中包含相对较大的数据,则仍然需要很长时间才能计算出哈希值.还有更好的方法吗?

UPDATE: I tried using hash(str(args)), but if args have relatively large data in it, still takes long time to compute the hash value. Any better way to do it?

实际上,拥有大量数据的str(args)永远需要...

Actually str(args) with large data takes forever...

推荐答案

您是否尝试过仅使用 hash 函数?它在元组上效果很好.

Have you tried just using the hash function? It works perfectly well on tuples.

这篇关于哈希大型任意对象的最快方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆