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

6 messages
Open this post in threaded view
|
Report Content as Inappropriate

## [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); 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   _______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users
Open this post in threaded view
|
Report Content as Inappropriate

## Re: my loop faster than vectorization (???)

 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); for i = 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); 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   De : users [mailto:[hidden email]] De la part de Carrico, Paul EnvoyĂ© : jeudi 29 juin 2017 09:29 Ă€ : International users mailing list for Scilab. ([hidden email]) 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); 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   _______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users
Open this post in threaded view
|
Report Content as Inappropriate

## Re: my loop faster than vectorization (???)

 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); for i = 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); 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 Â  DeÂ : users [[hidden email]] De la part de Carrico, Paul EnvoyĂ©Â : jeudi 29 juin 2017 09:29 Ă€Â : International users mailing list for Scilab. ([hidden email]) 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); 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 Â  ```_______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users ``` _______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users
Open this post in threaded view
|
Report Content as Inappropriate

## Re: [EXTERNAL] Re: my loop faster than vectorization (???)

 Hi   Interesting feedback, but different of mine J   With n=100000 with my working station under Windows 7.0 with Scilab 5.5.2 (from memory I noticed the same behavior on my old laptop under Ubuntu 16.04 LTS)   duration1  =     93.057   duration2  =     108.325   duration3  =     1.279   duration3  =     135.178      Nota: nevertheless for huge asci files containing millions of lines (not in the same format of course) , I finally decided to “outsource”  this work   Paul   EXPORT CONTROL : Cet email ne contient pas de donnĂ©es techniques This email does not contain technical data   De : users [mailto:[hidden email]] De la part de Alexis Cros EnvoyĂ© : vendredi 30 juin 2017 12:33 Ă€ : Users mailing list for Scilab Objet : [EXTERNAL] Re: [Scilab-users] my loop faster than vectorization (???)   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); for i = 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); 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   De : users [[hidden email]] De la part de Carrico, Paul EnvoyĂ© : jeudi 29 juin 2017 09:29 Ă€ : International users mailing list for Scilab. ([hidden email]) 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); 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   `_______________________________________________` ` ` `users mailing list` ` ` `[hidden email]` ` ` `http://lists.scilab.org/mailman/listinfo/users` ` `   _______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users test_boucles.sce (1K) Download Attachment
Open this post in threaded view
|
Report Content as Inappropriate

## Re: [EXTERNAL] Re: my loop faster than vectorization (???)

 Hi,   For huge matrices of strings like your variable “b”, eval()  or evstr() do not seem to work efficiently in Scilab 5.5.2.   However, if you have a fast hard disk (SSD), it will be comparatively lightning fast to do the conversion via I/O functions such as: csvWrite(b,"outb.csv"); c=csvRead("outb.csv");   Then use c in your code, which has now type of ‘constant’, with nor further need for eval/evstr.   Rgds, Rafael   From: users [mailto:[hidden email]] On Behalf Of Carrico, Paul Sent: Friday, June 30, 2017 12:46 PM To: Users mailing list for Scilab <[hidden email]> Subject: Re: [Scilab-users] [EXTERNAL] Re: my loop faster than vectorization (???)   Hi   Interesting feedback, but different of mine J   With n=100000 with my working station under Windows 7.0 with Scilab 5.5.2 (from memory I noticed the same behavior on my old laptop under Ubuntu 16.04 LTS)   duration1  =       93.057   duration2  =       108.325   duration3  =       1.279   duration3  =       135.178      Nota: nevertheless for huge asci files containing millions of lines (not in the same format of course) , I finally decided to “outsource”  this work   Paul     EXPORT CONTROL : Cet email ne contient pas de donnĂ©es techniques This email does not contain technical data   De : users [[hidden email]] De la part de Alexis Cros EnvoyĂ© : vendredi 30 juin 2017 12:33 Ă€ : Users mailing list for Scilab Objet : [EXTERNAL] Re: [Scilab-users] my loop faster than vectorization (???)   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); for i = 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); 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   De : users [[hidden email]] De la part de Carrico, Paul EnvoyĂ© : jeudi 29 juin 2017 09:29 Ă€ : International users mailing list for Scilab. ([hidden email]) 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); 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   `_______________________________________________` ` ` `users mailing list` ` ` `[hidden email]` ` ` `http://lists.scilab.org/mailman/listinfo/users` ` `   _______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users