我将您的代码重写为我可以实际运行和测试的代码,并最终得到以下代码。它很粗糙,但对我有用,我会以此为基础。如果有与您的实际行为不符的地方,请仔细阅读。
private static final int SAMPLE_RATE = 1;
public static void main(String[] args) {
long time = System.currentTimeMillis();
Calendar logged_time = Calendar.getInstance();
Format dateFormat = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");
try (BufferedWriter writer = Files.newBufferedWriter(Paths.get("out.txt"), StandardCharsets.UTF_8)) {
for (int i = 0; i < 100000; i++) {
// print the line
writer.write(i + ", " + dateFormat.format(logged_time.getTime()));
writer.newLine();
// add the sample rate to calculate next date/time
logged_time.add(Calendar.SECOND, SAMPLE_RATE);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
System.out.println(System.currentTimeMillis() - time + " ms");
}
当我在我的机器上运行它时,我平均得到 1000 毫秒。
简单地改变
dateFormat.format(logged_time.getTime())
至
dateFormat.format(logged_time)
平均为 730 毫秒。您不需要Date
每次都创建新对象。
但我们可以做得更好!有很多重复的工作——我们不必一次又一次地格式化整个日期。让我们缓存大部分格式化的日期,只检查是否需要在秒溢出时重新格式化它:
private static final int SAMPLE_RATE = 1;
public static void main(String[] args) {
long time = System.currentTimeMillis();
Calendar logged_time = Calendar.getInstance();
Format dateFormatNoSecs = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:");
Format dateFormatSecs = FastDateFormat.getInstance("ss");
String lastFormattedDateNoSecs = dateFormatNoSecs.format(logged_time);
int lastSecs = logged_time.get(Calendar.SECOND);
try (BufferedWriter writer = Files.newBufferedWriter(Paths.get("out.txt"), StandardCharsets.UTF_8)) {
for (int i = 0; i < 100000; i++) {
int secs = logged_time.get(Calendar.SECOND);
if (secs < lastSecs) {
// at least minutes changed, we need to reformat it
lastFormattedDateNoSecs = dateFormatNoSecs.format(logged_time);
}
// print the line
writer.write(i + ", " + lastFormattedDateNoSecs + dateFormatSecs.format(logged_time));
writer.newLine();
// add the sample rate to calculate next date/time
logged_time.add(Calendar.SECOND, SAMPLE_RATE);
lastSecs = secs;
}
} catch (IOException e) {
throw new RuntimeException(e);
}
System.out.println(System.currentTimeMillis() - time + " ms");
}
610 毫秒。
最后的微优化:几秒钟的自定义手动格式化程序。奇怪的DecimalFormat
是,它比FastDateFormat
秒格式化慢。但是一个简单的开关是最快的:
public class TwoDigitIntegerFormatter {
public static String format(int number) {
assert (number >= 0) && (number <= 99);
switch (number) {
case 0: return "00";
case 1: return "01";
case 2: return "02";
case 3: return "03";
case 4: return "04";
case 5: return "05";
case 6: return "06";
case 7: return "07";
case 8: return "08";
case 9: return "09";
default: return String.valueOf(number);
}
}
}
我们也可以手动维护第二个计数:
private static final int SAMPLE_RATE = 1;
public static void main(String[] args) {
long time = System.currentTimeMillis();
Calendar logged_time = Calendar.getInstance();
Format dateFormatNoSecs = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:");
String lastFormattedDateNoSecs = dateFormatNoSecs.format(logged_time);
int secs = logged_time.get(Calendar.SECOND);
try (BufferedWriter writer = Files.newBufferedWriter(Paths.get("out.txt"), StandardCharsets.UTF_8)) {
for (int i = 0; i < 100000; i++) {
if (secs > 59) {
// at least minutes changed, we need to reformat it
lastFormattedDateNoSecs = dateFormatNoSecs.format(logged_time);
secs %= 60;
}
writer.write(i + ", " + lastFormattedDateNoSecs + TwoDigitIntegerFormatter.format(secs));
writer.newLine();
// add the sample rate to calculate next date/time
logged_time.add(Calendar.SECOND, SAMPLE_RATE);
secs += SAMPLE_RATE;
}
} catch (IOException e) {
throw new RuntimeException(e);
}
System.out.println(System.currentTimeMillis() - time);
}
510 毫秒。即使您的代码不完全相同,您也可以使用此解决方案中的想法来帮助自己。