Skip to content

Commit 8f5b4a1

Browse files
Illviljandcherian
andauthored
Speed up _mapping_repr (#5661)
* Create a ordered list from the keys only. * Add benchmark this branch: [ 31.25%] ··· repr.Repr.time_repr 14.8±0.3ms [ 37.50%] ··· repr.Repr.time_repr_html 164±1ms main: [ 31.25%] ··· repr.Repr.time_repr 41.6±0.5ms [ 37.50%] ··· repr.Repr.time_repr_html 188±1ms * Update whats-new.rst Co-authored-by: dcherian <deepak@cherian.net>
1 parent 9ea09e0 commit 8f5b4a1

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

asv_bench/benchmarks/repr.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,30 @@
1+
import numpy as np
12
import pandas as pd
23

34
import xarray as xr
45

56

7+
class Repr:
8+
def setup(self):
9+
a = np.arange(0, 100)
10+
data_vars = dict()
11+
for i in a:
12+
data_vars[f"long_variable_name_{i}"] = xr.DataArray(
13+
name=f"long_variable_name_{i}",
14+
data=np.arange(0, 20),
15+
dims=[f"long_coord_name_{i}_x"],
16+
coords={f"long_coord_name_{i}_x": np.arange(0, 20) * 2},
17+
)
18+
self.ds = xr.Dataset(data_vars)
19+
self.ds.attrs = {f"attr_{k}": 2 for k in a}
20+
21+
def time_repr(self):
22+
repr(self.ds)
23+
24+
def time_repr_html(self):
25+
self.ds._repr_html_()
26+
27+
628
class ReprMultiIndex:
729
def setup(self):
830
index = pd.MultiIndex.from_product(

doc/whats-new.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ Documentation
4343
Internal Changes
4444
~~~~~~~~~~~~~~~~
4545

46+
- Improve the performance of reprs for large datasets or dataarrays. (:pull:`5661`)
47+
By `Jimmy Westling <https://github.com/illviljan>`_.
48+
4649
.. _whats-new.0.19.0:
4750

4851
v0.19.0 (23 July 2021)

xarray/core/formatting.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -387,12 +387,14 @@ def _mapping_repr(
387387
elif len_mapping > max_rows:
388388
summary = [f"{summary[0]} ({max_rows}/{len_mapping})"]
389389
first_rows = max_rows // 2 + max_rows % 2
390-
items = list(mapping.items())
391-
summary += [summarizer(k, v, col_width) for k, v in items[:first_rows]]
390+
keys = list(mapping.keys())
391+
summary += [summarizer(k, mapping[k], col_width) for k in keys[:first_rows]]
392392
if max_rows > 1:
393393
last_rows = max_rows // 2
394394
summary += [pretty_print(" ...", col_width) + " ..."]
395-
summary += [summarizer(k, v, col_width) for k, v in items[-last_rows:]]
395+
summary += [
396+
summarizer(k, mapping[k], col_width) for k in keys[-last_rows:]
397+
]
396398
else:
397399
summary += [summarizer(k, v, col_width) for k, v in mapping.items()]
398400
else:

0 commit comments

Comments
 (0)