[Scilab-users] my loop faster than vectorization (???)

Carrico, Paul paul.carrico at esterline.com
Thu Jun 29 09:28:40 CEST 2017


Hi all

I'm facing a case where a loop is faster than vectorization (at least I hope I'm using vectorization) ... I'm necessarily doing something wrong but I don't see what

Hope somebody will point out my mistake

Thanks for your time

Paul

################################################
mode(0)
clear

n = 10000;
lower_bound = 0.1*n;
upper_bound = 0.6*n;

a = rand(n,1);
b = string(a);

// case 1 : eval on each row
tic();
c = zeros((upper_bound-lower_bound),1);
c(1:(upper_bound-lower_bound),1) = eval(b(1:(upper_bound-lower_bound),1));
duration1 = toc()

// case 2 : eval on the complete matrix
tic();
d = zeros((upper_bound-lower_bound),1);
d = string(d);
d = b([1:(upper_bound-lower_bound)],1);
d = eval(d);
duration2 = toc()

// case 3 :with an uggly loop
tic();
e = zeros((upper_bound-lower_bound),1);
for i = 1 : (upper_bound - lower_bound)
    e(i,1) = eval(b(i+lower_bound-1,1));
end
duration3 = toc()

// case 4 :with an uggly loop (eval on the complete matrix)
tic();
f = zeros((upper_bound-lower_bound),1);
f = string(f);
for i = 1 : (upper_bound - lower_bound)
    f(i,1) = b(i+lower_bound-1,1);
end
f = eval(f);
duration3 = toc()

EXPORT CONTROL :
Cet email ne contient pas de données techniques
This email does not contain technical data

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.scilab.org/pipermail/users/attachments/20170629/81c4e214/attachment.htm>


More information about the users mailing list