Sunday, February 7, 2010

Use stopwatch to measure the time elapsed

 run 3D random walk for 30 times and get the average time of each run.

% random walk
for k=1:30
tic;
x=[];
y=[];
z=[];
x(1)=0;
y(1)=0;
z(1)=0;
tTotal=0;
for i=1:1000
    J=rand*6;
if J<1
    x(i+1)=x(i)+1;
    y(i+1)=y(i);
    z(i+1)=z(i);
elseif (J>1)&(J<2)
    x(i+1)=x(i)-1;
    y(i+1)=y(i);
    z(i+1)=z(i);
elseif (J>2)&(J<3)
    x(i+1)=x(i);
    y(i+1)=y(i)+1;
    z(i+1)=z(i);
elseif (J>3)&(J<4)
    x(i+1)=x(i);
    y(i+1)=y(i)-1;
    z(i+1)=z(i);
elseif (J>4)&(J<5)
    x(i+1)=x(i);
    y(i+1)=y(i);
    z(i+1)=z(i)+1;
else
    x(i+1)=x(i);
    y(i+1)=y(i);
    z(i+1)=z(i)-1;
end
end
plot3(x,y,z)
t(i)=toc;
tTotal=tTotal+t(i);
end
tAve=tTotal/10;

Resutls:
when i=1:1000, tAve=0.0012s
when i=1:2000, tAve=0.0032s
when i=1:4000, tAve=0.0114s
when i=1:6000, tAve=0.0506s
when i=1:12000, tAve=0.1882s
when i=1:24000, tAve=0.8823s.
when i=1:48000, tAve=2.7501s.

No comments:

Post a Comment

Any comments?

my-alpine and docker-compose.yml

 ``` version: '1' services:     man:       build: .       image: my-alpine:latest   ```  Dockerfile: ``` FROM alpine:latest ENV PYTH...