Skip to content

Compare performance of IterativeDecoder and ChunkedDecoder

Owen Lloyd edited this page Jun 23, 2021 · 1 revision

Both IterativeDecoder and ChunkedDecoder work well with encoded sequences. Additionally, an IterativeDecoder can replace the functionality of a ChunkedDecoder as the iterator it returns can easily be converted to a list. Because of this we will compare the efficiency of IterativeDecoder and ChunkedDecoder to see if we should replace the functionality of ChunkedDecoder with IterativeDecoder.

from hum.gen.sine_mix import freq_based_stationary_wf
from recode import (ChunkedEncoder, 
                    ChunkedDecoder, 
                    IterativeDecoder, 
                    StructCodecSpecs)

Create a synthetic waveform to test both decoders on:

DFLT_N_SAMPLES = 21 * 2048
DFLT_SR = 44100

wf_mix = freq_based_stationary_wf(freqs=(200, 400, 600, 800), weights=None,
                             n_samples = DFLT_N_SAMPLES*300, sr = DFLT_SR)

Define the decoders and encode the waveform:

specs = StructCodecSpecs('d')
encoder = ChunkedEncoder(frame_to_chk=specs.frame_to_chk)
decoder = ChunkedDecoder(
    chk_size_bytes=specs.chk_size_bytes,
    chk_to_frame=specs.chk_to_frame,
    n_channels=specs.n_channels
)
idecoder = IterativeDecoder(chk_to_frame = specs.chk_to_frame_iter)
b = encoder(wf_mix)

Helper function so the output of the decoders are in the same format:

def it_to_list(it):
    frame = list(it)
    frame = [item for tup in frame for item in tup]
    return frame

Performance of ChunkedDecoder:

%timeit -r 2 -n 5 list(decoder(b))
5.59 s ± 14.5 ms per loop (mean ± std. dev. of 2 runs, 5 loops each)

Performance of IterativeDecoder:

%timeit -r 2 -n 5 it_to_list(idecoder(b))
2.04 s ± 13.4 ms per loop (mean ± std. dev. of 2 runs, 5 loops each)

From these results, we can clearly see that we should replace the functionality of ChunkedDecoder with IterativeDecoder as it is nearly three times more efficient.

Clone this wiki locally