Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Why not just benchmark it?</p> <pre><code>&gt;&gt;&gt; def sha1(s): ... return hashlib.sha1(s).hexdigest() ... &gt;&gt;&gt; def sha512(s): ... return hashlib.sha512(s).hexdigest() ... &gt;&gt;&gt; t1 = timeit.Timer("sha1('asdf' * 100)", "from __main__ import sha1") &gt;&gt;&gt; t512 = timeit.Timer("sha512('asdf' * 100)", "from __main__ import sha512") &gt;&gt;&gt; t1.timeit() 3.2463729381561279 &gt;&gt;&gt; t512.timeit() 6.5079669952392578 </code></pre> <p>So on my machine, <code>hash512</code> is twice as slow as <code>sha1</code>. But as <a href="https://stackoverflow.com/users/238704/gregs">GregS</a> said, why would you use secure hash for caching? Try the builtin hash algorithms which should be really fast and tuned:</p> <pre><code>&gt;&gt;&gt; s = "asdf" &gt;&gt;&gt; hash(s) -618826466 &gt;&gt;&gt; s = "xxx" &gt;&gt;&gt; hash(s) 943435 &gt;&gt;&gt; hash("xxx") 943435 </code></pre> <p>Or better yet, use the builtin Python dictionaries. Maybe you can tell us more about what you plan on caching.</p> <p><em>EDIT:</em> I'm thinking that you are trying to achieve something like this:</p> <pre><code>hash = hashlib.sha1(object_to_cache_as_string).hexdigest() cache[hash] = object_to_cache </code></pre> <p>What I was refferring to by "use the builtin Python dictinoaries" is that you can simplify the above:</p> <pre><code>cache[object_to_cache_as_string] = object_to_cache </code></pre> <p>In this way, Python takes care of the hashing so you don't have to!</p> <p>Regarding your particular problem, you could refer to <a href="https://stackoverflow.com/questions/1151658/python-hashable-dicts">Python hashable dicts</a> in order to make a dictionary hashable. Then, all you'd need to do to cache the object is:</p> <pre><code>cache[object_to_cache] = object_to_cache </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload