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

classic Classic list List threaded Threaded
6 messages Options
Carrico, Paul Carrico, Paul
Reply | Threaded
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
Carrico, Paul Carrico, Paul
Reply | Threaded
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
Alexx Alexx
Reply | Threaded
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
Carrico, Paul Carrico, Paul
Reply | Threaded
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
Rafael Guerra Rafael Guerra
Reply | Threaded
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
Rafael Guerra Rafael Guerra
Reply | Threaded
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 typeof ‘constant’, with nor further need for eval/evstr.

 

Rgds,

Rafael


_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users
Loading...