This page was generated from docs/examples/DataSet/Dataset_Performance.ipynb. Interactive online version: Binder badge.

DataSet Performance

This notebook shows the trade-off between inserting data into a database row-by-row and as binary blobs. Inserting the data row-by-row means that we have direct access to all the data and may perform queries directly on the values of the data. On the other hand, as we shall see, this is much slower than inserting the data directly as binary blobs.

First, we choose a new location for the database to ensure that we don’t add a bunch of benchmarking data to the default one.

[1]:
import os

import qcodes as qc

cwd = os.getcwd()
qc.config["core"]["db_location"] = os.path.join(cwd, "testing.db")
Logging hadn't been started.
Activating auto-logging. Current session state plus future input saved.
Filename       : /home/runner/.qcodes/logs/command_history.log
Mode           : append
Output logging : True
Raw input log  : False
Timestamping   : True
State          : active
Qcodes Logfile : /home/runner/.qcodes/logs/241008-12060-qcodes.log
[2]:
%matplotlib inline
import time
from pathlib import Path

import matplotlib.pyplot as plt
import numpy as np

import qcodes as qc
from qcodes.dataset import (
    Measurement,
    initialise_or_create_database_at,
    load_or_create_experiment,
)
from qcodes.parameters import ManualParameter
[3]:
initialise_or_create_database_at(Path.cwd() / "dataset_performance.db")
exp = load_or_create_experiment(experiment_name="tutorial_exp", sample_name="no sample")

Here, we define a simple function to benchmark the time it takes to insert n points with either numeric or array data type. We will compare both the time used to call add_result and the time used for the full measurement.

[4]:
def insert_data(paramtype, npoints, nreps=1):
    meas = Measurement(exp=exp)

    x1 = ManualParameter("x1")
    x2 = ManualParameter("x2")
    x3 = ManualParameter("x3")
    y1 = ManualParameter("y1")
    y2 = ManualParameter("y2")

    meas.register_parameter(x1, paramtype=paramtype)
    meas.register_parameter(x2, paramtype=paramtype)
    meas.register_parameter(x3, paramtype=paramtype)
    meas.register_parameter(y1, setpoints=[x1, x2, x3], paramtype=paramtype)
    meas.register_parameter(y2, setpoints=[x1, x2, x3], paramtype=paramtype)
    start = time.perf_counter()
    with meas.run() as datasaver:
        start_adding = time.perf_counter()
        for i in range(nreps):
            datasaver.add_result(
                (x1, np.random.rand(npoints)),
                (x2, np.random.rand(npoints)),
                (x3, np.random.rand(npoints)),
                (y1, np.random.rand(npoints)),
                (y2, np.random.rand(npoints)),
            )
        stop_adding = time.perf_counter()
        run_id = datasaver.run_id
    stop = time.perf_counter()
    tot_time = stop - start
    add_time = stop_adding - start_adding
    return tot_time, add_time, run_id

Comparison between numeric/array data and binary blob

Case1: Short experiment time

[5]:
sizes = [1, 500, 1000, 2000, 3000, 4000, 5000]
t_numeric = []
t_numeric_add = []
t_array = []
t_array_add = []
for size in sizes:
    tn, tna, run_id_n = insert_data("numeric", size)
    t_numeric.append(tn)
    t_numeric_add.append(tna)

    ta, taa, run_id_a = insert_data("array", size)
    t_array.append(ta)
    t_array_add.append(taa)
