Overview of Program Efficiency
This chapter presents ideas to consider when trying to create the most efficient programs possible, and discusses how to analyze the performance of your applications.
Knowledge of IDL's implementation and the pitfalls of virtual memory can be used to greatly improve the efficiency of IDL programs. In IDL, complicated computations can be specified at a high level. Therefore, inefficient IDL programs can suffer severe speed penalties — perhaps much more so than with most other languages.
Techniques for writing efficient programs in IDL are identical to those in other computer languages with the addition of the following simple guidelines:
- Use array operations rather than loops wherever possible. Try to avoid loops with high repetition counts. See Use Vector and Array Operations.
- Use IDL system functions and procedures wherever possible. See Use System Functions and Procedures.
- Access array data in machine address order. See Access Large Arrays by Memory Order.
Attention also must be given to algorithm complexity and efficiency, as this is usually the greatest determinant of resources used.
IDL Implementation
IDL programs are compiled into a low-level abstract machine code which is interpretively executed. The dynamic nature of variables in IDL and the relative complexity of the operators precludes the use of directly executable code. Statements are only compiled once, regardless of the frequency of their execution.
The IDL interpreter emulates a simple stack machine with approximately 50 operation codes. When performing an operation, the interpreter must determine the type and structure of each operand and branch to the appropriate routine. The time required to properly dispatch each operation may be longer than the time required for the operation itself.
The characteristics of the time required for array operations is similar to that of vector computers and array processors. There is an initial set-up time, followed by rapid evaluation of the operation for each element. The time required per element is shorter in longer arrays because the cost of this initial set-up period is spread over more elements. The speed of IDL is comparable to that of optimized FORTRAN for array operations. When data are treated as scalars, IDL efficiency degrades by a factor of 30 or more.
Additional Programming Efficiency Resources
Also refer to the following topics, located in other sections of this manual, for additional ways to improve the efficiency of your IDL program:
- Efficiency and Expression Evaluation Order — describes how to organize operations to increase execution speed
- Defining and Using Constants — describes the importance of using constants of the correct type
- Avoid Invariant Expressions — describes the inefficiency of invariant expression within loop statements