在matlab中使用tic toc函数。

时间:2021-04-21 22:49:52

I have these two different ways to implement the same thing but I guess the second is the best. However, I get a better result when using tic toc for the first. How comes ?

我有两种不同的方法来实现相同的东西,但我想第二种方法是最好的。但是,在使用tic toc的时候,我得到了一个更好的结果。是如何?

j=6;
i=j;
Savings = zeros(i,j);
Costs = magic(400);

tic;
for x=2:i
   for y=2:j
     if(x ~= y)
       Savings(x,y) = Costs(x,1) + Costs(1,y) - Costs(x,y);
     end
   end
end
first=toc;

disp(num2str(first))

Savings = zeros(i,j);

tic;
Ix=2:i; 
Iy=2:j;
I = false(i,j);
I(Ix,Iy) = bsxfun(@ne, Ix', Iy);
S = bsxfun(@plus, Costs(Ix,1), Costs(1,Iy)) - Costs(Ix,Iy);
Savings(I) = S(I(Ix,Iy));
second=toc;

temp = Savings;
disp(num2str(second))

1 个解决方案

#1


6  

It depends on how MATLAB's JIT engine can improve the performance of for loops. For small matrices it works fine but for large ones not really. Seems for i less than 60, first method is faster, but not for larger matrices. Try this benchmark

这取决于MATLAB的JIT引擎如何改进for循环的性能。对于小矩阵来说,它很好用,但对于大的矩阵来说,它并不是很好。似乎小于60,第一种方法更快,但对于更大的矩阵来说不是这样。试试这个基准

for j=[6 30 60 100 200 400 600]
    disp(['j=' num2str(j)]);
    i=j;
    Savings = zeros(i,j);
    Costs = magic(600);

    tic;
    for mm=1:1e2
        for x=2:i
            for y=2:j
                if(x ~= y)
                    Savings(x,y) = Costs(x,1) + Costs(1,y) - Costs(x,y);
                end
            end
        end
    end
    first=toc;

    disp(num2str(first));

    Savings = zeros(i,j);

    tic;
    for mm=1:1e2
        Ix=2:i;
        Iy=2:j;
        I = false(i,j);
        I(Ix,Iy) = bsxfun(@ne, Ix', Iy);
        S = bsxfun(@plus, Costs(Ix,1), Costs(1,Iy)) - Costs(Ix,Iy);
        Savings(I) = S(I(Ix,Iy));
    end
    second=toc;

    temp = Savings;
    disp(num2str(second))
end

On my machine, it returns:

在我的机器上,它返回:

j=6
0.0001874
0.0052893
j=30
0.0034454
0.0057184
j=60
0.011097
0.01268
j=100
0.027957
0.023952
j=200
0.11529
0.058686
j=400
0.45791
0.37246
j=600
1.1496
0.74932

#1


6  

It depends on how MATLAB's JIT engine can improve the performance of for loops. For small matrices it works fine but for large ones not really. Seems for i less than 60, first method is faster, but not for larger matrices. Try this benchmark

这取决于MATLAB的JIT引擎如何改进for循环的性能。对于小矩阵来说,它很好用,但对于大的矩阵来说,它并不是很好。似乎小于60,第一种方法更快,但对于更大的矩阵来说不是这样。试试这个基准

for j=[6 30 60 100 200 400 600]
    disp(['j=' num2str(j)]);
    i=j;
    Savings = zeros(i,j);
    Costs = magic(600);

    tic;
    for mm=1:1e2
        for x=2:i
            for y=2:j
                if(x ~= y)
                    Savings(x,y) = Costs(x,1) + Costs(1,y) - Costs(x,y);
                end
            end
        end
    end
    first=toc;

    disp(num2str(first));

    Savings = zeros(i,j);

    tic;
    for mm=1:1e2
        Ix=2:i;
        Iy=2:j;
        I = false(i,j);
        I(Ix,Iy) = bsxfun(@ne, Ix', Iy);
        S = bsxfun(@plus, Costs(Ix,1), Costs(1,Iy)) - Costs(Ix,Iy);
        Savings(I) = S(I(Ix,Iy));
    end
    second=toc;

    temp = Savings;
    disp(num2str(second))
end

On my machine, it returns:

在我的机器上,它返回:

j=6
0.0001874
0.0052893
j=30
0.0034454
0.0057184
j=60
0.011097
0.01268
j=100
0.027957
0.023952
j=200
0.11529
0.058686
j=400
0.45791
0.37246
j=600
1.1496
0.74932