Java程序性能和速度优化实例
David Lee
例一:应用具有I/O Buffer功能Class |
import java.io.*; |
public class IoTest { |
public static void main(String args[]) { |
try { |
FileReader fr = new FileReader(args[0]); |
BufferedReader br = new BufferedReader(fr); |
while ( br.readLine() != null ) { |
System.out.println(" The file content are :" + br.readLine()); |
} |
fis.close(); |
} catch ( IOException ioe ) { |
System.out.println("The I/O exception is " + ioe); |
} |
} |
} |
在上例中,程序使用了具有Buffer功能的Class,使得Disk I/O的读取速度大大提高。BufferedReader 是取代DataInputStream 而提高读写速度的Java Class。在新的Java版本中,已不建议使用 DataInputStream,因为其读写是基于字符为单位的。 |
例二:字符串运算处理 |
public class StringOperation { |
public static void main(String args[]) { |
String sqlQuery = null; |
String sqlCondition = " conditionC = conditionD "); |
StringBuffer sb = new StringBuffer(); |
sb.append("select * from database table where "); |
sb.append(" conditionA = conditionB and "); |
if ( ! sqlCondition.equals(null) { |
sb.append(sqlCondition); |
} else { |
sb.append(" conditionE = conditionF "); |
} |
sqlQuery = sb.toString(); |
// Then connect to the database then excute the database query |
// ....... |
} |
} |
在上例中,使用StingBuffer class来完成数据库查询建立,避免使用String class的"+="操作,以减少JVM在内存中创建新的对象,占用资源,增加JVM回收资源负担。读者可以使用Java Proflier功能来具体比较使用不同的String操作,JVM需要完成多少资源回收和运行时间。因此在JVM中对String直接进行"+="是非常昂贵的运算。 |
例三:处理昂贵的数据库初始化 |
目前许多网站可以透过Web服务器查询数据库,如何提高数据库查询速度成为许多程序员关注的问题。在Java Servlets或JSP中可以通过init() 或Jspinit()来实现,以下是一具体Java Servlet与数据库对话实例。 |
import java.io.*; |
import java.sql.*; |
import javax.servlet.*; |
import javax.servlet.http.*; |
public class DatabaseServlet extends HttpServlet { |
public void init( ServletConfig conf) throws ServletException { |
super.init(conf); |
Connection conn = null; |
try { |
Class.forName("sun.jdbc.odbc.JdcOdbcDriver"); |
Conn = DriverManager.getConnection("jdbc:odbc:yourDSN,"",""); |
} catch ( SQLException sqle ) { |
System.err.println("your error exception is " + sqle); |
} catch ( ClassNotFoundException cnfe ) { |
System.err.println("your error exception is " + cnfe); |
} |
} |
public void doGet( HttpServletRequest req, HttpServletResponse res) throws |
ServletException, IOException { |
res.setContentType("text/html"); |
ServletOutputStream out = null; |
// Your HTML formatter |
out.println(" Your HTML"); |
try { |
Statement stmt = conn.creatStatement(); |
ResultSet rs = stmt.excuteQuery("select * from yourDatabasetable "); |
while ( rs.next() ) { |
// Processing your data |
} |
} catch ( SQLException sqle ) { |
out.println("The SQL error is " + sqle); |
} |
// output your processing result to HTML page |
out.println(" your HTML"); |
rs.close(); |
stmt.close(); |
} |
public void destroy() { |
try { |
conn.close(); |
} catch ( SQLException sqle ) { |
System.err.println("your SQL error is " + sqle); |
} |
} |
} |
在上例中,由于Java Servlet运行机制的特点,将昂贵的数据库初始化运算在整个Servlet运行中仅只调用一次的init()中完成,以减少不必要的重复性数据库运算。读者可以根据应用的具体情况,甚至将数据库的Statement和ResultSet部分移至init()中完成,或者调用PreparedStatement与CallableStatement来优化数据库的运算。同时,对数据库的连接的关闭由destroy()一次性完成。 |