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

cwd = os.getcwd()
import qcodes as qc

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/240712-13172-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.15708807, 0.15708807, 0.79418078, 0.79418078, 0.20721966,
         0.20721966, 0.39974113, 0.39974113, 0.11604016, 0.11604016,
         0.960433  , 0.960433  , 0.84337958, 0.84337958, 0.61662845,
         0.61662845, 0.5644887 , 0.5644887 , 0.48830327, 0.48830327,
         0.29010426, 0.29010426, 0.52767143, 0.52767143, 0.4081032 ,
         0.4081032 , 0.52339414, 0.52339414, 0.93084057, 0.93084057,
         0.70590394, 0.70590394, 0.40236204, 0.40236204, 0.14180282,
         0.14180282, 0.71872993, 0.71872993, 0.96082759, 0.96082759,
         0.71350665, 0.71350665, 0.96412301, 0.96412301, 0.71998653,
         0.71998653, 0.6337231 , 0.6337231 , 0.46810811, 0.46810811,
         0.06770862, 0.06770862, 0.83913795, 0.83913795, 0.84392554,
         0.84392554, 0.62703762, 0.62703762, 0.89808743, 0.89808743,
         0.76374992, 0.76374992, 0.94214748, 0.94214748, 0.16731922,
         0.16731922, 0.4157601 , 0.4157601 , 0.52674182, 0.52674182,
         0.82954331, 0.82954331, 0.47653919, 0.47653919, 0.18938951,
         0.18938951, 0.12742258, 0.12742258, 0.72191995, 0.72191995,
         0.39438782, 0.39438782, 0.24376127, 0.24376127, 0.83610935,
         0.83610935, 0.46485501, 0.46485501, 0.88604657, 0.88604657,
         0.43818265, 0.43818265, 0.42183697, 0.42183697, 0.82893613,
         0.82893613, 0.41914725, 0.41914725, 0.66526577, 0.66526577,
         0.21998492, 0.21998492, 0.84118872, 0.84118872, 0.17535551,
         0.17535551, 0.00563487, 0.00563487, 0.73492974, 0.73492974,
         0.47629392, 0.47629392, 0.25491587, 0.25491587, 0.05375134,
         0.05375134, 0.97733392, 0.97733392, 0.59393419, 0.59393419,
         0.98496071, 0.98496071, 0.70925832, 0.70925832, 0.3775926 ,
         0.3775926 , 0.19260407, 0.19260407, 0.92400417, 0.92400417,
         0.99388209, 0.99388209, 0.32706121, 0.32706121, 0.68119753,
         0.68119753, 0.68766437, 0.68766437, 0.53875375, 0.53875375,
         0.48964564, 0.48964564, 0.00562066, 0.00562066, 0.73870651,
         0.73870651, 0.20133591, 0.20133591, 0.24893986, 0.24893986,
         0.05589401, 0.05589401, 0.91895741, 0.91895741, 0.32587468,
         0.32587468, 0.63646535, 0.63646535, 0.64515703, 0.64515703,
         0.97848381, 0.97848381, 0.85033732, 0.85033732, 0.50453844,
         0.50453844, 0.48084864, 0.48084864, 0.81305846, 0.81305846,
         0.89396205, 0.89396205, 0.12369326, 0.12369326, 0.06520642,
         0.06520642, 0.39996286, 0.39996286, 0.49019051, 0.49019051,
         0.06414698, 0.06414698, 0.26005864, 0.26005864, 0.43089135,
         0.43089135, 0.32210571, 0.32210571, 0.83332982, 0.83332982,
         0.36860636, 0.36860636, 0.36501089, 0.36501089, 0.49846407,
         0.49846407, 0.2371005 , 0.2371005 , 0.61255409, 0.61255409])}}

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.02395921],
         [0.02395921],
         [0.47383639],
         [0.47383639],
         [0.36159951],
         [0.36159951],
         [0.48221003],
         [0.48221003],
         [0.31314542],
         [0.31314542],
         [0.60873178],
         [0.60873178],
         [0.54947794],
         [0.54947794],
         [0.60778917],
         [0.60778917],
         [0.8436389 ],
         [0.8436389 ],
         [0.52132086],
         [0.52132086],
         [0.91545057],
         [0.91545057],
         [0.77690453],
         [0.77690453],
         [0.65814483],
         [0.65814483],
         [0.82180449],
         [0.82180449],
         [0.60254445],
         [0.60254445],
         [0.55502942],
         [0.55502942],
         [0.17004356],
         [0.17004356],
         [0.28740526],
         [0.28740526],
         [0.95071467],
         [0.95071467],
         [0.57109656],
         [0.57109656],
         [0.80534415],
         [0.80534415],
         [0.84900109],
         [0.84900109],
         [0.91692472],
         [0.91692472],
         [0.85168405],
         [0.85168405],
         [0.27873381],
         [0.27873381],
         [0.28760611],
         [0.28760611],
         [0.09097395],
         [0.09097395],
         [0.64914709],
         [0.64914709],
         [0.82746661],
         [0.82746661],
         [0.34324573],
         [0.34324573],
         [0.29528613],
         [0.29528613],
         [0.86956851],
         [0.86956851],
         [0.82102588],
         [0.82102588],
         [0.30371136],
         [0.30371136],
         [0.87731112],
         [0.87731112],
         [0.38304085],
         [0.38304085],
         [0.34412377],
         [0.34412377],
         [0.10090747],
         [0.10090747],
         [0.57057319],
         [0.57057319],
         [0.27853609],
         [0.27853609],
         [0.74291669],
         [0.74291669],
         [0.20536696],
         [0.20536696],
         [0.45614937],
         [0.45614937],
         [0.8976734 ],
         [0.8976734 ],
         [0.80826655],
         [0.80826655],
         [0.57156133],
         [0.57156133],
         [0.00423627],
         [0.00423627],
         [0.52451222],
         [0.52451222],
         [0.69725321],
         [0.69725321],
         [0.27540047],
         [0.27540047],
         [0.29563359],
         [0.29563359],
         [0.76118795],
         [0.76118795],
         [0.3827162 ],
         [0.3827162 ],
         [0.01367039],
         [0.01367039],
         [0.05666185],
         [0.05666185],
         [0.26916277],
         [0.26916277],
         [0.2444314 ],
         [0.2444314 ],
         [0.15835341],
         [0.15835341],
         [0.91889321],
         [0.91889321],
         [0.92791085],
         [0.92791085],
         [0.3004108 ],
         [0.3004108 ],
         [0.08580764],
         [0.08580764],
         [0.08892929],
         [0.08892929],
         [0.1398329 ],
         [0.1398329 ],
         [0.57079247],
         [0.57079247],
         [0.15882077],
         [0.15882077],
         [0.68317723],
         [0.68317723],
         [0.73871378],
         [0.73871378],
         [0.76177622],
         [0.76177622],
         [0.21831058],
         [0.21831058],
         [0.04093148],
         [0.04093148],
         [0.25801646],
         [0.25801646],
         [0.20322269],
         [0.20322269],
         [0.27640245],
         [0.27640245],
         [0.45487513],
         [0.45487513],
         [0.96366961],
         [0.96366961],
         [0.25590515],
         [0.25590515],
         [0.39661777],
         [0.39661777],
         [0.1332253 ],
         [0.1332253 ],
         [0.2828667 ],
         [0.2828667 ],
         [0.24674851],
         [0.24674851],
         [0.56152777],
         [0.56152777],
         [0.07905094],
         [0.07905094],
         [0.01506298],
         [0.01506298],
         [0.68105411],
         [0.68105411],
         [0.98507357],
         [0.98507357],
         [0.83154103],
         [0.83154103],
         [0.87509316],
         [0.87509316],
         [0.97542395],
         [0.97542395],
         [0.29430021],
         [0.29430021],
         [0.9711763 ],
         [0.9711763 ],
         [0.28710783],
         [0.28710783],
         [0.30757226],
         [0.30757226],
         [0.16104486],
         [0.16104486],
         [0.65001995],
         [0.65001995],
         [0.63928761],
         [0.63928761],
         [0.56081319],
         [0.56081319],
         [0.97183167],
         [0.97183167],
         [0.01686016],
         [0.01686016],
         [0.38900945],
         [0.38900945]])}}
[ ]: