2010-02-04 40 views

回答

2

您可以使用System.currentTimeMillis()獲取代碼中各個時間點的時間戳,然後使用這些值計算時間。例如:

long start = System.currentTimeMillis(); 
HttpConnection connection = (HttpConnection) Connector.open(url); 
long opened = System.currentTimeMillis(); 
String body = new String(IOUtilities.streamToBytes(connection.openInputStream())); 
long done = System.currentTimeMillis(); 

long bytes = body.length(); 
float durationSeconds = (float)(done - opened)/1000.0f; 
float bytesPerSecond = bytes/durationSeconds; 

System.out.println("Latency: " + (opened - start) + " ms"); 
System.out.println("Bandwidth: " + bytesPerSecond + " bytes per second");