Skip to content

Commit 1125296

Browse files
Small performance/readability improvments when iterating dictionnary with keys()
Based on pylint's message ``consider-iterating-dictionary`` suggestion. Surprisingly using a dict or set comprehension instead of a new temp var is actually consistently slower here, which was not intuitive for me. ```python from timeit import timeit families = {1: {"testcase": [1, 2, 3, 5, 8]}} attrs = {1: "a", 2: "b", 3: "c", 4: "d", 5: "e", 6: "f", 7: "g", 8: "h"} class Old: def old(self): self.attrs = attrs temp_attrs = {} for key in self.attrs.keys(): if key in families[1]["testcase"]: temp_attrs[key] = self.attrs[key] self.attrs = temp_attrs class OldBis: def old(self): self.attrs = attrs temp_attrs = {} for key in self.attrs: if key in families[1]["testcase"]: temp_attrs[key] = self.attrs[key] self.attrs = temp_attrs class New: def new(self): self.attrs = attrs self.attrs = { # Even worse with k: v for k in self.attrs.items() k: self.attrs[k] for k in self.attrs if k in families[1]["testcase"] } if __name__ == "__main__": n = 1000000 print(f"Old: {timeit(Old().old, number=n)}") print(f"Just removing the keys(): {timeit(OldBis().old, number=n)}") print(f"List comp, no temp var: {timeit(New().new, number=n)}") ``` Result: Old: 0.9493889989680611 Just removing the keys(): 0.9042672360083088 List comp, no temp var: 0.9916125109884888 It's also true for the other example with similar benchmark, but the exact code probably does not need to be in the commit message.
1 parent bd9b621 commit 1125296

File tree

3 files changed

+2
-3
lines changed

3 files changed

+2
-3
lines changed

pyproject.toml

-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,6 @@ disable= [
183183
"comparison-with-callable",
184184
"comparison-with-itself",
185185
"condition-evals-to-constant",
186-
"consider-iterating-dictionary",
187186
"consider-using-dict-items",
188187
"consider-using-enumerate",
189188
"consider-using-from-import",

src/_pytest/junitxml.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ def record_testreport(self, testreport: TestReport) -> None:
143143
# Filter out attributes not permitted by this test family.
144144
# Including custom attributes because they are not valid here.
145145
temp_attrs = {}
146-
for key in self.attrs.keys():
146+
for key in self.attrs:
147147
if key in families[self.family]["testcase"]:
148148
temp_attrs[key] = self.attrs[key]
149149
self.attrs = temp_attrs

src/_pytest/terminal.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1267,7 +1267,7 @@ def _determine_main_color(self, unknown_type_seen: bool) -> str:
12671267

12681268
def _set_main_color(self) -> None:
12691269
unknown_types: List[str] = []
1270-
for found_type in self.stats.keys():
1270+
for found_type in self.stats:
12711271
if found_type: # setup/teardown reports have an empty key, ignore them
12721272
if found_type not in KNOWN_TYPES and found_type not in unknown_types:
12731273
unknown_types.append(found_type)

0 commit comments

Comments
 (0)