ZeeUTao / zi_labrad

python module to control Zurich instruments in quantum computing
GNU General Public License v3.0
1 stars 2 forks source link

create list by built-in method instead of append #19

Closed ZeeUTao closed 4 years ago

ZeeUTao commented 4 years ago

In zilabrad.instrument.zurichHelper.zurich_qa, we notice that acquisition_poll() use append to create list,but we can replace it by the python build-in functions like map and lambda

### map version
chunks[p] = list(map(lambda v: v['vector'],dataset[p]))
num_obtained = sum([len(x) for x in chunks[p]])
if num_obtained >= num_samples:
    gotem[p] = True

### append version
for v in dataset[p]:
    chunks[p].append(v['vector'])
    num_obtained = sum([len(x) for x in chunks[p]])
    if num_obtained >= num_samples:
        gotem[p] = True

We can numerically test it

import numpy as np
import cProfile
import timeit
from timeit import Timer

dataset_p = list(range(1024))

def chunks_append():
    chunks = {'p':[]}
    for x in dataset_p:
        chunks['p'].append(x)

def chunks_map():
    chunks = {'p':[]}
    chunks['p'] = list(map(lambda x: x,dataset_p))

rep = 10000

time1 = Timer("chunks_append()","from __main__ import chunks_append").timeit(rep)
time2 = Timer("chunks_map()","from __main__ import chunks_map").timeit(rep)

# time1 0.9954
# time2 0.7891

In the real device

# append func

ncalls  tottime  percall  cumtime  percall
101   23.218    0.230   23.223    0.230 zurichHelper.py:371(acquisition_poll)
# map func
ncalls  tottime  percall  cumtime  percall
101   23.157    0.229   23.161    0.229 zurichHelper.py:371(acquisition_poll)
ZeeUTao commented 4 years ago

fixed by #21