<rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0">
  <channel>
    <title>MaplePrimes - answers and comments on Question, Big O of a procedure</title>
    <link>http://www.mapleprimes.com/questions/35833-Big-O-Of-A-Procedure</link>
    <language>en-us</language>
    <copyright>2026 Maplesoft, A Division of Waterloo Maple Inc.</copyright>
    <generator>Maplesoft Document System</generator>
    <lastBuildDate>Fri, 12 Jun 2026 06:31:33 GMT</lastBuildDate>
    <pubDate>Fri, 12 Jun 2026 06:31:33 GMT</pubDate>
    <itunes:subtitle />
    <itunes:summary />
    <description>The latest answers and comments added to the Question, Big O of a procedure</description>
    <image>
      <url>http://www.mapleprimes.com/images/mapleprimeswhite.jpg</url>
      <title>MaplePrimes - answers and comments on Question, Big O of a procedure</title>
      <link>http://www.mapleprimes.com/questions/35833-Big-O-Of-A-Procedure</link>
    </image>
    <item>
      <title>Interesting question</title>
      <link>http://www.mapleprimes.com/questions/35833-Big-O-Of-A-Procedure?ref=Feed:MaplePrimes:Big O of a procedure:Comments#answer45282</link>
      <itunes:summary>&lt;p&gt;This is not an easy question to answer, first of all because people who want to know such things sometimes want different types of answers. I'll show you two methods that can be used, and only the first one will give you a true order calculation - but it's often a lot harder to do than the second one. I'll first give you a somewhat more theoretical overview, then examples.&lt;/p&gt;
&lt;p&gt;Basically, you count the number of basic operations that Maple needs to do in order to finish your algorithm, as a function of the size of your input, say &lt;i&gt;n&lt;/i&gt;. Often, the number of steps will depend on the actual input, not just its size, and in this case you need to find an upper bound on the number of basic operations. A basic operation should in this case be read as something that can always execute in a bounded amount of time, independent of the parameters of the operation. This can be very difficult to determine, because you have to know what algorithms Maple uses internally for certain operations; this is generally not too difficult for library-level Maple routines, but for Maple kernel routines it is less easy. As an example, assigning an entry in a table or Vector or Matrix is a basic operation, but adding an element to a list is not (in Maple).&lt;/p&gt;
&lt;p&gt;If you succeed in determining the number of basic operations, this will give you some algebraic expression involving &lt;i&gt;n&lt;/i&gt;. You are then allowed (technically not required) to omit all but the eventually largest term in an addition of a fixed number of terms, and omit constant factors, to simplify this algebraic expression.&lt;/p&gt;
&lt;p&gt;Let's take a simple bubble sort-like sorting algorithm as an example - as you said, we hope to get out O(n^2). We'll take a Vector of real numberic constants as input.&lt;/p&gt;
&lt;pre&gt;
bsort := proc(v :: Vector(numeric), $) :: Vector(numeric);
local 
&amp;nbsp; i :: posint, 
&amp;nbsp; j :: posint, 
&amp;nbsp; n :: nonnegint := LinearAlgebra:-Dimension(v);

&amp;nbsp; for i from n to 2 by -1 do
&amp;nbsp;&amp;nbsp;&amp;nbsp; for j to i - 1 do
&amp;nbsp; &amp;nbsp; &amp;nbsp; if v[j] &amp;gt; v[j+1] then
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; v[j], v[j+1] := v[j+1], v[j];
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; end if;
&amp;nbsp;&amp;nbsp;&amp;nbsp; end do;
&amp;nbsp; end do;

  return v;
