old_contents: """
  Microbenchmark on the "copy builtin to constant".
  
  The benchmark doesn't use fatoptimize, but specialize explicitly the function.
  """
  import fat
  import sys
  from fatoptimizer.benchmark import bench, format_dt, compared_dt
  
  def func(obj):
      return len(obj)
  
  if fat.get_specialized(func):
      print("ERROR: func() was already specialized")
      sys.exit(1)
  
  def func_cst(obj):
      return 'LEN'(obj)
  func_cst.__code__ = fat.replace_consts(func_cst.__code__, {'LEN': len})
  
  dt = bench('func("abc")')
  print("original bytecode (LOAD_GLOBAL dict): %s" % format_dt(dt))
  
  dt2 = bench('func_cst("abc")')
  print("LOAD_CONST: %s" % compared_dt(dt2, dt))
  
  fat.specialize(func, func_cst, [fat.GuardBuiltins(('dict',))])
  assert fat.get_specialized(func)
  dt3 = bench('func("abc")')
  print("specialized (LOAD_CONST): %s" % compared_dt(dt3, dt))
  
  print("cost of GuardBuiltins: %s" % format_dt(dt3 - dt2, sign=True))
  
new_contents: """
  Microbenchmark on the "copy builtin to constant".
  
  The benchmark doesn't use fatoptimize, but specialize explicitly the function.
  """
  import fat
  import sys
  
  mylen = len
  
  def func(obj):
      return mylen(obj)
  
  if fat.get_specialized(func):
      print("ERROR: func() was already specialized")
      sys.exit(1)
  
  def func_cst(obj):
      return 'MYLEN'(obj)
  func_cst.__code__ = fat.replace_consts(func_cst.__code__, {'MYLEN': mylen})
  
  def run_benchmark(bench):
      bench.timeit(stmt='func("abc")',
                   globals=globals(),
                   name='original bytecode (LOAD_GLOBAL)')
  
      bench.timeit(stmt='func_cst("abc")',
                   globals=globals(),
                   name='LOAD_CONST')
  
      fat.specialize(func, func_cst, [fat.GuardGlobals(('mylen',))])
      assert fat.get_specialized(func)
  
      bench.timeit(stmt='func("abc")',
                   globals=globals(),
                   name='LOAD_CONST with guard')
  
  
subject: rewrite bench_copy_cst with my benchmark library