go.starlark.net@v0.0.0-20231101134539-556fd59b42f6/starlark/testdata/benchmark.star (about) 1 # Benchmarks of Starlark execution 2 # option:set 3 4 def bench_range_construction(b): 5 for _ in range(b.n): 6 range(200) 7 8 def bench_range_iteration(b): 9 for _ in range(b.n): 10 for x in range(200): 11 pass 12 13 # Make a 2-level call tree of 100 * 100 calls. 14 def bench_calling(b): 15 list = range(100) 16 17 def g(): 18 for x in list: 19 pass 20 21 def f(): 22 for x in list: 23 g() 24 25 for _ in range(b.n): 26 f() 27 28 # Measure overhead of calling a trivial built-in method. 29 emptydict = {} 30 range1000 = range(1000) 31 32 def bench_builtin_method(b): 33 for _ in range(b.n): 34 for _ in range1000: 35 emptydict.get(None) 36 37 def bench_int(b): 38 for _ in range(b.n): 39 a = 0 40 for _ in range1000: 41 a += 1 42 43 def bench_bigint(b): 44 for _ in range(b.n): 45 a = 1 << 31 # maxint32 + 1 46 for _ in range1000: 47 a += 1 48 49 def bench_gauss(b): 50 # Sum of arithmetic series. All results fit in int32. 51 for _ in range(b.n): 52 acc = 0 53 for x in range(92000): 54 acc += x 55 56 def bench_mix(b): 57 "Benchmark of a simple mix of computation (for, if, arithmetic, comprehension)." 58 for _ in range(b.n): 59 x = 0 60 for i in range(50): 61 if i: 62 x += 1 63 a = [x for x in range(i)] 64 65 largedict = {str(v): v for v in range(1000)} 66 67 def bench_dict_equal(b): 68 "Benchmark of dict equality operation." 69 for _ in range(b.n): 70 if largedict != largedict: 71 fail("invalid comparison") 72 73 largeset = set([v for v in range(1000)]) 74 75 def bench_set_equal(b): 76 "Benchmark of set union operation." 77 for _ in range(b.n): 78 if largeset != largeset: 79 fail("invalid comparison") 80 81 flat = { "int": 1, "float": 0.2, "string": "string", "list": [], "bool": True, "nil": None, "tuple": (1, 2, 3) } 82 deep = { 83 "type": "int", 84 "value": 1, 85 "next": { 86 "type": "float", 87 "value": 0.2, 88 "next": { 89 "type": "string", 90 "value": "string", 91 "next": { 92 "type": "list", 93 "value": [ 1, "", True, None, (1, 2) ], 94 "next": { 95 "type": "bool", 96 "value": True, 97 "next": { 98 "type": "tuple", 99 "value": (1, 2.0, "3"), 100 "next": None 101 } 102 } 103 } 104 } 105 } 106 } 107 108 deep_list = [ deep for _ in range(100) ] 109 110 def bench_to_json_flat_mixed(b): 111 "Benchmark json.encode builtin with flat mixed input" 112 for _ in range(b.n): 113 json.encode(flat) 114 115 def bench_to_json_flat_big(b): 116 "Benchmark json.encode builtin with big flat integer input" 117 for _ in range(b.n): 118 json.encode(largedict) 119 120 def bench_to_json_deep(b): 121 "Benchmark json.encode builtin with deep input" 122 for _ in range(b.n): 123 json.encode(deep) 124 125 def bench_to_json_deep_list(b): 126 "Benchmark json.encode builtin with a list of deep input" 127 for _ in range(b.n): 128 json.encode(deep) 129 130 def bench_issubset_unique_large_small(b): 131 "Benchmark set.issubset builtin" 132 s = set(range(10000)) 133 for _ in range(b.n): 134 s.issubset(range(1000)) 135 136 def bench_issubset_unique_small_large(b): 137 "Benchmark set.issubset builtin" 138 s = set(range(1000)) 139 for _ in range(b.n): 140 s.issubset(range(10000)) 141 142 def bench_issubset_unique_same(b): 143 "Benchmark set.issubset builtin" 144 s = set(range(1000)) 145 for _ in range(b.n): 146 s.issubset(range(1000)) 147 148 def bench_issubset_duplicate_large_small(b): 149 "Benchmark set.issubset builtin" 150 s = set(range(10000)) 151 l = list(range(200)) * 5 152 for _ in range(b.n): 153 s.issubset(range(1000)) 154 155 def bench_issubset_duplicate_small_large(b): 156 "Benchmark set.issubset builtin" 157 s = set(range(1000)) 158 l = list(range(2000)) * 5 159 for _ in range(b.n): 160 s.issubset(l) 161 162 def bench_issubset_duplicate_same(b): 163 "Benchmark set.issubset builtin" 164 s = set(range(1000)) 165 l = list(range(200)) * 5 166 for _ in range(b.n): 167 s.issubset(l)