8000 Use `collections.defaultdict` to speed up metrics aggregation by umax · Pull Request #257 · prometheus/client_python · GitHub
[go: up one dir, main page]

Skip to content

Use collections.defaultdict to speed up metrics aggregation #257

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

umax
Copy link
Contributor
@umax umax commented Mar 21, 2018

Hi!

This is the next performance pull request. This change is about using collections.defaultdict for counting samples. It works much faster than setdefault method:

In [1]: from collections import defaultdict

In [2]: d={}
In [3]: %timeit d.setdefault('key', 0.0);d['key']+=1
197 ns ± 7.5 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
In [4]: d
Out[4]: {'key': 8111111.0}

In [5]: d=defaultdict(float)
In [6]: %timeit d['key']+=1
80.9 ns ± 0.841 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
In [7]: d
Out[7]: defaultdict(float, {'key': 81111111.0})

@brian-brazil brian-brazil merged commit cce9af5 into prometheus:master Mar 21, 2018
@brian-brazil
Copy link
Contributor

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0