Starting experimental run with id: 1.
Starting experimental run with id: 2.
Starting experimental run with id: 3.
Starting experimental run with id: 4.
Starting experimental run with id: 5.
Starting experimental run with id: 6.
Starting experimental run with id: 7.
Starting experimental run with id: 8.
Starting experimental run with id: 9.
Starting experimental run with id: 10.
Starting experimental run with id: 11.
Starting experimental run with id: 12.
Starting experimental run with id: 13.
Starting experimental run with id: 14.
[6]:
fig, ax = plt.subplots(1, 1)
ax.plot(sizes, t_numeric, "o-", label="Inserting row-by-row")
ax.plot(sizes, t_numeric_add, "o-", label="Inserting row-by-row: add_result only")
ax.plot(sizes, t_array, "d-", label="Inserting as binary blob")
ax.plot(sizes, t_array_add, "d-", label="Inserting as binary blob: add_result only")
ax.legend()
ax.set_xlabel("Array length")
ax.set_ylabel("Time (s)")
fig.tight_layout()
../../_images/examples_DataSet_Dataset_Performance_10_0.png

As shown in the latter figure, the time to setup and and close the experiment is approximately 0.4 sec. In case of small array sizes, the difference between inserting values of data as arrays and inserting them row-by-row is relatively unimportant. At larger array sizes, i.e. above 10000 points, the cost of writing data as individual datapoints starts to become important.

Case2: Long experiment time

[7]:
sizes = [1, 500, 1000, 2000, 3000, 4000, 5000]
nreps = 100
t_numeric = []
t_numeric_add = []
t_numeric_run_ids = []
t_array = []
t_array_add = []
t_array_run_ids = []
for size in sizes:
    tn, tna, run_id_n = insert_data("numeric", size, nreps=nreps)
    t_numeric.append(tn)
    t_numeric_add.append(tna)
    t_numeric_run_ids.append(run_id_n)

    ta, taa, run_id_a = insert_data("array", size, nreps=nreps)
    t_array.append(ta)
    t_array_add.append(taa)
    t_array_run_ids.append(run_id_a)
Starting experimental run with id: 15.
Starting experimental run with id: 16.
Starting experimental run with id: 17.
Starting experimental run with id: 18.
Starting experimental run with id: 19.
Starting experimental run with id: 20.
Starting experimental run with id: 21.
Starting experimental run with id: 22.
Starting experimental run with id: 23.
Starting experimental run with id: 24.
Starting experimental run with id: 25.
Starting experimental run with id: 26.
Starting experimental run with id: 27.
Starting experimental run with id: 28.
[8]:
fig, ax = plt.subplots(1, 1)
ax.plot(sizes, t_numeric, "o-", label="Inserting row-by-row")
ax.plot(sizes, t_numeric_add, "o-", label="Inserting row-by-row: add_result only")
ax.plot(sizes, t_array, "d-", label="Inserting as binary blob")
ax.plot(sizes, t_array_add, "d-", label="Inserting as binary blob: add_result only")
ax.legend()
ax.set_xlabel("Array length")
ax.set_ylabel("Time (s)")
fig.tight_layout()
../../_images/examples_DataSet_Dataset_Performance_14_0.png

However, as we increase the length of the experiment, as seen here by repeating the insertion 100 times, we see a big difference between inserting values of the data row-by-row and inserting it as a binary blob.

Loading the data

[9]:
from qcodes.dataset import load_by_id

As usual you can load the data by using the load_by_id function but you will notice that the different storage methods are reflected in shape of the data as it is retrieved.

[10]:
run_id_n = t_numeric_run_ids[0]
run_id_a = t_array_run_ids[0]
[11]:
ds = load_by_id(run_id_n)
ds.get_parameter_data("x1")
[11]:
{'x1': {'x1': array([0.03329051, 0.03329051, 0.32932212, 0.32932212, 0.43727484,
         0.43727484, 0.85207834, 0.85207834, 0.58730025, 0.58730025,
         0.27832434, 0.27832434, 0.03460887, 0.03460887, 0.75133689,
         0.75133689, 0.63709093, 0.63709093, 0.88086298, 0.88086298,
         0.68354495, 0.68354495, 0.65336203, 0.65336203, 0.20025082,
         0.20025082, 0.50431677, 0.50431677, 0.74535292, 0.74535292,
         0.27044505, 0.27044505, 0.94304053, 0.94304053, 0.97466145,
         0.97466145, 0.72054555, 0.72054555, 0.32318813, 0.32318813,
         0.97812024, 0.97812024, 0.89265238, 0.89265238, 0.94437642,
         0.94437642, 0.62185162, 0.62185162, 0.08135166, 0.08135166,
         0.23580773, 0.23580773, 0.83977717, 0.83977717, 0.93020276,
         0.93020276, 0.41414258, 0.41414258, 0.18184505, 0.18184505,
         0.16522042, 0.16522042, 0.05502545, 0.05502545, 0.02314229,
         0.02314229, 0.07394564, 0.07394564, 0.16890928, 0.16890928,
         0.51187749, 0.51187749, 0.52873634, 0.52873634, 0.17656994,
         0.17656994, 0.79436959, 0.79436959, 0.07032538, 0.07032538,
         0.77330677, 0.77330677, 0.93233888, 0.93233888, 0.07921368,
         0.07921368, 0.54259784, 0.54259784, 0.00701252, 0.00701252,
         0.63734841, 0.63734841, 0.92473061, 0.92473061, 0.01885314,
         0.01885314, 0.56614351, 0.56614351, 0.21840803, 0.21840803,
         0.90052309, 0.90052309, 0.23210786, 0.23210786, 0.03548724,
         0.03548724, 0.45341576, 0.45341576, 0.91846645, 0.91846645,
         0.39372212, 0.39372212, 0.96267536, 0.96267536, 0.81863432,
         0.81863432, 0.70956654, 0.70956654, 0.95430105, 0.95430105,
         0.00445095, 0.00445095, 0.44679782, 0.44679782, 0.81633244,
         0.81633244, 0.50225672, 0.50225672, 0.26115074, 0.26115074,
         0.27558225, 0.27558225, 0.70411666, 0.70411666, 0.96545456,
         0.96545456, 0.34217654, 0.34217654, 0.30715941, 0.30715941,
         0.81645425, 0.81645425, 0.68573888, 0.68573888, 0.67961969,
         0.67961969, 0.79408738, 0.79408738, 0.87632658, 0.87632658,
         0.69624986, 0.69624986, 0.81183946, 0.81183946, 0.29227242,
         0.29227242, 0.77892597, 0.77892597, 0.36583174, 0.36583174,
         0.43329475, 0.43329475, 0.42709115, 0.42709115, 0.9862701 ,
         0.9862701 , 0.62647511, 0.62647511, 0.76083429, 0.76083429,
         0.51979863, 0.51979863, 0.52648463, 0.52648463, 0.35684169,
         0.35684169, 0.50120875, 0.50120875, 0.33770825, 0.33770825,
         0.85479457, 0.85479457, 0.31681498, 0.31681498, 0.06134297,
         0.06134297, 0.23678924, 0.23678924, 0.89638095, 0.89638095,
         0.95780725, 0.95780725, 0.04306291, 0.04306291, 0.01266276,
         0.01266276, 0.83057988, 0.83057988, 0.67504535, 0.67504535])}}

And a dataset stored as binary arrays

