Hallo,
wollte einmal fragen, ob ich die Zeit, die eine Methode braucht, so messen kann:
beispielhafte Klasse, die gemessen werden soll:
Nun mein Logger:
Was haltet ihr davon?
wollte einmal fragen, ob ich die Zeit, die eine Methode braucht, so messen kann:
beispielhafte Klasse, die gemessen werden soll:
Java:
public class PunkteMalen {
public static void main(String[] args) throws IOException {
// Eine Beispiel Methode, in der die Zeit gemessen werden soll
double timeBefore = System.nanoTime();
for(int i = 0; i <= 100000; i++){
System.out.println("...");
}
Logging.logging(timeBefore, System.nanoTime(), 1, "PunkteMalen");
}
}
Nun mein Logger:
Java:
public class Logging {
static FileWriter writer;
File file;
static String filename = "Performance_Log.txt";
/**
* Konstruktor
*/
Logging() {
}
/**
* Write the calculated values in the log.
*
* @param timeBefore
* @param timeAfter
* @param level
* @param name
* @throws IOException
*/
public static void logging(double timeBefore, double timeAfter, int level,
String name) throws IOException {
checkIfEmpty();
try {
writer = new FileWriter(filename, true);
writer.write(getTimestamp() + "," + name + "," + level + ","
+ formateRunningtime(timeBefore, timeAfter) + "," + getUser());
writer.write(System.getProperty("line.separator"));
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Get the timestamp of today.
*
* @return timestamp
*/
static String getTimestamp() {
Calendar cal = Calendar.getInstance();
// Datum
String date = cal.get(Calendar.DAY_OF_MONTH) + "."
+ (cal.get(Calendar.MONTH) + 1) + "." + cal.get(Calendar.YEAR);
// Uhrzeit
String time = cal.get(Calendar.HOUR_OF_DAY) + ":"
+ cal.get(Calendar.MINUTE) + ":" + cal.get(Calendar.SECOND)
+ ":" + cal.get(Calendar.MILLISECOND);
return date + "," + time;
}
/**
* Converting from nanoseconds in seconds.
*
* @param timeBefore
* @param timeAfter
* @return runtimeWithFour
*/
static String formateRunningtime(double timeBefore, double timeAfter) {
// Nanosekunden in Sekunden konvertieren
double runtime = ((timeAfter - timeBefore) / 1000000000);
// nur 4 Nachkommastellen
DecimalFormat f = new DecimalFormat("#0.0000");
String runtimeWithFour = f.format(runtime);
// Komma durch einen Punkt ersetzen, da sonst nicht nach Excel
// importiert werden kann
runtimeWithFour = runtimeWithFour.replace(",", ".");
return runtimeWithFour;
}
/**
* If textfile is empty, then this this methode creates a title column.
*
* @throws IOException
*/
static void checkIfEmpty() throws IOException {
FileInputStream fis = new FileInputStream(new File(filename));
int b = fis.read();
if (b == -1) {
try {
writer = new FileWriter(filename, true);
writer.write("Datum,Uhrzeit,Name,Level,Zeit,Von");
writer.write(System.getProperty("line.separator"));
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* Getting the username
*
* @return user
*/
static String getUser() {
return System.getProperty("user.name");
}
}
Was haltet ihr davon?
Zuletzt bearbeitet: