4 Replies - 8726 Views - Last Post: 15 August 2012 - 04:07 AM Rate Topic: -----

#1 squarewavedreams  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 13-March 09

MATLAB: Measuring time and memory used by a function?

Posted 05 May 2009 - 10:43 AM

I have a project which involves running two filters -- rcosflt() and a customized version provided by the profesor -- separately on a random array of data. In addition to the results, I need to find out how much time and memory each one took to arrive at their result. I'm completely new to MATLAB and the professor has been, well, unhelpful. Thoughts?

This post has been edited by squarewavedreams: 05 May 2009 - 10:45 AM

Is This A Good Question/Topic? 0
  • +

Replies To: MATLAB: Measuring time and memory used by a function?

#2 paperclipmuffin  Icon User is offline

  • Disassembling...
  • member icon

Reputation: 13
  • View blog
  • Posts: 944
  • Joined: 16-April 09

Re: MATLAB: Measuring time and memory used by a function?

Posted 09 May 2009 - 12:25 AM

Could you please post the code you have used so far?

Thank you for helping us to help you. :D
Was This Post Helpful? 0
  • +
  • -

#3 squarewavedreams  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 13-March 09

Re: MATLAB: Measuring time and memory used by a function?

Posted 09 May 2009 - 10:21 PM

I hadn't written the code yet when I posted, so I apologize. Here it is so far:

Delay = 3; DataL = 20; R= .5; Fs = 8; Fd = 1;
x = round(rand([DataL,1]));
tx = (0: DataL - 1) ./ Fd + Delay / Fd;
subplot(3,1,1);
stem(tx, x, 'kx');
axis([0 30 -1.6 1.6]); xlabel('Time'); ylabel('Amplitude');
title('Unfiltered data with delay compensation');
[yf, tf] = rcosine(Fd, Fs, 'fir', R, Delay);
[yo1, to1] = rcosflt(x, Fd, Fs, 'filter', yf);
[yo2, to2] = rcos2dflt(x, Fd, Fs, yf, Delay);
subplot(3,1,2);
stem(tx, x, 'kx'); hold on;
plot(to1, yo1, 'b-'); hold off;
axis([0 30 -1.6 1.6]); xlabel('Time'); ylabel('Amplitude');
title('Filtered with rcosflt()');
subplot(3,1,3);
stem(tx, x, 'kx'); hold on;
plot(to2, yo2, 'b-'); hold off;
axis([0 30 -1.6 1.6]); xlabel('Time'); ylabel('Amplitude');
title('Filtered with two-index algorithm');



So what I need is some way to measure and report the efficiency of the rcosflt() and the custom rcos2dflt() function in lines 9 and 10 respectively. The professor simply asked to compare 'time and memory usage', but I at least need to know how much time each one takes.

EDIT: I've been told on another forum to use tic, toc, and whos to accomplish this. I'm about to start working on this, but if anyone comes up with a better solution, I'm all ears. Thanks!

This post has been edited by squarewavedreams: 09 May 2009 - 10:28 PM

Was This Post Helpful? 0
  • +
  • -

#4 InfiniteL00P  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 12
  • Joined: 05-November 09

Re: MATLAB: Measuring time and memory used by a function?

Posted 05 November 2009 - 06:32 PM

Hello,

In order to measure the memory used by the function, all you have to do is have it saved to your current directory and enter whos <name of function> in the command window and it will return where the function is stored, data types, total memory etc.

Ex:
 
myfunction(some input) //Say your function was called 'myfunction'

whos myfunction //will return information about the function myfunction.m




Also, in order to measure the time it takes to run your function, just open the function in the editor, add tic where you want to start recording and then toc at the end. The time taken to execute will be printed in the command window.

Ex:
function [output] = myfunction(input)

tic //Start timer
count = 0;
idx = 0;
for idx = 1:length(input)
	 if count <= length(input)
		  count = count + 1
	 end
end
output = count
toc //Stop Timer
end

//You could also step through your function in the debugger line by line using the tic and toc keywords.


This post has been edited by InfiniteL00P: 05 November 2009 - 10:24 PM

Was This Post Helpful? 1

#5 rakesh1988  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 15-August 12

Re: MATLAB: Measuring time and memory used by a function?

Posted 15 August 2012 - 04:07 AM

View PostInfiniteL00P, on 05 November 2009 - 06:32 PM, said:

Hello,

In order to measure the memory used by the function, all you have to do is have it saved to your current directory and enter whos <name of function> in the command window and it will return where the function is stored, data types, total memory etc.

Ex:
 
myfunction(some input) //Say your function was called 'myfunction'

whos myfunction //will return information about the function myfunction.m




Also, in order to measure the time it takes to run your function, just open the function in the editor, add tic where you want to start recording and then toc at the end. The time taken to execute will be printed in the command window.

Ex:
function [output] = myfunction(input)

tic //Start timer
count = 0;
idx = 0;
for idx = 1:length(input)
	 if count <= length(input)
		  count = count + 1
	 end
end
output = count
toc //Stop Timer
end

//You could also step through your function in the debugger line by line using the tic and toc keywords.




Thanks a lot for the information. I am not able to use whos on a function to get the memory used by it.
here is my code
function closing_price_code
clear;
tic;
load('cell_close_mat.mat');
struct_close_price = cell2struct(cell_close, {'sl_no', 'closing_price'}, 2);
[cA, cD] = dwt([struct_close_price.closing_price],'db4');
toc;
end


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1