Software Efficiency Flashcards

(12 cards)

1
Q

What are the 6 software efficiency “rules of thumb”?

A
  1. Always use the right data structure
  2. Make the common case fast
  3. Don’t do work unnecessarily
  4. Don’t allocate memory unnecessarily
  5. Given that optimizing efficiency involves trade offs, be careful not to accidentally harm other aspects of software quality
  6. Do more than one thing at a time where possible
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is meant by “wall clock time”?

A

The elapsed “real” time between two events, as would be measured by a wall clock or stopwatch. Using this can lead to inaccuracies when measuring software execution time because of other factors, such as operations being performed by the Java Virtual Machine or the operating system and hardware of the computer on which the code is being run.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How should software execution time be measured?

A
  • Use Java System methods to measure execution time of small pieces of code
  • Use UNIX “time” command to measure execution time of entire program
  • Use profiling tools for more detailed information
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you avoid doing unnecessary work?

A
  • Don’t check conditions unnecessarily
  • Don’t call methods unnecessarily
  • Don’t redo work you’ve already done
  • Use the Java API when possible
  • Use lazy evaluation, lazy initialization, and memoization
  • Take advantage of short-circuit boolean operators
  • Use micro-optimizations sparingly, especially if they may harm understandability
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Lazy initialization

A

A programming technique in which we only create objects when we know we’re going to need them.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Lazy evaluation

A

A programming technique in which we evaluate expressions only when we know we’ll need the results.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Memoization

A

This term simply means remembering and resuing previously performed work. In regards to software efficiency, it is a programming technique in which we keep a Map of inputs (arguments) to outputs (results) and return the result from the Map if the same input is seen more than once. This will lead to more memory utilization but may greatly reduce execution time.

This technique is illustrated in the following chart of method executions:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What 4 micro-optimization techniques can improve efficiency but likely harm some other aspect of software quality necessitating evaluation of a trade off?

A
  1. Method inlining
  2. Loop unrolling
  3. Constant folding
  4. Strength reduction
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Method inlining

A

Replacing a method call with the code in the method body to eliminate the overhead of calling the method. Note that this using this technique duplicates code (the most common code smell) and thus compromises another aspect of software quality. Therefore you must be comfortable with this trade off when using it!

//TODO insert screen grab with example

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Loop unrolling

A

A programming technique to eliminate the overhead required by a for loop by replacing it with multiple instance of the body. This is illustrated below.

//TODO insert screen grab with example

Note that this can only be done when you know how many times the loop will be executed and as with method inlining, makes code harder to read and thus, involves a trade off with other aspects of software quality.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Constant folding

A

A programming technique of replacing a numerical expression with the result to eliminate the overhead associated with performing the calculation. Note that will this increases efficiency, it may compromise readability and often, the compiler will perform this optimization for you.

//TODO insert screen grab with example

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Strength reduction

A

A programming technique of replacing complex operations, such as multiplication, with simpler ones, such as left shift. Note that like other optimization techniques, this often makes code harder to read and so should be used cautiously or at least accompanied by comments.

//TODO insert screen grab with example

How well did you know this?
1
Not at all
2
3
4
5
Perfectly