Skip to content

Use collections.defaultdict to speed up metrics aggregation - #257

Merged
brian-brazil merged 1 commit into
prometheus:masterfrom
umax:use-defaultdict-instead-of-setdefault
Mar 21, 2018
Merged

brian-brazil merged 1 commit into
prometheus:masterfrom
umax:use-defaultdict-instead-of-setdefault

Conversation

@umax

@umax umax commented Mar 21, 2018

Copy link
Copy Markdown
Contributor

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
Copy Markdown
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