LINUX TIPS AND TRICKS --- June 02, 2000

Published by ITworld.com, the IT problem-solving network
http://www.itworld.com/newsletters

*********************************************************************

The First and Second Rules of Optimization
By Danny Kalev

In many books on code optimization, the first rule is: "Don't!" In 
real-world programming, correctness, development time and cost, and 
software stability have a higher priority than performance. Only when 
these goals have been achieved should you consider optimization -- but 
only if it's truly necessary. 

Why is premature optimization considered harmful? Programmers' 
intuitions can be misleading. A feature that they consider expensive can 
be cheap in terms of CPU time. By contrast, features that seem innocuous 
might turn out to be CPU-intense.

Thus, inexperienced programmers who believe they're optimizing their 
code might actually achieve the opposite result. Furthermore, the 
changes made to the original program during optimization introduce new 
bugs or obfuscate the code, thereby making it harder to read and 
maintain.

That said, optimization is sometimes inevitable. Although most 
optimization tricks are language and compiler dependent, there is one 
that's ubiquitously effective: reducing the executable's size. A slimmer 
program usually executes faster. Here's why. 

A CPU cycle is roughly equivalent to the time needed to perform one 
machine instruction. On modern CPUs, a cycle takes about one to two 
nanoseconds. By contrast, RAM access is 10 times slower. Therefore, if 
the CPU needs to fetch program instructions from the main memory, it 
wastes 10 CPU cycles in idle mode. 

To minimize this bottleneck, CPUs have a special type of memory called  
cache memory. Cache-memory access takes about one CPU cycle. The 
problem, however, is that the cache size is limited to 256Kb or 512Kb at 
most. An average application easily exceeds this limit. As a result, 
only a small portion of the executable image can be loaded into the 
cache. 

If a certain instruction isn't found in the cache (this is called a 
cache miss), the system must fetch it from main memory, which slows down 
performance. To reduce the number of cache misses, keep your 
application's size as small as possible. Sometimes an executable that is 
10 to 15 percent slimmer reduces the number of cache misses 
dramatically. 

To reduce an application's size, avoid injudicious use of inline 
functions and templates. Remove any unused functions and data from the 
program (use the GCC -Wunused flag for this purpose). Finally, use the 
"strip" command to remove symbols from object files.

About the author
----------------
Danny Kalev is a system analyst and software engineer with more
than 10 years of experience, specializing in C++ and
object-oriented analysis and design on various platforms including
VMS, DOS, Windows, Unix, and Linux. His technical interests involve
code optimization, networking, and distributed computing. He is
also a member of the ANSI C++ standardization committee and the
author of ANSI/ISO C++ Professional Programmer's Handbook (Que,
1999). Contact him at linuxnl@excite.com.

*********************************************************************

CUSTOMER SERVICE

You can subscribe or unsubscribe to any of your e-mail newsletters by 
updating your form at: 
http://www.itworld.com/cgi-bin/w3-msql/newsletters/subcontent12.html?

For subscription changes that cannot be handled via the web, please send 
an email to our customer service dept: support@itworld.com

*********************************************************************

CONTACTS

* For editorial comments, write Heidi Carson, Managing editor,
Newsletters at: heidi_carson@itworld.com
* For advertising information, write Dan Chupka, Account Executive at:
dan_chupka@itworld.com
* For all other inquiries, write Jodie Naze, Product Manager,
Newsletters at: jodie_naze@itworld.com

*********************************************************************

Copyright 2000 ITworld.com, Inc., All Rights Reserved. 

http://www.itworld.com
