How to estimate the time required for a program.

Once an algorithm is given for a problem and
decided to be correct, an important step is to determine how much in the way of resources,such as time or space, the algorithm will require.

The important things to know are:

1)It‘s very bad style to include constants or low-order terms inside a Big-Oh

2)This means that in any analysis that ignore lower-order terms and constants.

Example: Do not say T(N) = O(2N^2) or T(N) = O(N^2 + N);

The correct form is T(N) = O(N^2).

General Rules:


Rule1: for loop:

The running time of a for loop is at most the running time of the statements inside the for loop(including tests) times the number of iterations.

As an example, the following program fragment is O(N):

for(i=0; i<N; i++)

       k++;

Rule2:Nested for loop:

Analyze these inside out. The total running time of a statement inside a group of nested loops is the running time of the statement multiplied by the product of the sizes of all the for
loops.

As an example, the following program fragment is O(N^2):

for(i=0; i<N; i++)
    for(j=0; j<N; j++)
        k++;

Rule3:Consecutive Statements:

These just add(which means that the maximum is the one that counts).

As an example, the following program fragment, which has O(N) work followed by O(N^2) work, is also O(N^2):

for(i=0; i<N; i++)
    A[i] = 0;
for(i=0; i<N; i++)
    for(j=0; j<N; j++)
        A[i] += A[j]+i+j;

Rule4:if/else:

  
For the fragment

if(Condition)

S1

else

S2

The running time of an if/else statement is never more than the running time of the test plus the larger of the running times of S1 and S2.


Notices:

   A basic strategy of analyzing from the inside(or deepest part) our works.

   If there are function calls, these must be analyzed first.

Reference:

Data Structures and Algorithm Analysis in C .Second Edtion

Than you for reading!

                 --By lzq at NanYang May.26.2014

How to estimate the time required for a program.

时间: 2024-11-08 10:49:12

How to estimate the time required for a program.的相关文章

Java,AWTUtilities,eclipse报编译错误:Access restriction: The type &#39;AWTUtilities&#39; is not API (restriction on required library &#39;C:\Program Files\Java\jre7\lib\rt.jar&#39;)

[场景]调用com.sun.awt.AWTUtilities时,eclipse提示编译错误: Access restriction: The type 'AWTUtilities' is not API (restriction on required library 'C:\Program Files\Java\jre7\lib\rt.jar') [分析]eclipse项目默认的jre版本较早. com.sun.awt.AWTUtilities是在JDK 6 update10中增加的. [解决

Access restriction:The type JPEGCodec is not accessible due to restriction on required library C:\Program Files\Java\jre6\lib\rt.jar 报错

报错: Access restriction:The type JPEGCodec is not accessible due to restriction on required library C:\Program Files\Java\jre6\lib\rt.jar   解决方法: Project -> Properties -> libraries, 先remove掉JRE System Library,然后再Add Library重新加入. 将jre项目用自己下载的jre

not accessible due to restriction on required library

The type AWTUtilities is not accessible due to restriction on required library D:\Program Files\jdk1.6.0_24\jre\lib\rt.jar”, Access restriction: The type WindowsLookAndFeel is not accessible due to restriction on required library 解决的方法:在project build

关于Access restriction: The type &#39;Application&#39; is not API (restriction on required library)

原文链接:http://rxxluowei.iteye.com/blog/671893 今天写第一次写JavaFX的入门程序就GG 遇到了导入API的问题,无奈疯狂地通过网络找解决方案.. 我的问题是: 导入import javafx.application.Application;时 出现这个错误提示 Access restriction: The type 'Application' is not API (restriction on required library 'C:\Progra

Access restriction: The constructor SunJCE() is not accessible due to restriction on required library(转)

错误信息: Access restriction: The constructor SunJCE() is not accessible due to restriction on required library C:\Program Files \Java\jre7\lib\ext\sunjce_provider.jar Access restriction: The type TaskTopicResolver is not accessible due to restrict 做NC的时

The constructor BASE64Encoder() is not accessible due to restriction on required

在Eclipse中编写Java代码时,用到了BASE64Decoder,import sun.misc.BASE64Decoder;可是Eclipse提示: Access restriction : The type BASE64Decoder is not accessible due to restriction on required library C:\Program files\java\jre6\lib\rt.jar Access restriction : The constru

报错: The type ByteInputStream is not accessible due to restriction on required library

报错: Access restriction:The type JPEGCodec is not accessible due to restriction on required library C:\Program Files\Java\jre6\lib\rt.jar   解决方法: Project -> Properties -> libraries, 先remove掉JRE System Library,然后再Add Library重新加入.

If You Design It, You Should Be Able to Code It

? If You Design It, You Should Be Able to Code It Mike Brown in ARCHiTECTuRE, it's tempting to create elaborate designs and abstrac- tions that elegantly address the problem at hand. It is even more tempting to sprinkle new technologies into the proj

Project Management Process

Project Management ProcessDescription .......................................................................................................................................................................................1STAGE/STEP/TASK SUMMARY LIST