11import functools
2- import itertools
3- import multiprocessing
4- import operator
5- import random
62import sys
7- import timeit
83
94import invoke
105
11-
126run = functools .partial (invoke .Context .run , echo = True , pty = True )
137
148
@@ -23,7 +17,7 @@ def install(context):
2317def clean (context ):
2418 # type: (invoke.Context) -> None
2519 # run(context, f"{sys.executable} setup.py develop --uninstall")
26- run (context , f"{ sys .executable } -m pip uninstall --yes automap " )
20+ run (context , f"{ sys .executable } -m pip uninstall --yes arraymap " )
2721
2822 for artifact in ("*.egg-info" , "*.so" , "build" , "dist" ):
2923 run (context , f"rm -rf { artifact } " )
@@ -41,61 +35,3 @@ def build(context):
4135def test (context ):
4236 # type: (invoke.Context) -> None
4337 run (context , f"{ sys .executable } -m pytest -v" )
44-
45-
46- def do_work (info ):
47- import automap
48-
49- namespace = {"FrozenAutoMap" : automap .FrozenAutoMap }
50- create_a = timeit .Timer ("a = FrozenAutoMap(keys)" , globals = namespace )
51- create_d = timeit .Timer ("d = {k: i for i, k in enumerate(keys)}" , globals = namespace )
52- access_a = timeit .Timer (
53- "for key in a: a[key]" , "a = FrozenAutoMap(keys)" , globals = namespace
54- )
55- access_d = timeit .Timer (
56- "for key in d: d[key]" ,
57- "d = {k: i for i, k in enumerate(keys)}" ,
58- globals = namespace ,
59- )
60- kind , power , factor = info
61- items = factor * 10 ** power
62- namespace ["keys" ] = [kind (_ ) for _ in range (items )]
63- random .shuffle (namespace ["keys" ])
64- iterations = max (create_a .autorange ()[0 ], create_d .autorange ()[0 ])
65- create = create_a .timeit (iterations ) / create_d .timeit (iterations )
66- size = sys .getsizeof (automap .FrozenAutoMap (namespace ["keys" ])) / sys .getsizeof (
67- {k : i for i , k in enumerate (namespace ["keys" ])}
68- )
69- iterations = max (access_a .autorange ()[0 ], access_d .autorange ()[0 ])
70- access = access_a .timeit (iterations ) / access_d .timeit (iterations )
71- return items , create , access , size
72-
73-
74- @invoke .task (build )
75- def performance (context ):
76- # type: (invoke.Context) -> None
77- print ("TYPE\t ITEMS\t CREATE\t ACCESS\t SIZE" )
78-
79- def geometric_mean (xs ):
80- return functools .reduce (operator .mul , xs ) ** (1 / len (xs ))
81-
82- with multiprocessing .get_context ("spawn" ).Pool () as pool :
83- for kind in (str , int ):
84- total_create = []
85- total_access = []
86- total_size = []
87-
88- for items , create , access , size in pool .imap (
89- do_work , itertools .product ((kind ,), range (6 ), range (1 , 10 ))
90- ):
91- print (
92- f"{ kind .__name__ } \t { items :,} \t { create - 1 :+.0%} \t { access - 1 :+.0%} \t { size - 1 :+.0%} " ,
93- flush = True ,
94- )
95- total_create .append (create )
96- total_access .append (access )
97- total_size .append (size )
98- print (
99- f"{ kind .__name__ } \t MEAN\t { geometric_mean (total_create )- 1 :+.0%} \t { geometric_mean (total_access )- 1 :+.0%} \t { geometric_mean (total_size )- 1 :+.0%} " ,
100- flush = True ,
101- )
0 commit comments