package de.ikoffice.jdbc; import java.io.Reader; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class StringVsStreamTest { public static void main(String args[]) throws Exception { long nanos; int count; char[] buffer = new char[1000]; long tempNanos1; long tempNanos2; Class.forName("org.postgresql.Driver"); Connection c = DriverManager.getConnection("jdbc:postgresql://192.168.1.3:5432/Test","ik","ik0000"); Statement s = c.createStatement(); ResultSet r = s.executeQuery("SELECT * FROM tbl_test"); r.next(); for( int j = 0; j < 10; j++ ) { // Ignore the times for the first run to be sure no JIT compilation slows us down. for( int i=0; i<1000; i++ ) { String string = r.getString("varchar_col"); } for( int i=0; i<1000; i++ ) { Reader reader = r.getCharacterStream("varchar_col"); count = reader.read(buffer, 0, 1000); String string = new String(buffer,0,count); } for( int i=0; i<1000; i++ ) { String string = r.getString("text_col"); } for( int i=0; i<1000; i++ ) { Reader reader = r.getCharacterStream("text_col"); count = reader.read(buffer, 0, 1000); String string = new String(buffer,0,count); } } // Now do the real measurement. nanos = System.nanoTime(); for( int i=0; i<1000; i++ ) { String string = r.getString("text_col"); } tempNanos1 = System.nanoTime()-nanos; System.out.println("getString on text_col took "+((tempNanos1)/100000d)+"ms"); nanos = System.nanoTime(); for( int i=0; i<1000; i++ ) { Reader reader = r.getCharacterStream("text_col"); count = reader.read(buffer, 0, 1000); String string = new String(buffer,0,count); } tempNanos2 = System.nanoTime()-nanos; System.out.println("getCharacterStream on text_col took "+((tempNanos2)/100000d)+"ms"); System.out.println("Factor: " + (tempNanos2*1d/tempNanos1)); nanos = System.nanoTime(); for( int i=0; i<1000; i++ ) { String string = r.getString("varchar_col"); } tempNanos1 = System.nanoTime()-nanos; System.out.println("getString on varchar_col took "+((tempNanos1)/100000d)+"ms"); nanos = System.nanoTime(); for( int i=0; i<1000; i++ ) { Reader reader = r.getCharacterStream("varchar_col"); count = reader.read(buffer, 0, 1000); String string = new String(buffer,0,count); } tempNanos2 = System.nanoTime()-nanos; System.out.println("getCharacterStream on varchar_col took "+((tempNanos2)/100000d)+"ms"); System.out.println("Factor: " + (tempNanos2*1d/tempNanos1)); } }