deadpixi / contracts

An implementation of contracts for Python.
GNU Lesser General Public License v3.0
342 stars 18 forks source link

Do not build namedtuple on each function call #21

Open jamescasbon opened 5 years ago

jamescasbon commented 5 years ago

Building a namedtuple is expensive, this takes the overhead from over 1000 200 times.

Before

---------------------------------------------------------------------------------------------------------- benchmark: 2 tests ----------------------------------------------------------------------------------------------------------
Name (time in ns)                             Min                       Max                    Mean                 StdDev                  Median                   IQR            Outliers  OPS (Kops/s)            Rounds  Iterations
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
test_bench_python_call                    72.1193 (1.0)            301.5599 (1.0)           74.4546 (1.0)           3.4195 (1.0)           74.1100 (1.0)          0.6799 (1.0)    1724;12054   13,431.0006 (1.0)      124843         100
test_bench_python_call_with_pred     242,119.8878 (>1000.0)  1,165,485.9409 (>1000.0)  256,147.9291 (>1000.0)  34,836.0742 (>1000.0)  251,077.8140 (>1000.0)  7,661.0013 (>1000.0)    85;143        3.9040 (0.00)       2467           1
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

After

------------------------------------------------------------------------------------------------------- benchmark: 2 tests ------------------------------------------------------------------------------------------------------
Name (time in ns)                            Min                    Max                   Mean                StdDev                 Median                 IQR              Outliers  OPS (Kops/s)            Rounds  Iterations
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
test_bench_python_call                   73.4092 (1.0)         508.5301 (1.0)          77.5179 (1.0)         10.0248 (1.0)          75.6001 (1.0)        1.0477 (1.0)      4535;18625   12,900.2492 (1.0)      128917         100
test_bench_python_call_with_pred     14,385.9070 (195.97)   62,283.1285 (122.48)   15,858.5322 (204.58)   2,521.3667 (251.51)   15,086.9600 (199.56)   659.3764 (629.33)    1430;2856       63.0575 (0.00)      19077           1
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
aldanor commented 4 years ago

@deadpixi Wonder if this could be merged?..