...

Text file src/go.starlark.net/starlark/testdata/benchmark.star

Documentation: go.starlark.net/starlark/testdata

     1# Benchmarks of Starlark execution
     2# option:set
     3
     4def bench_range_construction(b):
     5    for _ in range(b.n):
     6        range(200)
     7
     8def 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.
    14def 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.
    29emptydict = {}
    30range1000 = range(1000)
    31
    32def bench_builtin_method(b):
    33    for _ in range(b.n):
    34        for _ in range1000:
    35            emptydict.get(None)
    36
    37def bench_int(b):
    38    for _ in range(b.n):
    39        a = 0
    40        for _ in range1000:
    41            a += 1
    42
    43def bench_bigint(b):
    44    for _ in range(b.n):
    45        a = 1 << 31  # maxint32 + 1
    46        for _ in range1000:
    47            a += 1
    48
    49def 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
    56def 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
    65largedict = {str(v): v for v in range(1000)}
    66
    67def 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
    73largeset = set([v for v in range(1000)])
    74
    75def 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
    81flat = { "int": 1, "float": 0.2, "string": "string", "list": [], "bool": True, "nil": None, "tuple": (1, 2, 3) }
    82deep = {
    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
   108deep_list = [ deep for _ in range(100) ]
   109
   110def 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
   115def 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
   120def bench_to_json_deep(b):
   121    "Benchmark json.encode builtin with deep input"
   122    for _ in range(b.n):
   123        json.encode(deep)
   124
   125def 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)

View as plain text