eu preciso de um metodo que calcule o tempo de processamento de um metodo, em segundos, pois o processamento sera rapido.
alguem sabe como eu faço isso usando a classe Date? ou se nao, existe outra forma de fazer?
obrigado
Calculo de tempo
E
4 Respostas
Já precisei fazer isso, olha o código aí
long startTime =System.currentTimeMillis();
parser.parse(input);
long endTime =System.currentTimeMillis();
usei isso para mediro tempo de parsing de um xml. Se vc precisar de mais precisao, vc pode pedir o tempo corrente em nanosegundos em vez de milisegundos…
aí basta vc subrir o startTime do endTime pra encontrar o tempo de duração.
Acho que é dessa classe que você precisa:
** Time the execution of any block of code. */
public final class Stopwatch {
/**
* An example of the use of this class to
* time the execution of String manipulation code.
*/
public static void main (String... arguments) {
Stopwatch stopwatch = new Stopwatch();
stopwatch.start();
//do stuff
StringBuilder messageOne = new StringBuilder();
int numIterations = 5000;
for(int idx=0; idx < numIterations; ++idx){
messageOne.append("blah");
}
stopwatch.stop();
//Note that there is no need to call a method to get the duration,
//since toString is automatic here
System.out.println("The reading for StringBuilder is: " + stopwatch);
//reuse the same stopwatch to measure an alternative implementation
//Note that there is no need to call a reset method.
stopwatch.start();
//do stuff again
String messageTwo = null;
for(int idx=0; idx < numIterations; ++idx){
messageTwo = messageTwo + "blah";
}
stopwatch.stop();
//perform a numeric comparsion
if ( stopwatch.toValue() > 5 ) {
System.out.println("The reading is high: " + stopwatch);
}
else {
System.out.println("The reading is low: " + stopwatch);
}
}
/**
* Start the stopwatch.
*
* @throws IllegalStateException if the stopwatch is already running.
*/
public void start(){
if ( fIsRunning ) {
throw new IllegalStateException("Must stop before calling start again.");
}
//reset both start and stop
fStart = System.currentTimeMillis();
fStop = 0;
fIsRunning = true;
fHasBeenUsedOnce = true;
}
/**
* Stop the stopwatch.
*
* @throws IllegalStateException if the stopwatch is not already running.
*/
public void stop() {
if ( !fIsRunning ) {
throw new IllegalStateException("Cannot stop if not currently running.");
}
fStop = System.currentTimeMillis();
fIsRunning = false;
}
/**
* Express the "reading" on the stopwatch.
*
* @throws IllegalStateException if the Stopwatch has never been used,
* or if the stopwatch is still running.
*/
public String toString() {
validateIsReadable();
StringBuilder result = new StringBuilder();
result.append(fStop - fStart);
result.append(" ms");
return result.toString();
}
/**
* Express the "reading" on the stopwatch as a numeric type.
*
* @throws IllegalStateException if the Stopwatch has never been used,
* or if the stopwatch is still running.
*/
public long toValue() {
validateIsReadable();
return fStop - fStart;
}
// PRIVATE ////
private long fStart;
private long fStop;
private boolean fIsRunning;
private boolean fHasBeenUsedOnce;
/**
* Throws IllegalStateException if the watch has never been started,
* or if the watch is still running.
*/
private void validateIsReadable() {
if ( fIsRunning ) {
String message = "Cannot read a stopwatch which is still running.";
throw new IllegalStateException(message);
}
if ( !fHasBeenUsedOnce ) {
String message = "Cannot read a stopwatch which has never been started.";
throw new IllegalStateException(message);
}
}
}
fonte:
[url]http://www.javapractices.com/topic/TopicAction.do?Id=85[/url]
fw
E
mas renzonuccitelli
isso que vc colocou calcula em milesimos ne, teria como eu calcular em segundos??
obrigado
Hmm… já tentou dividir os milésimos por mil 
Criado 24 de novembro de 2008
Ultima resposta 24 de nov. de 2008
Respostas 4
Participantes 4
Alura O que é Python? — um guia completo para iniciar nessa linguagem de programação Acesse agora o guia sobre Python e inicie sua jornada nessa linguagem de programação: o que é e para que serve, sua sintaxe e como iniciar nela!
Casa do Codigo Engenharia de Prompt para Devs: Um guia para aprender a... Por Ricardo Pupo Larguesa — Casa do Codigo