Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: counting for loop operations

Subject: counting for loop operations

From: P Sherry

Date: 30 Jul, 2007 10:30:13

Message: 1 of 7

i am required to calculated the complexity of a program.

is there a way to count the number of "for loop" operations? or the amount of calculations carried out?

depending on the number of inputs, N, the loops operate for 1:N so i just want to compare the complexity as N increases

thanks
P

Subject: counting for loop operations

From: David

Date: 30 Jul, 2007 10:36:39

Message: 2 of 7

"P Sherry" <pistolpetesherry@nospam.hotmail.com> wrote in message <f8kejl$34r$1@fred.mathworks.com>...
> i am required to calculated the complexity of a program.
>
> is there a way to count the number of "for loop" operations? or the amount of calculations carried out?
>
> depending on the number of inputs, N, the loops operate for 1:N so i just want to compare the complexity as N increases
>
> thanks
> P

pardon me if i'm a bit out of date, but i don't think the number of iterations of a loop is part of the measure of complexity. it may relate to performance or other things. complexity is usually measured by things like 'how many for loops are there', 'how deeply nested are conditional statements', and stuff like that, not how many times the code gets executed.

Subject: counting for loop operations

From: P Sherry

Date: 30 Jul, 2007 11:04:53

Message: 3 of 7

> pardon me if i'm a bit out of date, but i don't think the number of iterations of a loop is part of the measure of complexity. it may relate to performance or other things. complexity is usually measured by things like 'how many for loops are there', 'how deeply nested are conditional statements', and stuff like that, not how many times the code gets executed.
>

i do agree but i want to draw a comparison between the number of iterations that take place and the N inputs.

any hints?

P

Subject: counting for loop operations

From: David

Date: 30 Jul, 2007 11:26:43

Message: 4 of 7

"P Sherry" <pistolpetesherry@nospam.hotmail.com> wrote in message <f8kgkk$5in$1@fred.mathworks.com>...
> > pardon me if i'm a bit out of date, but i don't think the number of iterations of a loop is part of the measure of complexity. it may relate to performance or other things. complexity is usually measured by things like 'how many for loops are there', 'how deeply nested are conditional statements', and stuff like that, not how many times the code gets executed.
> >
>
> i do agree but i want to draw a comparison between the number of iterations that take place and the N inputs.
>
> any hints?
>
> P

the number of iterations of a for loop that has a parameter like 1:N is just N. if it gets more complicated like 1:.1:N then you can do length(1:.1:N) to see how many there are. but somehow i don't think that is what you think you are looking for.

Subject: counting for loop operations

From: us

Date: 30 Jul, 2007 12:26:27

Message: 5 of 7

P Sherry:
<SNIP complex obfuscation...

> i am required to calculated the complexity of a program.
> depending on the number of inputs, N, the loops operate for 1:N so i just want to compare the complexity as N increases...

hmmmm (?), well, if you are looking for the <cyclomatic complexity>, aka <mc cabe complexity>, you could try

% mlint -m3 -cyc your_m_file;
% eg,
     mlint -m3 -cyc printdlg;

us

Subject: counting for loop operations

From: us

Date: 30 Jul, 2007 13:25:23

Message: 6 of 7

us:
<SNIP down to incomplete snippet...

> mlint -m3 -cyc printdlg;

note: the latest <mlint> versions (finally) return a structure and some...
therefore, if you need to save your output, you could do this

     fnam='printdlg.m';
     [s,ff]=mlint('-m3','-cyc',fnam);
     ff
     s
     {s.message}.'

us

Subject: counting for loop operations

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 30 Jul, 2007 19:26:36

Message: 7 of 7

In article <f8kevn$cgs$1@fred.mathworks.com>,
David <dave@bigcompany.com> wrote:

>pardon me if i'm a bit out of date, but i don't think the number of
>iterations of a loop is part of the measure of complexity. it may
>relate to performance or other things. complexity is usually measured
>by things like 'how many for loops are there', 'how deeply nested are
>conditional statements', and stuff like that, not how many times the
>code gets executed.

The OP is talking about "big-O" complexity, O(), the rate at which
the algorithm executes dependant upon the size of the data.

A for loop with constant bounds has O(c) (i.e., a constant)
for that subexpression, but a for loop 1:n where n is the
size of the input, has O(n) complexity, and for sufficiently large n
would operate more slowly than any particular O(c) expression,
even if that expression was hundreds of terms long.

--
Programming is what happens while you're busy making other plans.

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
mlint us 30 Jul, 2007 08:47:20
complexity us 30 Jul, 2007 08:31:42
mc cabe us 30 Jul, 2007 08:31:42
cyclomatic us 30 Jul, 2007 08:31:42
programming us 30 Jul, 2007 08:31:42
code us 30 Jul, 2007 08:31:42
rssFeed for this Thread

envelope graphic E-mail this page to a colleague

Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.
Related Topics