Mercurial > cpython-lru_cache-weakref
annotate Lib/test/time_hashlib.py @ 87690:66c1c9f32567 default tip
Add use_weakref option to functools.lru_cache
The use_weakref option makes lru_cache create weak references to the function
arguments when creating the cache key, which prevents them from staying in the
memory forever even after all other references to them are gone.
This is a proof of concept for http://bugs.python.org/issue19859
| author | Radomir Dopieralski <devel@sheep.art.pl> |
|---|---|
| date | Tue, 03 Dec 2013 16:02:48 +0100 |
| parents | 9834301a2fd3 |
| children |
| rev | line source |
|---|---|
|
37875
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
1 # It's intended that this script be run by hand. It runs speed tests on |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
2 # hashlib functions; it does not test for correctness. |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
3 |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
4 import sys, time |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
5 import hashlib |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
6 |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
7 |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
8 def creatorFunc(): |
|
43014
9834301a2fd3
Raise statement normalization in Lib/test/.
Collin Winter <collinw@gmail.com>
parents:
41824
diff
changeset
|
9 raise RuntimeError("eek, creatorFunc not overridden") |
|
37875
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
10 |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
11 def test_scaled_msg(scale, name): |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
12 iterations = 106201/scale * 20 |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
13 longStr = 'Z'*scale |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
14 |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
15 localCF = creatorFunc |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
16 start = time.time() |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
17 for f in range(iterations): |
|
37875
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
18 x = localCF(longStr).digest() |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
19 end = time.time() |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
20 |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
39782
diff
changeset
|
21 print(('%2.2f' % (end-start)), "seconds", iterations, "x", len(longStr), "bytes", name) |
|
37875
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
22 |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
23 def test_create(): |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
24 start = time.time() |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
25 for f in range(20000): |
|
37875
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
26 d = creatorFunc() |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
27 end = time.time() |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
28 |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
39782
diff
changeset
|
29 print(('%2.2f' % (end-start)), "seconds", '[20000 creations]') |
|
37875
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
30 |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
31 def test_zero(): |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
32 start = time.time() |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
33 for f in range(20000): |
|
37875
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
34 x = creatorFunc().digest() |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
35 end = time.time() |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
36 |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
39782
diff
changeset
|
37 print(('%2.2f' % (end-start)), "seconds", '[20000 "" digests]') |
|
37875
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
38 |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
39 |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
40 |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
41 hName = sys.argv[1] |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
42 |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
43 # |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
44 # setup our creatorFunc to test the requested hash |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
45 # |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
46 if hName in ('_md5', '_sha'): |
|
39782
c94f37054f19
Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents:
37875
diff
changeset
|
47 exec('import '+hName) |
|
c94f37054f19
Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents:
37875
diff
changeset
|
48 exec('creatorFunc = '+hName+'.new') |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
39782
diff
changeset
|
49 print("testing speed of old", hName, "legacy interface") |
|
37875
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
50 elif hName == '_hashlib' and len(sys.argv) > 3: |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
51 import _hashlib |
|
39782
c94f37054f19
Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents:
37875
diff
changeset
|
52 exec('creatorFunc = _hashlib.%s' % sys.argv[2]) |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
39782
diff
changeset
|
53 print("testing speed of _hashlib.%s" % sys.argv[2], getattr(_hashlib, sys.argv[2])) |
|
37875
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
54 elif hName == '_hashlib' and len(sys.argv) == 3: |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
55 import _hashlib |
|
39782
c94f37054f19
Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents:
37875
diff
changeset
|
56 exec('creatorFunc = lambda x=_hashlib.new : x(%r)' % sys.argv[2]) |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
39782
diff
changeset
|
57 print("testing speed of _hashlib.new(%r)" % sys.argv[2]) |
|
41824
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41642
diff
changeset
|
58 elif hasattr(hashlib, hName) and hasattr(getattr(hashlib, hName), '__call__'): |
|
37875
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
59 creatorFunc = getattr(hashlib, hName) |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
39782
diff
changeset
|
60 print("testing speed of hashlib."+hName, getattr(hashlib, hName)) |
|
37875
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
61 else: |
|
39782
c94f37054f19
Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents:
37875
diff
changeset
|
62 exec("creatorFunc = lambda x=hashlib.new : x(%r)" % hName) |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
39782
diff
changeset
|
63 print("testing speed of hashlib.new(%r)" % hName) |
|
37875
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
64 |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
65 try: |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
66 test_create() |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
67 except ValueError: |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
39782
diff
changeset
|
68 print() |
|
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
39782
diff
changeset
|
69 print("pass argument(s) naming the hash to run a speed test on:") |
|
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
39782
diff
changeset
|
70 print(" '_md5' and '_sha' test the legacy builtin md5 and sha") |
|
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
39782
diff
changeset
|
71 print(" '_hashlib' 'openssl_hName' 'fast' tests the builtin _hashlib") |
|
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
39782
diff
changeset
|
72 print(" '_hashlib' 'hName' tests builtin _hashlib.new(shaFOO)") |
|
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
39782
diff
changeset
|
73 print(" 'hName' tests the hashlib.hName() implementation if it exists") |
|
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
39782
diff
changeset
|
74 print(" otherwise it uses hashlib.new(hName).") |
|
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
39782
diff
changeset
|
75 print() |
|
37875
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
76 raise |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
77 |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
78 test_zero() |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
79 test_scaled_msg(scale=106201, name='[huge data]') |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
80 test_scaled_msg(scale=10620, name='[large data]') |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
81 test_scaled_msg(scale=1062, name='[medium data]') |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
82 test_scaled_msg(scale=424, name='[4*small data]') |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
83 test_scaled_msg(scale=336, name='[3*small data]') |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
84 test_scaled_msg(scale=212, name='[2*small data]') |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
85 test_scaled_msg(scale=106, name='[small data]') |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
86 test_scaled_msg(scale=creatorFunc().digest_size, name='[digest_size data]') |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
87 test_scaled_msg(scale=10, name='[tiny data]') |