end proc;
&lt;/pre&gt;
&lt;p&gt;The &lt;i&gt;if&lt;/i&gt;-block inside the loop will always execute in bounded time (*), so we'll count that as one operation, adding in the&amp;nbsp; bookkeeping overhead of increasing &lt;i&gt;j&lt;/i&gt; and such in the inner for loop. Then the inner for loop takes &lt;i&gt;i&lt;/i&gt; - 1 basic steps. The outer loop also has some bookkeeping overhead in each iteration, which we can account for by saying that the full inner loop plus bookkeeping for one iteration of the outer loop can be done in &lt;i&gt;i&lt;/i&gt; basic steps. For the full outer loop, we get &lt;maple&gt;Sum(i, i=2..n) = (n+1)^2/2 - n/2 - 3/2&lt;/maple&gt; steps in total. This is the algebraic expression that's our upper bound for the number of basic operations of the algorithm as a whole. If we expand that (using Maple's expand command) we get &lt;maple&gt;n^2/2 + n/2 - 1&lt;/maple&gt;.&amp;nbsp;Eventually, the quadratic term will be bigger than the other two terms, so we can omit the linear and the constant term. Then we can also omit the constant factor of a half, and we get the answer we were looking for: the algorithm is &lt;i&gt;O(n&lt;sup&gt;2&lt;/sup&gt;)&lt;/i&gt;.&lt;/p&gt;
&lt;p&gt;OK - that's it for the first method, which as I said is the only one that can actually get you a true &amp;quot;order&amp;quot; of an algorithm. However, people who ask for a big-O calculation are sometimes more interested in how well the algorithm will scale to large inputs in practice. The second method gives you such information: it is to just time your procedure, then fit a function to it. This is a piece of software engineering that you can very easily and get rough results, or do very well and spend a lot of time accounting for cache warmup etc. I'll show a very rough and easy solution here.&lt;/p&gt;
&lt;pre&gt;
timeit := proc(n :: posint, k :: posint, $) :: numeric;
local 
&amp;nbsp; vs :: list(Vector) := [seq(LinearAlgebra:-RandomVector(n, generator = 1. .. 2.), i=1..k)],
&amp;nbsp; i :: posint,
&amp;nbsp; t, t0;

&amp;nbsp; t0 := time();
&amp;nbsp; map(bsort, vs);
&amp;nbsp; t := time() - t0;

&amp;nbsp; return t / k;
end proc;
nvalues := [seq(ceil(5 * 1.2^k), k=0..29)];
times := [seq(timeit(n, min(5000, ceil(10^6/n^2))), n in nvalues)];
plots:-pointplot(nvalues, times, axis=[mode=log]);
Statistics[PowerFit](nvalues[4..], times[4..], n);
&lt;/pre&gt;
&lt;p&gt;This yields, after some computation, first the following plot:&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;a href="http://www.mapleprimes.com/viewfile/4071"&gt;&lt;img alt="" src="http://www.mapleprimes.com/scripts/image.php?image=http://www.mapleprimes.com/files/8808_logplot.gif&amp;amp;width=300&amp;amp;height=300" /&gt;&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;We see that the points are almost on a straight line, except for the first three points (which I therefore discard). Then we find the fitted expression 0.00000276 * n^1.92 (with more digits, but let's forget about those) (and this number is from my new core i7 720QM laptop). This suggests approximately quadratic behaviour.&lt;/p&gt;
&lt;p&gt;Hope this helps,&lt;/p&gt;
&lt;p&gt;Erik Postma&lt;br /&gt;
Maplesoft.&lt;/p&gt;
&lt;p&gt;(*) Here's an example of how difficult it is to do this in a general-purpose computer algebra system. In an earlier version of this comment, the procedure took a Vector(realcons) as an argument instead of a Vector(numeric). When I wrote the sentence with the footnote mark, I realized that the procedure wouldn't work in general. Writing &lt;i&gt;if v[j] &amp;gt; v[j+1]&lt;/i&gt; is a somewhat dangerous thing to do, specifically because Maple has been written so that you can write that and know that it will execute in bounded time. The price to pay, however, is that the comparison is not very powerful. If v[j] and v[j+1] are both of type numeric, then you're safe. However, symbolic constants like Pi cannot be compared like this, because it might require a long computation to ascertain which is the bigger one (or indeed, if the two constants are different). For example, if you would write &lt;i&gt;if x &amp;gt; y then ...&lt;/i&gt; while having &lt;i&gt;x&lt;/i&gt; = Pi and &lt;i&gt;y&lt;/i&gt; = 2, then Maple will complain. If you want to risk the somewhat more lengthy computation, you can write &lt;i&gt;if is(x &amp;gt; y)&lt;/i&gt; or if you like living dangerously, &lt;i&gt;if evalf(x - y &amp;gt; 0)&lt;/i&gt;. However, then the count for the big-O computation cannot be bounded easily without further knowledge of the structure of &lt;i&gt;x&lt;/i&gt; and &lt;i&gt;y&lt;/i&gt;.&lt;/p&gt;</itunes:summary>
      <description>&lt;p&gt;This is not an easy question to answer, first of all because people who want to know such things sometimes want different types of answers. I'll show you two methods that can be used, and only the first one will give you a true order calculation - but it's often a lot harder to do than the second one. I'll first give you a somewhat more theoretical overview, then examples.&lt;/p&gt;
&lt;p&gt;Basically, you count the number of basic operations that Maple needs to do in order to finish your algorithm, as a function of the size of your input, say &lt;i&gt;n&lt;/i&gt;. Often, the number of steps will depend on the actual input, not just its size, and in this case you need to find an upper bound on the number of basic operations. A basic operation should in this case be read as something that can always execute in a bounded amount of time, independent of the parameters of the operation. This can be very difficult to determine, because you have to know what algorithms Maple uses internally for certain operations; this is generally not too difficult for library-level Maple routines, but for Maple kernel routines it is less easy. As an example, assigning an entry in a table or Vector or Matrix is a basic operation, but adding an element to a list is not (in Maple).&lt;/p&gt;
&lt;p&gt;If you succeed in determining the number of basic operations, this will give you some algebraic expression involving &lt;i&gt;n&lt;/i&gt;. You are then allowed (technically not required) to omit all but the eventually largest term in an addition of a fixed number of terms, and omit constant factors, to simplify this algebraic expression.&lt;/p&gt;
&lt;p&gt;Let's take a simple bubble sort-like sorting algorithm as an example - as you said, we hope to get out O(n^2). We'll take a Vector of real numberic constants as input.&lt;/p&gt;
&lt;pre&gt;
bsort := proc(v :: Vector(numeric), $) :: Vector(numeric);
local 
&amp;nbsp; i :: posint, 
&amp;nbsp; j :: posint, 
&amp;nbsp; n :: nonnegint := LinearAlgebra:-Dimension(v);

&amp;nbsp; for i from n to 2 by -1 do
&amp;nbsp;&amp;nbsp;&amp;nbsp; for j to i - 1 do
&amp;nbsp; &amp;nbsp; &amp;nbsp; if v[j] &amp;gt; v[j+1] then
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; v[j], v[j+1] := v[j+1], v[j];
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; end if;
&amp;nbsp;&amp;nbsp;&amp;nbsp; end do;
&amp;nbsp; end do;

  return v;
end proc;
&lt;/pre&gt;
&lt;p&gt;The &lt;i&gt;if&lt;/i&gt;-block inside the loop will always execute in bounded time (*), so we'll count that as one operation, adding in the&amp;nbsp; bookkeeping overhead of increasing &lt;i&gt;j&lt;/i&gt; and such in the inner for loop. Then the inner for loop takes &lt;i&gt;i&lt;/i&gt; - 1 basic steps. The outer loop also has some bookkeeping overhead in each iteration, which we can account for by saying that the full inner loop plus bookkeeping for one iteration of the outer loop can be done in &lt;i&gt;i&lt;/i&gt; basic steps. For the full outer loop, we get &lt;maple&gt;Sum(i, i=2..n) = (n+1)^2/2 - n/2 - 3/2&lt;/maple&gt; steps in total. This is the algebraic expression that's our upper bound for the number of basic operations of the algorithm as a whole. If we expand that (using Maple's expand command) we get &lt;maple&gt;n^2/2 + n/2 - 1&lt;/maple&gt;.&amp;nbsp;Eventually, the quadratic term will be bigger than the other two terms, so we can omit the linear and the constant term. Then we can also omit the constant factor of a half, and we get the answer we were looking for: the algorithm is &lt;i&gt;O(n&lt;sup&gt;2&lt;/sup&gt;)&lt;/i&gt;.&lt;/p&gt;
&lt;p&gt;OK - that's it for the first method, which as I said is the only one that can actually get you a true &amp;quot;order&amp;quot; of an algorithm. However, people who ask for a big-O calculation are sometimes more interested in how well the algorithm will scale to large inputs in practice. The second method gives you such information: it is to just time your procedure, then fit a function to it. This is a piece of software engineering that you can very easily and get rough results, or do very well and spend a lot of time accounting for cache warmup etc. I'll show a very rough and easy solution here.&lt;/p&gt;
&lt;pre&gt;
timeit := proc(n :: posint, k :: posint, $) :: numeric;
local 
&amp;nbsp; vs :: list(Vector) := [seq(LinearAlgebra:-RandomVector(n, generator = 1. .. 2.), i=1..k)],
&amp;nbsp; i :: posint,
&amp;nbsp; t, t0;

&amp;nbsp; t0 := time();
&amp;nbsp; map(bsort, vs);
&amp;nbsp; t := time() - t0;

&amp;nbsp; return t / k;
end proc;
nvalues := [seq(ceil(5 * 1.2^k), k=0..29)];
times := [seq(timeit(n, min(5000, ceil(10^6/n^2))), n in nvalues)];
plots:-pointplot(nvalues, times, axis=[mode=log]);
Statistics[PowerFit](nvalues[4..], times[4..], n);
&lt;/pre&gt;
&lt;p&gt;This yields, after some computation, first the following plot:&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;a href="http://www.mapleprimes.com/viewfile/4071"&gt;&lt;img alt="" src="http://www.mapleprimes.com/scripts/image.php?image=http://www.mapleprimes.com/files/8808_logplot.gif&amp;amp;width=300&amp;amp;height=300" /&gt;&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;We see that the points are almost on a straight line, except for the first three points (which I therefore discard). Then we find the fitted expression 0.00000276 * n^1.92 (with more digits, but let's forget about those) (and this number is from my new core i7 720QM laptop). This suggests approximately quadratic behaviour.&lt;/p&gt;
&lt;p&gt;Hope this helps,&lt;/p&gt;
&lt;p&gt;Erik Postma&lt;br /&gt;
Maplesoft.&lt;/p&gt;
&lt;p&gt;(*) Here's an example of how difficult it is to do this in a general-purpose computer algebra system. In an earlier version of this comment, the procedure took a Vector(realcons) as an argument instead of a Vector(numeric). When I wrote the sentence with the footnote mark, I realized that the procedure wouldn't work in general. Writing &lt;i&gt;if v[j] &amp;gt; v[j+1]&lt;/i&gt; is a somewhat dangerous thing to do, specifically because Maple has been written so that you can write that and know that it will execute in bounded time. The price to pay, however, is that the comparison is not very powerful. If v[j] and v[j+1] are both of type numeric, then you're safe. However, symbolic constants like Pi cannot be compared like this, because it might require a long computation to ascertain which is the bigger one (or indeed, if the two constants are different). For example, if you would write &lt;i&gt;if x &amp;gt; y then ...&lt;/i&gt; while having &lt;i&gt;x&lt;/i&gt; = Pi and &lt;i&gt;y&lt;/i&gt; = 2, then Maple will complain. If you want to risk the somewhat more lengthy computation, you can write &lt;i&gt;if is(x &amp;gt; y)&lt;/i&gt; or if you like living dangerously, &lt;i&gt;if evalf(x - y &amp;gt; 0)&lt;/i&gt;. However, then the count for the big-O computation cannot be bounded easily without further knowledge of the structure of &lt;i&gt;x&lt;/i&gt; and &lt;i&gt;y&lt;/i&gt;.&lt;/p&gt;</description>
      <guid>45282</guid>
      <pubDate>Tue, 02 Feb 2010 08:35:42 Z</pubDate>
      <itunes:author>epostma</itunes:author>
      <author>epostma</author>
    </item>
    <item>
      <title>Thank you?</title>
      <link>http://www.mapleprimes.com/questions/35833-Big-O-Of-A-Procedure?ref=Feed:MaplePrimes:Big O of a procedure:Comments#answer45283</link>
      <itunes:summary>&lt;p&gt;Thank you very much for your kind help. Unfortunately i am still an amature on maple but i did read everything you wrote and tried my best to understand it but because of the lack of knowledge in programming i did not grasp the whole concept but i am still grateful that you replied beause the information maybe useful to someone else. thank you&lt;/p&gt;</itunes:summary>
      <description>&lt;p&gt;Thank you very much for your kind help. Unfortunately i am still an amature on maple but i did read everything you wrote and tried my best to understand it but because of the lack of knowledge in programming i did not grasp the whole concept but i am still grateful that you replied beause the information maybe useful to someone else. thank you&lt;/p&gt;</description>
      <guid>45283</guid>
      <pubDate>Wed, 03 Feb 2010 01:00:15 Z</pubDate>
      <itunes:author>Jabz</itunes:author>
      <author>Jabz</author>
    </item>
    <item>
      <title>simplistic</title>
      <link>http://www.mapleprimes.com/questions/35833-Big-O-Of-A-Procedure?ref=Feed:MaplePrimes:Big O of a procedure:Comments#comment45284</link>
      <itunes:summary>&lt;p&gt;Pick a large value of the parameter of interest. Let's say it is value n some numeric value. Evaluate your algorithm at that value, and time it. &lt;b&gt;T1:=time(f(n))&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;Now do the same at 2*n. &lt;b&gt;T2:=time(f(2*n))&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;Now look at the log (base 2) of the ratio of T2 to T1.&amp;nbsp;&lt;b&gt; log[2](T2/T1)&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;If you are fortunate enough for the asymptotic behaviour of f to have manifested itself by size n, then that last value will tell you something.&lt;/p&gt;
&lt;p&gt;Let's put it another way. Suppose your algorithm has O(n^3) behavior. If you double the input parameter then you expect to see an 8 times increase in the computation time (if you are lucky enough, if n is already large enough). And &lt;b&gt;log[2](8)=3&lt;/b&gt;.&lt;/p&gt;</itunes:summary>
      <description>&lt;p&gt;Pick a large value of the parameter of interest. Let's say it is value n some numeric value. Evaluate your algorithm at that value, and time it. &lt;b&gt;T1:=time(f(n))&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;Now do the same at 2*n. &lt;b&gt;T2:=time(f(2*n))&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;Now look at the log (base 2) of the ratio of T2 to T1.&amp;nbsp;&lt;b&gt; log[2](T2/T1)&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;If you are fortunate enough for the asymptotic behaviour of f to have manifested itself by size n, then that last value will tell you something.&lt;/p&gt;
&lt;p&gt;Let's put it another way. Suppose your algorithm has O(n^3) behavior. If you double the input parameter then you expect to see an 8 times increase in the computation time (if you are lucky enough, if n is already large enough). And &lt;b&gt;log[2](8)=3&lt;/b&gt;.&lt;/p&gt;</description>
      <guid>45284</guid>
      <pubDate>Wed, 03 Feb 2010 01:22:34 Z</pubDate>
      <itunes:author>pagan</itunes:author>
      <author>pagan</author>
    </item>
  </channel>
</rss>