Topic : Opzimizing Your Code
Author : McMillan
Page : << Previous 6  
Go to page :

operations that are coded in assembly are low-level library functions. On most implementations, for example, the standard library functions memset() and strcpy() are written in native assembly code. C and C++ enable the programmer to embed inline assembly code within an asm block. For example

  mov a, ecx

Interacting with the Operating System Directly
API functions and classes enable you to interact with the operating system. Sometimes, however, executing a system command directly can be much faster. For this purpose, you can use the standard function system() that takes a shell command as a const char *. For example, on a DOS/Windows system, you can display the files in the current directory as follows:

#include <cstdlib>
using namespace std;
int main()
  system("dir");  //execute the "dir" command

Here again, the tradeoff is between speed on the one hand and portability and future extensibility on the other hand.

In an ideal world, software designers and developers might focus their efforts on robust, extensible, and readable code. Fortunately, the current state of affairs in the software world is much closer to that ideal than it was 15, 30, or 50 years ago. Notwithstanding that, performance tuning and optimizations will probably remain a necessity for a long time. The faster hardware becomes, the more the software that runs on it is required to meet higher demands. Speech recognition, online translation of natural languages, neural networks, and complex mathematical computations are only a few examples of resource-hungry applications that will evolve in the future and require careful optimizations.

Textbooks often recommend that you put off optimization consideration to the final stages of testing. Indeed, the primary goal is to get the system to work correctly. Nonetheless, some of the techniques presented here -- such as declaring objects locally, preferring prefix to postfix operators, and using initialization instead of assignment -- need to become a natural habit. It is a well-known fact that programs usually spend 90% of their time executing only 10% of their code (the numbers might vary, but they range between 80% and 20% to 95% and 5%). The first step in optimization is, therefore, identifying that 10% of your programs and optimizing them. Many automated profiling and optimization tools can assist you in identifying these critical code parts. Some of these tools can also suggest solutions to enhance performance. Still, many of the optimization techniques are implementation-specific and always require human expertise. It is important to empirically verify your suspicions and to test the effect of suggested code modifications to ensure that they indeed improve the system's performance. Programmers' intuitions regarding the cost of certain operations are often misleading. For example, shorter code is not necessarily faster code. Similarly, writing convoluted code to avoid the cost of a simple if statement is not worth the trouble because it saves only one or two CPU cycles.

Page : << Previous 6