Skip to content

Instantly share code, notes, and snippets.

@kwatch
Created May 27, 2012 04:15
Show Gist options
  • Select an option

  • Save kwatch/2802160 to your computer and use it in GitHub Desktop.

Select an option

Save kwatch/2802160 to your computer and use it in GitHub Desktop.
benchmark script to measure cost of sys._getframe(1) and sys._getframe(1).f_locals
# -*- coding: utf-8 -*-
##
## benchmark script to measure cost of sys._getframe(1) and sys._getframe(1).f_locals
##
## How to run:
## $ easy_install benchmarker
## $ python bench_getframe.py -h
## $ python bench_getframe.py -n 1000000 -c 5 -X 1
##
import sys
import benchmarker
from benchmarker import Benchmarker
benchmarker.cmdopt.parse()
def f5(x): return f4(x)
def f4(x): return f3(x)
def f3(x): return f2(x)
def f2(x): return f1(x)
for bm in Benchmarker(width=40, loop=100000, cycle=5, extra=1):
def f1(x):
return None
with bm('(depth=2) return None'):
for _ in xrange(bm.loop): f2(0)
with bm('(depth=3) return None'):
for _ in xrange(bm.loop): f3(0)
with bm('(depth=4) return None'):
for _ in xrange(bm.loop): f4(0)
with bm('(depth=5) return None'):
for _ in xrange(bm.loop): f5(0)
def f1(x):
return sys._getframe(1)
with bm('(depth=2) sys._getframe()'):
for _ in xrange(bm.loop): f2(0)
with bm('(depth=3) sys._getframe()'):
for _ in xrange(bm.loop): f3(0)
with bm('(depth=4) sys._getframe()'):
for _ in xrange(bm.loop): f4(0)
with bm('(depth=5) sys._getframe()'):
for _ in xrange(bm.loop): f5(0)
def f1(x):
return sys._getframe(1).f_locals
with bm('(depth=2) sys._getframe().f_locals'):
for _ in xrange(bm.loop): f2(0)
with bm('(depth=3) sys._getframe().f_locals'):
for _ in xrange(bm.loop): f3(0)
with bm('(depth=4) sys._getframe().f_locals'):
for _ in xrange(bm.loop): f4(0)
with bm('(depth=5) sys._getframe().f_locals'):
for _ in xrange(bm.loop): f5(0)
### ----------------------------------------
### example of result on Python 2.7.2
### ----------------------------------------
#
# ## benchmarker: release 0.0.0 (for python)
# ## python platform: darwin [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)]
# ## python version: 2.7.2
# ## python executable: /opt/lang/python/2.7.2/bin/python
#
# ## Remove min & max min cycle max cycle
# (depth=2) return None 0.4516 (#7) 0.4606 (#1)
# (depth=3) return None 0.6002 (#6) 0.6201 (#3)
# (depth=4) return None 0.7393 (#2) 0.7503 (#5)
# (depth=5) return None 0.8938 (#5) 0.9023 (#2)
# (depth=2) sys._getframe() 0.7463 (#7) 0.7630 (#5)
# (depth=3) sys._getframe() 0.9362 (#5) 0.9519 (#2)
# (depth=4) sys._getframe() 1.1238 (#1) 1.1615 (#2)
# (depth=5) sys._getframe() 1.2662 (#3) 1.3054 (#7)
# (depth=2) sys._getframe().f_locals 0.9960 (#2) 1.0073 (#7)
# (depth=3) sys._getframe().f_locals 1.1433 (#6) 1.1672 (#3)
# (depth=4) sys._getframe().f_locals 1.3022 (#3) 1.3152 (#6)
# (depth=5) sys._getframe().f_locals 1.4709 (#5) 1.4860 (#3)
#
# ## Average of 5 (=7-2*1) user sys total real
# (depth=2) return None 0.4540 0.0000 0.4540 0.4527
# (depth=3) return None 0.6040 0.0020 0.6060 0.6087
# (depth=4) return None 0.7400 0.0000 0.7400 0.7450
# (depth=5) return None 0.8960 0.0000 0.8960 0.8960
# (depth=2) sys._getframe() 0.7520 0.0000 0.7520 0.7546
# (depth=3) sys._getframe() 0.9440 0.0000 0.9440 0.9453
# (depth=4) sys._getframe() 1.1240 0.0040 1.1280 1.1275
# (depth=5) sys._getframe() 1.2680 0.0000 1.2680 1.2736
# (depth=2) sys._getframe().f_locals 0.9960 0.0020 0.9980 1.0020
# (depth=3) sys._getframe().f_locals 1.1520 0.0020 1.1540 1.1562
# (depth=4) sys._getframe().f_locals 1.3020 0.0000 1.3020 1.3072
# (depth=5) sys._getframe().f_locals 1.4740 0.0060 1.4800 1.4801
#
# ## Ranking real
# (depth=2) return None 0.4527 (100.0%) *************************
# (depth=3) return None 0.6087 ( 74.4%) *******************
# (depth=4) return None 0.7450 ( 60.8%) ***************
# (depth=2) sys._getframe() 0.7546 ( 60.0%) ***************
# (depth=5) return None 0.8960 ( 50.5%) *************
# (depth=3) sys._getframe() 0.9453 ( 47.9%) ************
# (depth=2) sys._getframe().f_locals 1.0020 ( 45.2%) ***********
# (depth=4) sys._getframe() 1.1275 ( 40.1%) **********
# (depth=3) sys._getframe().f_locals 1.1562 ( 39.2%) **********
# (depth=5) sys._getframe() 1.2736 ( 35.5%) *********
# (depth=4) sys._getframe().f_locals 1.3072 ( 34.6%) *********
# (depth=5) sys._getframe().f_locals 1.4801 ( 30.6%) ********
#
# ## Ratio Matrix real [01] [02] [03] [04] [05] [06] [07] [08] [09] [10] [11] [12]
# [01] (depth=2) return None 0.4527 100.0% 134.5% 164.6% 166.7% 197.9% 208.8% 221.4% 249.1% 255.4% 281.4% 288.8% 327.0%
# [02] (depth=3) return None 0.6087 74.4% 100.0% 122.4% 124.0% 147.2% 155.3% 164.6% 185.2% 189.9% 209.2% 214.8% 243.2%
# [03] (depth=4) return None 0.7450 60.8% 81.7% 100.0% 101.3% 120.3% 126.9% 134.5% 151.3% 155.2% 171.0% 175.5% 198.7%
# [04] (depth=2) sys._getframe() 0.7546 60.0% 80.7% 98.7% 100.0% 118.7% 125.3% 132.8% 149.4% 153.2% 168.8% 173.2% 196.1%
# [05] (depth=5) return None 0.8960 50.5% 67.9% 83.1% 84.2% 100.0% 105.5% 111.8% 125.8% 129.0% 142.1% 145.9% 165.2%
# [06] (depth=3) sys._getframe() 0.9453 47.9% 64.4% 78.8% 79.8% 94.8% 100.0% 106.0% 119.3% 122.3% 134.7% 138.3% 156.6%
# [07] (depth=2) sys._getframe().f_locals 1.0020 45.2% 60.7% 74.4% 75.3% 89.4% 94.3% 100.0% 112.5% 115.4% 127.1% 130.5% 147.7%
# [08] (depth=4) sys._getframe() 1.1275 40.1% 54.0% 66.1% 66.9% 79.5% 83.8% 88.9% 100.0% 102.5% 113.0% 115.9% 131.3%
# [09] (depth=3) sys._getframe().f_locals 1.1562 39.2% 52.6% 64.4% 65.3% 77.5% 81.8% 86.7% 97.5% 100.0% 110.2% 113.1% 128.0%
# [10] (depth=5) sys._getframe() 1.2736 35.5% 47.8% 58.5% 59.2% 70.4% 74.2% 78.7% 88.5% 90.8% 100.0% 102.6% 116.2%
# [11] (depth=4) sys._getframe().f_locals 1.3072 34.6% 46.6% 57.0% 57.7% 68.5% 72.3% 76.7% 86.3% 88.4% 97.4% 100.0% 113.2%
# [12] (depth=5) sys._getframe().f_locals 1.4801 30.6% 41.1% 50.3% 51.0% 60.5% 63.9% 67.7% 76.2% 78.1% 86.1% 88.3% 100.0%
### ----------------------------------------
### example of result on PyPy 1.8
### ----------------------------------------
#
# ## benchmarker: release 0.0.0 (for python)
# ## python platform: darwin [PyPy 1.8.0 with GCC 4.2.1]
# ## python version: 2.7.2
# ## python executable: /opt/lang/pypy/1.8/bin/pypy
#
# ## Remove min & max min cycle max cycle
# (depth=2) return None 0.0144 (#5) 0.0314 (#2)
# (depth=3) return None 0.0130 (#5) 0.0263 (#2)
# (depth=4) return None 0.0143 (#5) 0.0271 (#2)
# (depth=5) return None 0.0233 (#5) 0.0398 (#2)
# (depth=2) sys._getframe() 0.3718 (#5) 0.3856 (#6)
# (depth=3) sys._getframe() 0.5530 (#7) 0.5833 (#3)
# (depth=4) sys._getframe() 0.7488 (#4) 0.7888 (#1)
# (depth=5) sys._getframe() 0.9581 (#7) 1.0232 (#4)
# (depth=2) sys._getframe().f_locals 0.7173 (#4) 0.7505 (#1)
# (depth=3) sys._getframe().f_locals 0.9044 (#7) 0.9286 (#1)
# (depth=4) sys._getframe().f_locals 1.1160 (#4) 1.1915 (#1)
# (depth=5) sys._getframe().f_locals 1.3334 (#7) 1.4511 (#1)
#
# ## Average of 5 (=7-2*1) user sys total real
# (depth=2) return None 0.0180 0.0000 0.0180 0.0157
# (depth=3) return None 0.0200 0.0000 0.0200 0.0158
# (depth=4) return None 0.0200 0.0000 0.0200 0.0170
# (depth=5) return None 0.0240 0.0000 0.0240 0.0247
# (depth=2) sys._getframe() 0.3760 0.0000 0.3760 0.3772
# (depth=3) sys._getframe() 0.5560 0.0020 0.5580 0.5588
# (depth=4) sys._getframe() 0.7500 0.0020 0.7520 0.7543
# (depth=5) sys._getframe() 0.9640 0.0000 0.9640 0.9720
# (depth=2) sys._getframe().f_locals 0.7200 0.0040 0.7240 0.7276
# (depth=3) sys._getframe().f_locals 0.9040 0.0020 0.9060 0.9146
# (depth=4) sys._getframe().f_locals 1.1180 0.0040 1.1220 1.1293
# (depth=5) sys._getframe().f_locals 1.3380 0.0000 1.3380 1.3463
#
# ## Ranking real
# (depth=2) return None 0.0157 (100.0%) *************************
# (depth=3) return None 0.0158 ( 99.1%) *************************
# (depth=4) return None 0.0170 ( 92.3%) ***********************
# (depth=5) return None 0.0247 ( 63.5%) ****************
# (depth=2) sys._getframe() 0.3772 ( 4.2%) *
# (depth=3) sys._getframe() 0.5588 ( 2.8%) *
# (depth=2) sys._getframe().f_locals 0.7276 ( 2.2%) *
# (depth=4) sys._getframe() 0.7543 ( 2.1%) *
# (depth=3) sys._getframe().f_locals 0.9146 ( 1.7%)
# (depth=5) sys._getframe() 0.9720 ( 1.6%)
# (depth=4) sys._getframe().f_locals 1.1293 ( 1.4%)
# (depth=5) sys._getframe().f_locals 1.3463 ( 1.2%)
#
# ## Ratio Matrix real [01] [02] [03] [04] [05] [06] [07] [08] [09] [10] [11] [12]
# [01] (depth=2) return None 0.0157 100.0% 100.9% 108.4% 157.4% 2406.9% 3565.3% 4642.1% 4812.9% 5835.5% 6201.4% 7205.0% 8590.0%
# [02] (depth=3) return None 0.0158 99.1% 100.0% 107.4% 155.9% 2384.4% 3531.9% 4598.6% 4767.7% 5780.7% 6143.3% 7137.4% 8509.4%
# [03] (depth=4) return None 0.0170 92.3% 93.1% 100.0% 145.2% 2220.9% 3289.7% 4283.3% 4440.8% 5384.4% 5722.0% 6648.0% 7925.9%
# [04] (depth=5) return None 0.0247 63.5% 64.1% 68.9% 100.0% 1529.4% 2265.5% 2949.7% 3058.2% 3707.9% 3940.5% 4578.2% 5458.2%
# [05] (depth=2) sys._getframe() 0.3772 4.2% 4.2% 4.5% 6.5% 100.0% 148.1% 192.9% 200.0% 242.4% 257.6% 299.3% 356.9%
# [06] (depth=3) sys._getframe() 0.5588 2.8% 2.8% 3.0% 4.4% 67.5% 100.0% 130.2% 135.0% 163.7% 173.9% 202.1% 240.9%
# [07] (depth=2) sys._getframe().f_locals 0.7276 2.2% 2.2% 2.3% 3.4% 51.8% 76.8% 100.0% 103.7% 125.7% 133.6% 155.2% 185.0%
# [08] (depth=4) sys._getframe() 0.7543 2.1% 2.1% 2.3% 3.3% 50.0% 74.1% 96.5% 100.0% 121.2% 128.9% 149.7% 178.5%
# [09] (depth=3) sys._getframe().f_locals 0.9146 1.7% 1.7% 1.9% 2.7% 41.2% 61.1% 79.6% 82.5% 100.0% 106.3% 123.5% 147.2%
# [10] (depth=5) sys._getframe() 0.9720 1.6% 1.6% 1.7% 2.5% 38.8% 57.5% 74.9% 77.6% 94.1% 100.0% 116.2% 138.5%
# [11] (depth=4) sys._getframe().f_locals 1.1293 1.4% 1.4% 1.5% 2.2% 33.4% 49.5% 64.4% 66.8% 81.0% 86.1% 100.0% 119.2%
# [12] (depth=5) sys._getframe().f_locals 1.3463 1.2% 1.2% 1.3% 1.8% 28.0% 41.5% 54.0% 56.0% 67.9% 72.2% 83.9% 100.0%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment