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/241122-11839-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.63581664, 0.63581664, 0.2812134 , 0.2812134 , 0.38687088,
         0.38687088, 0.68185841, 0.68185841, 0.22550649, 0.22550649,
         0.2844444 , 0.2844444 , 0.23891695, 0.23891695, 0.80683357,
         0.80683357, 0.89972263, 0.89972263, 0.40473255, 0.40473255,
         0.6178444 , 0.6178444 , 0.00633971, 0.00633971, 0.7210003 ,
         0.7210003 , 0.57740702, 0.57740702, 0.51006672, 0.51006672,
         0.52925399, 0.52925399, 0.78000257, 0.78000257, 0.4319463 ,
         0.4319463 , 0.40206225, 0.40206225, 0.72847395, 0.72847395,
         0.59139582, 0.59139582, 0.85922638, 0.85922638, 0.87045593,
         0.87045593, 0.13403759, 0.13403759, 0.82234437, 0.82234437,
         0.72384752, 0.72384752, 0.79563002, 0.79563002, 0.55181219,
         0.55181219, 0.09217348, 0.09217348, 0.82104468, 0.82104468,
         0.67879167, 0.67879167, 0.97049308, 0.97049308, 0.82989368,
         0.82989368, 0.19397254, 0.19397254, 0.00427016, 0.00427016,
         0.59453932, 0.59453932, 0.06342033, 0.06342033, 0.58348321,
         0.58348321, 0.80766763, 0.80766763, 0.88264406, 0.88264406,
         0.64583876, 0.64583876, 0.01074128, 0.01074128, 0.20756411,
         0.20756411, 0.80231753, 0.80231753, 0.88420573, 0.88420573,
         0.83142867, 0.83142867, 0.69376662, 0.69376662, 0.83610458,
         0.83610458, 0.40023542, 0.40023542, 0.68533697, 0.68533697,
         0.51510457, 0.51510457, 0.45126702, 0.45126702, 0.79836966,
         0.79836966, 0.67643759, 0.67643759, 0.5415006 , 0.5415006 ,
         0.76958251, 0.76958251, 0.12431946, 0.12431946, 0.78075332,
         0.78075332, 0.4552585 , 0.4552585 , 0.08199686, 0.08199686,
         0.50303865, 0.50303865, 0.776032  , 0.776032  , 0.65388215,
         0.65388215, 0.66326111, 0.66326111, 0.39566101, 0.39566101,
         0.49231836, 0.49231836, 0.78725074, 0.78725074, 0.11098695,
         0.11098695, 0.88571349, 0.88571349, 0.83926489, 0.83926489,
         0.4590238 , 0.4590238 , 0.49512691, 0.49512691, 0.45453389,
         0.45453389, 0.8314975 , 0.8314975 , 0.2357356 , 0.2357356 ,
         0.20081717, 0.20081717, 0.23086016, 0.23086016, 0.27099161,
         0.27099161, 0.9302292 , 0.9302292 , 0.161762  , 0.161762  ,
         0.33427661, 0.33427661, 0.78027231, 0.78027231, 0.50764547,
         0.50764547, 0.43314187, 0.43314187, 0.62251819, 0.62251819,
         0.06139786, 0.06139786, 0.14175678, 0.14175678, 0.6412269 ,
         0.6412269 , 0.91108422, 0.91108422, 0.26430197, 0.26430197,
         0.93504551, 0.93504551, 0.77379015, 0.77379015, 0.19802871,
         0.19802871, 0.56342293, 0.56342293, 0.70136915, 0.70136915,
         0.35853542, 0.35853542, 0.35399282, 0.35399282, 0.89535814,
         0.89535814, 0.50948573, 0.50948573, 0.72092195, 0.72092195])}}

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.14363923],
         [0.14363923],
         [0.41561176],
         [0.41561176],
         [0.13325221],
         [0.13325221],
         [0.29382898],
         [0.29382898],
         [0.83718938],
         [0.83718938],
         [0.26210915],
         [0.26210915],
         [0.56596063],
         [0.56596063],
         [0.11076017],
         [0.11076017],
         [0.16408748],
         [0.16408748],
         [0.93757284],
         [0.93757284],
         [0.65436013],
         [0.65436013],
         [0.51060029],
         [0.51060029],
         [0.77822869],
         [0.77822869],
         [0.4951609 ],
         [0.4951609 ],
         [0.34510207],
         [0.34510207],
         [0.83183575],
         [0.83183575],
         [0.28077611],
         [0.28077611],
         [0.64144778],
         [0.64144778],
         [0.68871241],
         [0.68871241],
         [0.2259748 ],
         [0.2259748 ],
         [0.32922425],
         [0.32922425],
         [0.32757647],
         [0.32757647],
         [0.13924564],
         [0.13924564],
         [0.77785295],
         [0.77785295],
         [0.80904586],
         [0.80904586],
         [0.0207014 ],
         [0.0207014 ],
         [0.45074694],
         [0.45074694],
         [0.2306415 ],
         [0.2306415 ],
         [0.59333361],
         [0.59333361],
         [0.29682571],
         [0.29682571],
         [0.72577953],
         [0.72577953],
         [0.34272853],
         [0.34272853],
         [0.76843937],
         [0.76843937],
         [0.21490996],
         [0.21490996],
         [0.4576829 ],
         [0.4576829 ],
         [0.68038011],
         [0.68038011],
         [0.48723503],
         [0.48723503],
         [0.62120516],
         [0.62120516],
         [0.49536701],
         [0.49536701],
         [0.76296797],
         [0.76296797],
         [0.80743509],
         [0.80743509],
         [0.7256313 ],
         [0.7256313 ],
         [0.00469677],
         [0.00469677],
         [0.12505539],
         [0.12505539],
         [0.14661709],
         [0.14661709],
         [0.31712091],
         [0.31712091],
         [0.0386482 ],
         [0.0386482 ],
         [0.77060516],
         [0.77060516],
         [0.40211669],
         [0.40211669],
         [0.11368134],
         [0.11368134],
         [0.16318235],
         [0.16318235],
         [0.02131501],
         [0.02131501],
         [0.24205628],
         [0.24205628],
         [0.30594458],
         [0.30594458],
         [0.65557481],
         [0.65557481],
         [0.17698094],
         [0.17698094],
         [0.79911655],
         [0.79911655],
         [0.98203201],
         [0.98203201],
         [0.78997305],
         [0.78997305],
         [0.51856417],
         [0.51856417],
         [0.47569123],
         [0.47569123],
         [0.05269378],
         [0.05269378],
         [0.00830284],
         [0.00830284],
         [0.68219361],
         [0.68219361],
         [0.0915459 ],
         [0.0915459 ],
         [0.40068216],
         [0.40068216],
         [0.62836148],
         [0.62836148],
         [0.75618966],
         [0.75618966],
         [0.66430319],
         [0.66430319],
         [0.15894626],
         [0.15894626],
         [0.7956938 ],
         [0.7956938 ],
         [0.88369101],
         [0.88369101],
         [0.84760467],
         [0.84760467],
         [0.45511127],
         [0.45511127],
         [0.76019512],
         [0.76019512],
         [0.39321636],
         [0.39321636],
         [0.03729683],
         [0.03729683],
         [0.62376544],
         [0.62376544],
         [0.98207849],
         [0.98207849],
         [0.54366328],
         [0.54366328],
         [0.42208709],
         [0.42208709],
         [0.6328519 ],
         [0.6328519 ],
         [0.12358163],
         [0.12358163],
         [0.03475722],
         [0.03475722],
         [0.04468951],
         [0.04468951],
         [0.56703841],
         [0.56703841],
         [0.08328008],
         [0.08328008],
         [0.42772336],
         [0.42772336],
         [0.47145508],
         [0.47145508],
         [0.83401704],
         [0.83401704],
         [0.33631092],
         [0.33631092],
         [0.04794245],
         [0.04794245],
         [0.10241021],
         [0.10241021],
         [0.70378287],
         [0.70378287],
         [0.32147878],
         [0.32147878],
         [0.25982631],
         [0.25982631],
         [0.14022996],
         [0.14022996],
         [0.16070115],
         [0.16070115],
         [0.80208884],
         [0.80208884],
         [0.25844985],
         [0.25844985]])}}
[ ]: