Tuesday, April 1, 2014

[Trouble shooting / MATLAB] Data memory allocation, speed acceleration, code optimization

If you put a data into an array or any memory repeatedly,
to allocate memory is much x1000000 faster than enlarge memory every iteration.




Code example.

#1. repeatedly enlarge memory (worst case)

     DB          = struct('phoneme',phone, 'cnt', 0, 'waveform',[]);
     DB.cnt      = DB_in.cnt;
   
    for ii = 1 : DB.cnt
        DB.waveform(ii,:) = func_fix_window(DB_in.waveform{ii}, L);
    end




#2. pre-allocate memory with size (it is much faster)

    DB = struct('phoneme',phone, 'cnt',DB_in.cnt, 'waveform',zeros(DB_in.cnt,L));
   
    for ii = 1 : DB.cnt
        DB.waveform(ii,:) = func_fix_window(DB_in.waveform{ii}, L);
    end








No comments:

Post a Comment