How to calculate the algorithm excution time?
Answer:
Knowing the Worst-Case Execution Time (WCET) of a program is necessary when designing and verifying real-time systems. A correct WCET analysis method must take into account the possible program flow, such as loop iterations and function calls, as well as the timing effects of different hardware features, such as caches and pipelines. A critical part of WCET analysis is the calculation, which combines flow information and hardware timing information in order to calculate a program WCET estimate. The type of flow information which a calculation method can take into account highly determines the WCET estimate precision obtainable. Traditionally, we have had a choice between precise methods that perform global calculations with a risk of high computational complexity and local methods that are fast but cannot take into account all types of flow information. This paper presents an innovative hybrid method to handle complex flows with low computational complexity, but still generate safe and tight WCET estimates. The method uses flow information to find the smallest parts of a program that have to be handled as a unit to ensure precision. These units are used to calculate a program WCET estimate in a demand-driven bottom-up manner. The calculation method to use for a unit is not fixed, but could depend on the included flow and program characteristics.
One way is to watch the wall clock. Another is to use the "time" command in Unix or print the time before it starts and when it completes. These are practical. A scientific way is to estimate how much longer it will run as the number of elements in an array or calls to the algorithm increase to a large number.
The main trick is to know if it is recursive. You also need to know if it has loops. Consider recursion: it is like an exponentially growing tree. So could grow exponentially. Say power(n,2) if each element of n elements exploded into n more recursions (n*n = power(n,2). E.g. quicksort has a worst cast of n^2. Java Collections.sort uses selection sort and mergesort.
Consider a loop, iterating over each element takes n operations. If the iterator (i) grows by more than one then the loop may complete in less than n loops and so will probably approach log(n) approximately.
It's all about asymptotic estimates and estimates of the best, average, and worst case running times. In practice, good best case algorithms (e.g. quicksort) can be used because they pathological cases which lead to their exhorbitant worst case performance can be coded around with check and tricks.
The answers post by the user, for information only, FunQA.com does not guarantee the right.
More Questions and Answers: