github.com/grumpyhome/grumpy@v0.3.1-0.20201208125205-7b775405bdf1/grumpy-runtime-src/benchmarks/dict.py (about)

     1  # Copyright 2016 Google Inc. All Rights Reserved.
     2  #
     3  # Licensed under the Apache License, Version 2.0 (the "License");
     4  # you may not use this file except in compliance with the License.
     5  # You may obtain a copy of the License at
     6  #
     7  #     http://www.apache.org/licenses/LICENSE-2.0
     8  #
     9  # Unless required by applicable law or agreed to in writing, software
    10  # distributed under the License is distributed on an "AS IS" BASIS,
    11  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  # See the License for the specific language governing permissions and
    13  # limitations under the License.
    14  
    15  """Benchmarks for dictionary operations."""
    16  
    17  # pylint: disable=pointless-statement
    18  
    19  import weetest
    20  
    21  
    22  def BenchmarkDictCreate(b):  
    23    for _ in xrange(b.N):
    24      d = {'one': 1, 'two': 2, 'three': 3}
    25  
    26  
    27  def BenchmarkDictCreateFunc(b):  
    28    for _ in xrange(b.N):
    29      d = dict(one=1, two=2, three=3)
    30  
    31      
    32  def BenchmarkDictGetItem(b):
    33    d = {42: 123}
    34    for _ in xrange(b.N):
    35      d[42]
    36  
    37  
    38  def BenchmarkDictStringOnlyGetItem(b):
    39    d = {'foo': 123}
    40    for _ in xrange(b.N):
    41      d['foo']
    42  
    43  
    44  def BenchmarkDictSetItem(b):
    45    d = {}
    46    for _ in xrange(b.N):
    47      d[42] = 123
    48  
    49  
    50  def BenchmarkDictStringOnlySetItem(b):
    51    d = {}
    52    for _ in xrange(b.N):
    53      d['foo'] = 123
    54  
    55  
    56  def BenchmarkHashStrCached(b):
    57    """Hashes the same value repeatedly to exercise any hash caching logic."""
    58    h = hash  # Prevent builtins lookup each iteration.
    59    for _ in xrange(b.N):
    60      h('foobarfoobarfoobar')
    61  
    62  
    63  if __name__ == '__main__':
    64    weetest.RunBenchmarks()