开发者

Plot time of two different for loops

开发者 https://www.devze.com 2023-02-10 06:57 出处:网络
I have two loops: for x = 1:100 tic for n=1:x #some code t(n) = toc 开发者_Python百科 end plot(t) end for y = 1:100

I have two loops:

for x = 1:100
    tic
    for n=1:x
       #some code
       t(n) = toc
   开发者_Python百科 end

    plot(t)

end

for y = 1:100
    tic
    for n=1:y
       #some code
       t(n) = toc
    end

plot(t)

end

If I just run one of the for loops and plot the time, it works perfectly. But I if I run both loops and I hold on from first to second series of plotting, I get lots of lines in the graph.

All I want is two graphs representing the two times of the functions plotted.


Here's how I would do it:

t1 = zeros(100*100);
i = 1;
for x = 1:100
    tic
    for n=1:x
       #some code
       t1(i) = toc
       i = i + 1;
    end
end


t2 = zeros(100*100);
i = 1;
for y = 1:100
    tic
    for n=1:y
       #some code
       t2(i) = toc
       i = i + 1;
    end
end

figure();
plot(t1);
hold on;
plot(t2)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号