Optimizing PHP and ASP Code
A crucial part of developing successful web applications is optimizing
the files that comprise them. When using scripting languages such
as ASP and PHP, enhancement of dynamic pages is especially important.
Speed optimization affects how users perceive the efficiency and fluidity
of an application, while value optimization (enhancing the readability
and reusability) of the code reduces development time and provides
building blocks for new versions or other web projects. This article
describes several techniques that professional script developers use
to improve web application speed and code value.
Optimizing for Speed
The speed of a web application is perhaps the most obvious item
noticed by end-users. Froms a development perspective, the correct
functionality of an application may seem more important. However,
enhancements that increase the speed and efficiency of a web application
will have a significant positive effect on the user. A speed-optimized
web application not only delivers information more quickly and with
less server effort, but also lends fluidity and professionalism
to how a user perceives that application. The following techniques
can can make a substantial difference in the development of successful
web projects.
Static Versus Dynamic
The embedded nature of scripting languages offers several obvious
advantages that are often overlooked by the developer. No matter
the speed of the server, connection, and parser, static code outside
of script tags will always load faster than dynamic tags. As such,
it is important that any static HTML be printed outside of scripts.
For example, consider the following ASP code that prints a simple
pull-down menu:
<SELECT NAME="woodType">
<%
For Each woodType in woodArray
Response.Write "<OPTION VALUE=" & woodType &
">" & woodType & "</OPTION>"
Next
%>
</SELECT>
It is clear that much of what could be statically printed is instead
put into the script, through the parser, and then outputted. Consider
this optimized alternative:
<SELECT NAME="woodType">
<%
For Each woodType in woodArray
%>
<OPTION VALUE=<%=woodType%>><%=woodType%></OPTION>
<%
Next
%>
</SELECT>
The second piece of code will evaluate more quickly. While this
may not be noticeable for one small segment of code, using it as
a model for script development will yield results, however minimal.
Another advantage to keeping HTML outside of script tags is that
WYSIWYG editors such as MS Frontpage will allow for easier formatting
of that HTML in design view.
Database Connections and Queries
One of the easiest ways to optimize for speed is by taking control
of database connections and queries. Keep two key pieces of information
in mind: 1) Connecting to and disconnecting from database is probably
the most time-consuming part of your scripting code. 2) Database
developers spend years enhancing their database systems with performance
in mind, but their achievements will only work for your application
if you make use of them. The general rule for speed optimization
with databases is that connections, disconnections, and statement
executions should be made as rarely as possible. For example, a
script that makes multiple queries without pause should make only
one connection per database. Connecting each time that a query must
be run is very inefficient.
SQL queries themselves should always be optimized for efficiency.
Only retrieve the fields that are required by a script, never more.
Imagine a page that pulls all of the information in a user's profile
from a single table record and prints it. One query that obtains
all of the information takes much less execution time than multiple
queries that each retrieve a specific piece of information. Avoid
nested loops and other time-intensive processes in queries as much
as possible. Finally, make as few queries as the application requires.
One query that performs several inner/outer joins is more efficient
than using a query for each table, then comparing the results.
Files
File I/O is another costly area of code execution in terms of CPU
and execution time. The most obvious way to increase file efficiency
is reducing the number of file opens and closes to an absolute minimum.
Log files, for example, should be opened and closed one time per
script unless extenuating circumstances call for otherwise. The
only time that multiple files may have an efficiency advantage over
one file is when file size comes into play. Remember that when a
script opens a file, it must access the file's entire contents.
File sizes that range into megabytes and certainly gigabytes will
cause a noticeable performance decline. Keep file sizes down by
using multiple smaller files, or by deleting files as their information
loses its use. In the web application world, log files are the best
example of items whose file size must be strictly monitored. Minimizing
the number of files opened, and keeping the sizes of those files
as small as can be, will help reduce the performance loss that file
I/O inherently causes. Another important idea to consider is this:
Information stored in a database can almost always be handled more
quickly than information stored in a file. Granted, this depends
on several factors such as connection and disk speeds, but a database
query will be a file read almost every time.
Optimizing for Value
Appearance and Structure
The appearance and structure of script code are influential almost
exclusively to the developers. In that vein, be sure to use industry-standard
methods for spacing and indentation. Because lines without any code
are ignored by script parsers, make excellent use of "white
space" to isolate logical groups of statements. Avoid using
more than one statement per line and cramming code together. Use
functions and include files to reduce the size of code blocks to
readable levels. When reading or updating code, take advantage of
editors that automatically color-code statements and HTML. Finally,
be extra cautious when developing scripts in WYSIWYG editors such
as MS Frontpage, whose automatic formatting can maul programming
structure or even make changes to the code.
Documentation
Documentation has been, and will always be, the most critical contribution
to the readability of code. Be extensive and thorough in documenting
code. Two general rules apply here. First, try to document why you
are using certain code, rather than what you are doing with it.
For someone with knowledge of the programming language, the latter
is obvious. Second, be consistent in the way that you document your
code. If you typically use blocked comments rather than in-line
comments, be sure to do so throughout the development of a web application.
The more extensive, consistent, and helpful your documentation is,
the more valuable it will be to you and other developers.
Reuse
One of the most powerful features of third- and fourth-generation
programming languages is code reuse. Not only does it save development
time and reduce file size, reuse also lends neatness and professionalism
to programming code. A simple process will result in outstanding
code reuse regularity and efficiency. First, put any statements
within a script that are used multiple times into a function, and
call that function instead of repeating the code. Second, take all
code blocks that are used by multiple scripts (such as database
connection functions, error handling, etc.) and put them into functions
in library include files. Finally, analyze your include files to
group functions that usually go together (database functions, logging
functions, and other groupings). At the end of this process you
have a set of code libraries (include files) that can be used again
and again. In a script that requires their functions, include the
file, then call the function as often as needed.
|