⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 5-14.m

📁 《MATLAB 7.0编程基础》第5章 (源码实例)主要讲解MATLAB的编程基础
💻 M
字号:
%scirpt file: timings.m
%purpose:
%This program calculates the time required to calculate the square
%of all integers from 1 to 10,000 in the three different ways:
%1. Using a for loop with an uninitialized output array
%2. Using a for loop with a pre-allocated output array
%3. Using vectors
%Define variables:
%ii,jj --Loop index
%average1  --Average time for calculation 1
%average2  --Average time for calculation 2
%average3  --Average time for calculation 3
%square --array of squares
%month   --Month(mm)
%year --Year(yyyy)
maxcount=1;
tic;
for jj=1:maxcount
    clear square
    for ii=1:10,00
        square(ii)=ii^2;
    end
end
average1=(toc)/maxcount;
%perform calculation with a pre-allocated array
maxcount=10;
tic;
for jj=1:maxcount
    clear square
    square(ii)=zeros(1,10000);
    for ii=1:10,00
        square(ii)=ii^2;
    end
end
average2=(toc)/maxcount;
%Perform calculation with vectors
maxcount=100;
tic;
for jj=1:maxcount
    clear square
    ii=1:10000;
    square(ii)=ii^2;
end
average3=(toc)/maxcount;
%display results
fprintf('loop/uninitialized array =%8.4f\n,...
        average1');
fprintf('loop/initialized array =%8.4f\n,...
        average2');
fprintf(' vector =%8.4f\n,...
        average3');

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -