matlab怎么看程序运行时间

matlab怎么看程序运行时间,第1张

 如果要计算一段程序运行时间,可以在这段程序的第一句前添加语句“tic”,在该端程序后添加语句“t=toc”即可,那么返回值t即为该段程序所耗时间。

看下例:

a=[1:900000]

b=zeros(1,900000)

tic

for i=1:900000

b(i)=2*a(i)

end

t=toc

a为一个递增数组,b是一个与a同大小的空数组。可能有人问,为什么要先或局创建一个空数组,原因是这样的,事先声明一个数组然后往里填数,这样比来一个数扩充一个友团让数的位置要省好局时间的。

代码比较简单,只是你需要做个界面(DateTime.fig),然后你需要一个代码文件(DateTime.m)

--------------------------------------------------------------------------------------------------------------------

核心部分:

1.建立触发定时器(DateTime_OpeningFcn):

T = timer('Period',1.0,'ExecutionMode','FixedRate', 'TimerFcn'亏困渣,{@onTimer,[handles.edit1,handles.edit2]})

start(T)

2.定时器运行时的代码:

function onTimer (obj,events,arg)

data=datestr(date,29)

cur_data = data

cur_time = fix(clock)

% str = sprintf('尺族%s %.2d:%.2d:%.2d', cur_data, cur_time(4), cur_time(5), cur_time(6))

str1 = sprintf('%s', cur_data)

str2 = sprintf('%.2d:%.2d:%.2d', cur_time(4), cur_time(5), cur_time(6))

set(arg(1),'String',str1)

set(arg(2),'String',str2)

3.关闭代码:

close

--------------------------------------------------------------------------------------------------------------------

DateTime.fig文件界面如截图所示

--------------------------------------------------------------------------------------------------------------------

DateTime.m文件代码(全):

function varargout = DateTime(varargin)

% DATETIME M-file for DateTime.fig

%      DATETIME, by itself, creates a new DATETIME or raises the existing

%      singleton*.

%

%      H = DATETIME returns the handle to a new DATETIME or the handle to

%      the existing singleton*.

%

%  销悄    DATETIME('CALLBACK',hObject,eventData,handles,...) calls the local

%      function named CALLBACK in DATETIME.M with the given input arguments.

%

%      DATETIME('Property','Value',...) creates a new DATETIME or raises the

%      existing singleton*.  Starting from the left, property value pairs are

%      applied to the GUI before DateTime_OpeningFcn gets called.  An

%      unrecognized property name or invalid value makes property application

%      stop.  All inputs are passed to DateTime_OpeningFcn via varargin.

%

%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one

%      instance to run (singleton)".

%

% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help DateTime

% Last Modified by GUIDE v2.5 22-Nov-2014 12:04:39

clc

% Begin initialization code - DO NOT EDIT

gui_Singleton = 1

gui_State = struct('gui_Name',       mfilename, ...

                 'gui_Singleton',  gui_Singleton, ...

                 'gui_OpeningFcn', @DateTime_OpeningFcn, ...

                 'gui_OutputFcn',  @DateTime_OutputFcn, ...

                 'gui_LayoutFcn',  [] , ...

                 'gui_Callback',   [])

if nargin &&ischar(varargin{1})

  gui_State.gui_Callback = str2func(varargin{1})

end

if nargout

  [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:})

else

  gui_mainfcn(gui_State, varargin{:})

end

% End initialization code - DO NOT EDIT

function onTimer (obj,events,arg)

data=datestr(date,29)

cur_data = data

cur_time = fix(clock)

% str = sprintf('%s %.2d:%.2d:%.2d', cur_data, cur_time(4), cur_time(5), cur_time(6))

str1 = sprintf('%s', cur_data)

str2 = sprintf('%.2d:%.2d:%.2d', cur_time(4), cur_time(5), cur_time(6))

set(arg(1),'String',str1)

set(arg(2),'String',str2)

%  set(arg,'String','111111')

% --- Executes just before DateTime is made visible.

function DateTime_OpeningFcn(hObject, eventdata, handles, varargin)

% This function has no output args, see OutputFcn.

% hObject    handle to figure

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    structure with handles and user data (see GUIDATA)

% varargin   command line arguments to DateTime (see VARARGIN)

% Choose default command line output for DateTime

handles.output = hObject

% Update handles structure

guidata(hObject, handles)

T = timer('Period',1.0,'ExecutionMode','FixedRate', 'TimerFcn',{@onTimer,[handles.edit1,handles.edit2]})

start(T)

% UIWAIT makes DateTime wait for user response (see UIRESUME)

% uiwait(handles.figure1)

% --- Outputs from this function are returned to the command line.

function varargout = DateTime_OutputFcn(hObject, eventdata, handles)

% varargout  cell array for returning output args (see VARARGOUT)

% hObject    handle to figure

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure

varargout{1} = handles.output

function edit1_Callback(hObject, eventdata, handles)

% hObject    handle to edit1 (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit1 as text

%        str2double(get(hObject,'String')) returns contents of edit1 as a double

% get(handles.edit1,'string')

% --- Executes during object creation, after setting all properties.

function edit1_CreateFcn(hObject, eventdata, handles)

% hObject    handle to edit1 (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.

%       See ISPC and COMPUTER.

if ispc &&isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))

  set(hObject,'BackgroundColor','white')

end

function edit2_Callback(hObject, eventdata, handles)

% hObject    handle to edit2 (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit2 as text

%        str2double(get(hObject,'String')) returns contents of edit2 as a double

% --- Executes during object creation, after setting all properties.

function edit2_CreateFcn(hObject, eventdata, handles)

% hObject    handle to edit2 (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.

%       See ISPC and COMPUTER.

if ispc &&isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))

  set(hObject,'BackgroundColor','white')

end

% --- Executes on button press in pushbutton1.

function pushbutton1_Callback(hObject, eventdata, handles)

% hObject    handle to pushbutton1 (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    structure with handles and user data (see GUIDATA)

close

Matlab中要实现计时功能,要用到MATLAB自带的函数。

我给你推春蠢荐几种函数:

cputime 显示所占用的CPU时间;

tic,toc 秒表态厅计时,tic是开始,toc是结束;

clock,etime 前者扒闭陪显示系统时间,后者计算两次调用clock之间的时间差。

例如:

1 t0 = cputime你的程序;time=cputime-t0

2 tic你的程序;toc

3 t0 = clock你的程序;time = etime(clock, t0)

最近在做数学问题,也在学习MATLAB,希望对你有所帮助!


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12522947.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-26
下一篇 2023-05-26

发表评论

登录后才能评论

评论列表(0条)

保存