15.2 Running Time Analysis of Algorithms: Counting Statements

One simple method to analyze the running time of a code sequence or a method is simply to count the number of times each statement is executed and to calculate a total count of statement executions.

Example 15.1 is a method that calculates the total value of all the elements of an array of size n and returns the sum.

Let’s count how many times each statement is executed.

Assuming the array has n elements, we can develop the following analysis:

Statement

# Times ...

Get Java Illuminated, 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.