Question: Could you please explain the following:1

This question is related to the Question Task/Threads simultaneous download

Could you please explain the following:


1) str1 and str2 should be global variables right? I mean so we can call them
after the Create statements....

2) When I execute the below code and then call str1 and str2 I dont get any html output.
The " " are empty.....why?

restart:
with(Sockets):
with(Threads):


X1 := proc () local str, sid, b;  global str1;

str1 := "";
sid := Open("google.com", 80);
Write(sid, cat("GET /finance/historical?q=INDEXSP:.INX&histperiod=daily HTTP/1.0 \n\n"));

b := Read(sid); while b <> false do str := cat(str, b); b := Read(sid) end do;

Close(sid);
str1 ;

end proc:


X2 := proc () local str, sid, b; global str2;

str2 := "";
sid := Open("google.com", 80);
Write(sid, cat("GET /finance/historical?q=INDEXEURO:.FCHI&histperiod=daily HTTP/1.0 \n\n"));

b := Read(sid); while b <> false do str := cat(str, b); b := Read(sid) end do;
Close(sid);
str2;

end proc:


Create(X1(), str1);
Create(X2(), str2);

str1;
str2;

                                                1
                                                2
                                                ""
                                                ""


3) what is the differens between task and thread ?  When should I use each of them ?

4) Why isnt there much different in speed in the below example. X1 counts the number of 7:s is
both columns. X2 counts the number of 7:s in the first column and X3 counts the number of 7:s
in the second column in parallel.

Now X1 and X2+X3 should produce the same result (which is does) and X2+X3 should be
much faster than X1. However this is not true. In this case X2+X3 actually takes longer.. why??


restart:
with(LinearAlgebra):
randomize():

dat := RandomMatrix(1000000, 2, generator = 1 .. 10):


X1 := proc () local c, i, j;

c := 0;

for i to 1000000 do
for j to 2 do

if dat[i, j] = 7 then c := c+1 end if

end do
end do;

c

end proc:



X2 := proc () local c, i;

c := 0;

for i to 1000000 do

if dat[i, 1] = 7 then c := c+1 end if

end do;

c

end proc:



X3 := proc () local c, i;

c := 0;

for i to 1000000 do if

dat[i, 2] = 7 then c := c+1 end if

end do;

c

end proc:




st := time():
X1();
"X1 time" = time()-st;


st := time():
data1 := Threads:-Task:-Start(X2):
data2 := Threads:-Task:-Start(X3):
data1+data2;
"X2+X3 time" = time()-st;


                                   199837
                              "X1 time" = 2.106
                                   199837
                            "X2+X3 time" = 2.184

Please Wait...