[12]:
ds = load_by_id(run_id_a)
ds.get_parameter_data("x1")
[12]:
{'x1': {'x1': array([[0.77060112],
         [0.77060112],
         [0.85353611],
         [0.85353611],
         [0.75200571],
         [0.75200571],
         [0.82866891],
         [0.82866891],
         [0.04678231],
         [0.04678231],
         [0.66439723],
         [0.66439723],
         [0.10882243],
         [0.10882243],
         [0.24885664],
         [0.24885664],
         [0.70692039],
         [0.70692039],
         [0.31030153],
         [0.31030153],
         [0.37720202],
         [0.37720202],
         [0.24305263],
         [0.24305263],
         [0.39637943],
         [0.39637943],
         [0.53832263],
         [0.53832263],
         [0.63783675],
         [0.63783675],
         [0.46649661],
         [0.46649661],
         [0.65728075],
         [0.65728075],
         [0.84284878],
         [0.84284878],
         [0.55739915],
         [0.55739915],
         [0.73907418],
         [0.73907418],
         [0.76133154],
         [0.76133154],
         [0.85424779],
         [0.85424779],
         [0.00136484],
         [0.00136484],
         [0.72435799],
         [0.72435799],
         [0.08868316],
         [0.08868316],
         [0.72747562],
         [0.72747562],
         [0.68827571],
         [0.68827571],
         [0.88084854],
         [0.88084854],
         [0.69914878],
         [0.69914878],
         [0.48743759],
         [0.48743759],
         [0.38059883],
         [0.38059883],
         [0.69168136],
         [0.69168136],
         [0.39056783],
         [0.39056783],
         [0.10911313],
         [0.10911313],
         [0.99066458],
         [0.99066458],
         [0.89887921],
         [0.89887921],
         [0.94682322],
         [0.94682322],
         [0.84215564],
         [0.84215564],
         [0.334819  ],
         [0.334819  ],
         [0.57972925],
         [0.57972925],
         [0.89310259],
         [0.89310259],
         [0.86317279],
         [0.86317279],
         [0.98220565],
         [0.98220565],
         [0.82721408],
         [0.82721408],
         [0.04282397],
         [0.04282397],
         [0.52210793],
         [0.52210793],
         [0.55590688],
         [0.55590688],
         [0.02420798],
         [0.02420798],
         [0.94172501],
         [0.94172501],
         [0.80639489],
         [0.80639489],
         [0.32288216],
         [0.32288216],
         [0.13513743],
         [0.13513743],
         [0.47224419],
         [0.47224419],
         [0.18412918],
         [0.18412918],
         [0.43850689],
         [0.43850689],
         [0.0676546 ],
         [0.0676546 ],
         [0.44542959],
         [0.44542959],
         [0.96808532],
         [0.96808532],
         [0.97769415],
         [0.97769415],
         [0.29150723],
         [0.29150723],
         [0.90186636],
         [0.90186636],
         [0.4710415 ],
         [0.4710415 ],
         [0.11873752],
         [0.11873752],
         [0.38808226],
         [0.38808226],
         [0.00796757],
         [0.00796757],
         [0.87497222],
         [0.87497222],
         [0.91490133],
         [0.91490133],
         [0.25798018],
         [0.25798018],
         [0.60711928],
         [0.60711928],
         [0.02032944],
         [0.02032944],
         [0.93986451],
         [0.93986451],
         [0.7204433 ],
         [0.7204433 ],
         [0.8136443 ],
         [0.8136443 ],
         [0.42838536],
         [0.42838536],
         [0.65952224],
         [0.65952224],
         [0.79086627],
         [0.79086627],
         [0.02551954],
         [0.02551954],
         [0.98475246],
         [0.98475246],
         [0.64231414],
         [0.64231414],
         [0.40153228],
         [0.40153228],
         [0.27254324],
         [0.27254324],
         [0.4916695 ],
         [0.4916695 ],
         [0.80807562],
         [0.80807562],
         [0.78196683],
         [0.78196683],
         [0.51074955],
         [0.51074955],
         [0.87533164],
         [0.87533164],
         [0.53150612],
         [0.53150612],
         [0.15138057],
         [0.15138057],
         [0.11916008],
         [0.11916008],
         [0.47984277],
         [0.47984277],
         [0.5791047 ],
         [0.5791047 ],
         [0.3139373 ],
         [0.3139373 ],
         [0.25585515],
         [0.25585515],
         [0.39163301],
         [0.39163301],
         [0.05213349],
         [0.05213349],
         [0.4966405 ],
         [0.4966405 ],
         [0.57253717],
         [0.57253717],
         [0.84616713],
         [0.84616713],
         [0.7799186 ],
         [0.7799186 ],
         [0.69944826],
         [0.69944826]])}}
[ ]: