0

I have a question regarding the processing time of copying data into another array. I noticed that it takes much more time to copy complex single data compared to normal single value. Even if I preallocate both arrays.

% Example to show different processing speed of copying data 
T1=0; % total time for single 
T2=0; % total time for complex single 
% preallocate rrays
Csingle = single(zeros(500,3000));
Cimagsingle =complex(Csingle);
for i=1:1000;
    A =rand(500,3000,'single');
    B = 1i.*A;
    tic ;
    C = A;
    t1=toc;
    T1=T1+t1;
    tic;
    Cimag = B;
    t2=toc ;
    T2=T2+t2;
end

The processing time in this example is

T1 = 0.6105

and

T2 = 1.1430

which is approximatley twice as slow!?

I do not understand this behavior. In a program I am writing to acquire real time data I need to copy complex data into a new array but the processing speed is way to slow. As a result my program is not able to function in real time.

Adriaan
  • 17,741
  • 7
  • 42
  • 75
Luapham
  • 11
  • 2
  • 8
    You mean that for a number defined as having 2 components (a+bi), stuff take twice the time as for a number defined as 1 component (a). Hummmmmm not sure why this surprises you. It is literally double the amount of data. – Ander Biguri Mar 16 '18 at 16:19

1 Answers1

3

A complex number is defined as a+bi, as opposed to a real number a. This means that for every complex number two real numbers are stored. Hence when copying an array of size x, it'll take twice as long for the array containing complex numbers as opposed to real numbers.

The same 2 fold difference shows up when using real(double) and real(single), as double is, as the name suggests, double the size of a single variable.

It's not exactly a two fold difference in time, as there's some overhead of the tic/toc functions, initial copying step etc.


Just as a note, using i as a loop variable is generally already frowned upon, and it's even worse when you're using a complex loop. Simply use ii or k or idx or something as loop variable, not i, especially when using complex numbers.

Adriaan
  • 17,741
  • 7
  • 42
  • 75