$ python3.9 -m timeit -r11 -s 'def s(x):pass' 's(5)'
5000000 loops, best of 11: 72.1 nsec per loop
$ python3.9 -m timeit -r11 -s 'from functools import cache' -s 'def s(x):pass' -s 'c=cache(s)' 'c(5)'
5000000 loops, best of 11: 60.6 nsec per loop
3/ Behind the scenes, there is a single dictionary lookup and a counter increment.
Some kinds arguments will take longer to hash, but you get the idea, @cache has very little overhead.
4/ It's implemented with fast C code, equivalent to:
key = make_key(args, kwds, typed)
result = cache_get(key, sentinel)
if result is not sentinel:
hits += 1
return result
misses += 1
result = user_function(*args, **kwds)
cache[key] = result
return result
5/ The @cache decorator is new in #Python version 3.9.
Prior to that, you can get the same effect with an lru_cache where the maxsize is set to None:
1/ #Python data science tip: To obtain a better estimate (on average) for a vector of multiple parameters, it is better to analyze sample vectors in aggregate than to use the mean of each component.
Surprisingly, this works even if the components are unrelated to one another.
2/ One example comes from baseball.
Individual batting averages near the beginning of the season aren't as good of a performance predictor as individual batting averages that have been “shrunk” toward the collective mean.
Shockingly, this also works for unrelated variables.
3/ If this seems unintuitive, then you're not alone. That is why it is called Stein's paradox 😉
The reason it works is that errors in one estimate tend to cancel the errors in the other estimates.
#Python tip: #hypothesis is good at finding bugs; however, often as not, the bug is in your understanding of what the code is supposed to do.
1/
Initial belief: The JSON module is buggy because #hypothesis finds cases that don't round-trip.
Bugs in understanding:
* The JSON spec doesn't have NaNs
* A JSON module feature is that lists and tuples both serialize into arrays but can't be distinguished when deserialized.
2/
Initial belief:The colorsys module is buggy because #hypothesis finds conversions that don't round-trip
Bugs in understanding:
* color gamuts are limited
* colors in one gamut may not be representable in another
* implementations are constrained by underlying specifications
3/
#Python tip: Given inexact data, subtracting nearly equal
values increases relative error significantly more
than absolute error.
4.6 ± 0.2 Age of Earth (4.3%) 4.2 ± 0.1 Age of Oceans (2.4%)
___ 0.4 ± 0.3 Huge relative error (75%)
This is called “catastrophic cancellation”.
1/
The subtractive cancellation issue commonly arises in floating point arithmetic. Even if the inputs are exact, intermediate values may not be exactly representable and will have an error bar. Subsequent operations can amplify the error.
1/7th is inexact but within ± ½ ulp.
2/
A commonly given example arises when estimating derivatives with f′(x) ≈ Δf(x) / Δx.
Intuitively, the estimate improves as Δx approaches zero, but in practice, the large relative error from the deltas can overwhelm the result.