1import torch 2import torch.utils.benchmark as benchmark 3 4 5MEMO = {} 6 7 8def create_nested_dict_type(layers): 9 if layers == 0: 10 return torch._C.StringType.get() 11 if layers not in MEMO: 12 less_nested = create_nested_dict_type(layers - 1) 13 result = torch._C.DictType( 14 torch._C.StringType.get(), torch._C.TupleType([less_nested, less_nested]) 15 ) 16 MEMO[layers] = result 17 return MEMO[layers] 18 19 20nesting_levels = (1, 3, 5, 10) 21types = (reasonable, medium, big, huge) = [ 22 create_nested_dict_type(x) for x in nesting_levels 23] 24 25timers = [ 26 benchmark.Timer(stmt="x.annotation_str", globals={"x": nested_type}) 27 for nested_type in types 28] 29 30for nesting_level, typ, timer in zip(nesting_levels, types, timers): 31 print("Nesting level:", nesting_level) 32 print("output:", typ.annotation_str[:70]) 33 print(timer.blocked_autorange()) 34