Skip to content

Commit

Permalink
Expand benchmarks for dataset insertion and creation
Browse files Browse the repository at this point in the history
Taken from discussions in pydata#7224 (comment)

Thank you @Illviljan
  • Loading branch information
hmaarrfk committed Oct 29, 2022
1 parent 51d37d1 commit 3343df1
Showing 1 changed file with 45 additions and 1 deletion.
46 changes: 45 additions & 1 deletion asv_bench/benchmarks/merge.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import xarray as xr
import numpy as np


class DatasetAddVariable:
Expand All @@ -13,6 +14,49 @@ def setup(self, existing_elements):
d[f"var{i}"] = i
self.dataset = xr.merge([d])

def time_variable_insertion(self, existin_elements):
d = {
"set_2_{i}": i
for i in range(existing_elements)
}
self.dataset2 = xr.merge([d])

def time_variable_insertion(self, existing_elements):
dataset = self.dataset
dataset["new_var"] = 0

def time_merge_two_datasets(self, existing_elements):
xr.merge([self.dataset, self.dataset2])

def time_dict_of_dataarrays_to_dataset(self, existing_elements):
# The idea here is to time how long it takes to go from numpy
# and python data types, to a full dataset
# See discussion
# https://github.com/pydata/xarray/issues/7224#issuecomment-1292216344
value = np.array(["0", "b"], dtype=str)
time = np.array([0, 1])
coords = dict(time=time)
data_vars = {
"long_variable_name_{i}": xr.DataArray(data=value, coords=coords)
for i in range(existing_elements)
}
xr.Dataset(data_vars=data_vars, coords=coords)

def time_dict_of_variables_to_dataset(self, existing_elements):
value = np.array(["0", "b"], dtype=str)
time = np.array([0, 1])
coords = dict(time=time)
data_vars = {
"long_variable_name_{i}": xr.Variable("time", value)
for i in range(existing_elements)
}
xr.Dataset(data_vars=data_vars, coords=coords)

def time_dict_of_tuples_to_dataset(self, existing_elements):
value = np.array(["0", "b"], dtype=str)
time = np.array([0, 1])
coords = dict(time=time)
data_vars = {
"long_variable_name_{i}": ("time", value)
for i in range(existing_elements)
}
xr.Dataset(data_vars=data_vars, coords=coords)

0 comments on commit 3343df1

Please sign in to comment.