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

Alexis Cros Alexis.Cros at promes.cnrs.fr
Fri Jun 30 12:32:52 CEST 2017


Hi,

On my computer came, in order of rapidity, 1st case, 2nd, 4th then 3rd

  duration1  =

    0.1907777

  duration2  =

    0.2304603

  duration3  =

    23.275328 wow!

  duration4  =

    0.2531153

We may compare who has the best computer ^^


Le 29/06/2017 à 09:56, Carrico, Paul a écrit :
>
> Here is the correct code – loop faster here (68 time faster with n = 
> 100 000)
>
> I’m confuse
>
> 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_();
> i=1:(upper_bound-lower_bound+1)';
> c(i,1)= zeros((upper_bound-lower_bound+1),1);
> c(i,1)= _eval_(b(lower_bound:upper_bound,1));
> duration1= _toc_()
>
> /// case 2 : eval on the complete matrix/
>
> _tic_();
>
> d= zeros((upper_bound-lower_bound+1),1);
>
> d= string(d);
>
> d= b(lower_bound:upper_bound,1);
>
> d= _eval_(d);
>
> duration2= _toc_()
>
> /// case 3 :with an uggly loop/
>
> _tic_();
>
> e= zeros((upper_bound-lower_bound+1),1);
>
> fori = 1 : (upper_bound - lower_bound+1)
>
> 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),1);
>
> f= string(f);
>
> fori = 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*
>
> *De :*users [mailto:users-bounces at lists.scilab.org] *De la part de* 
> Carrico, Paul
> *Envoyé :* jeudi 29 juin 2017 09:29
> *À :* International users mailing list for Scilab. 
> (users at lists.scilab.org)
> *Objet :* [EXTERNAL] [Scilab-users] my loop faster than vectorization 
> (???)
>
> 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);
>
> fori = 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);
>
> fori = 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*
>
>
>
> _______________________________________________
> users mailing list
> users at lists.scilab.org
> http://lists.scilab.org/mailman/listinfo/users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.scilab.org/pipermail/users/attachments/20170630/589f9f76/attachment.htm>


More information about the users mailing list