- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在寻找生成将在文本文件输出中使用的一长串日期/时间的最快方法。我正在努力实现的一个例子:
1, 2013-10-17 00:00:00,数据,数据
2, 2013-10-17 00:00:01,数据,数据
3, 2013-10-17 00:00:02,数据,数据
....
这是我必须遵守的格式,不能更改。这个数据可以轻松达到100,000+行。我目前正在做的是:
//text_output is a printstream to SD card
logged_time = Calendar.getInstance();
dateFormat = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");
// these values are gathered from a device via bluetooth
logged_time.set(year, month, day, hour, minute, second);
while(I_still_have_lines_to_process){
// print the line
text_output.print(line_count + "," + dateFormat.format(logged_time.getTime()) + data etc.)
// add the sample rate to calculate next date/time
logged_time.add(Calendar.SECOND, sample_rate);
}
我发现对于 250k 行,这个过程仅用于格式化就需要大约 50 秒。我还必须解析数据本身并通过蓝牙传输。如果我能加快速度,用户体验将会大大改善。有没有更快的方法来做到这一点?
感谢阅读!
最佳答案
我将您的代码重写为我可以实际运行和测试的代码,最后得到以下代码。它很粗糙,但对我有用,我会以此为基础进行工作。如果有不符合您实际行为的地方,请仔细阅读。
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 毫秒。即使您的代码不完全相同,您也可以使用此解决方案中的想法来帮助自己。
关于android - 在 Android 中格式化大量日期/时间对象的最快方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19432759/
我想了解 Ruby 方法 methods() 是如何工作的。 我尝试使用“ruby 方法”在 Google 上搜索,但这不是我需要的。 我也看过 ruby-doc.org,但我没有找到这种方法。
Test 方法 对指定的字符串执行一个正则表达式搜索,并返回一个 Boolean 值指示是否找到匹配的模式。 object.Test(string) 参数 object 必选项。总是一个
Replace 方法 替换在正则表达式查找中找到的文本。 object.Replace(string1, string2) 参数 object 必选项。总是一个 RegExp 对象的名称。
Raise 方法 生成运行时错误 object.Raise(number, source, description, helpfile, helpcontext) 参数 object 应为
Execute 方法 对指定的字符串执行正则表达式搜索。 object.Execute(string) 参数 object 必选项。总是一个 RegExp 对象的名称。 string
Clear 方法 清除 Err 对象的所有属性设置。 object.Clear object 应为 Err 对象的名称。 说明 在错误处理后,使用 Clear 显式地清除 Err 对象。此
CopyFile 方法 将一个或多个文件从某位置复制到另一位置。 object.CopyFile source, destination[, overwrite] 参数 object 必选
Copy 方法 将指定的文件或文件夹从某位置复制到另一位置。 object.Copy destination[, overwrite] 参数 object 必选项。应为 File 或 F
Close 方法 关闭打开的 TextStream 文件。 object.Close object 应为 TextStream 对象的名称。 说明 下面例子举例说明如何使用 Close 方
BuildPath 方法 向现有路径后添加名称。 object.BuildPath(path, name) 参数 object 必选项。应为 FileSystemObject 对象的名称
GetFolder 方法 返回与指定的路径中某文件夹相应的 Folder 对象。 object.GetFolder(folderspec) 参数 object 必选项。应为 FileSy
GetFileName 方法 返回指定路径(不是指定驱动器路径部分)的最后一个文件或文件夹。 object.GetFileName(pathspec) 参数 object 必选项。应为
GetFile 方法 返回与指定路径中某文件相应的 File 对象。 object.GetFile(filespec) 参数 object 必选项。应为 FileSystemObject
GetExtensionName 方法 返回字符串,该字符串包含路径最后一个组成部分的扩展名。 object.GetExtensionName(path) 参数 object 必选项。应
GetDriveName 方法 返回包含指定路径中驱动器名的字符串。 object.GetDriveName(path) 参数 object 必选项。应为 FileSystemObjec
GetDrive 方法 返回与指定的路径中驱动器相对应的 Drive 对象。 object.GetDrive drivespec 参数 object 必选项。应为 FileSystemO
GetBaseName 方法 返回字符串,其中包含文件的基本名 (不带扩展名), 或者提供的路径说明中的文件夹。 object.GetBaseName(path) 参数 object 必
GetAbsolutePathName 方法 从提供的指定路径中返回完整且含义明确的路径。 object.GetAbsolutePathName(pathspec) 参数 object
FolderExists 方法 如果指定的文件夹存在,则返回 True;否则返回 False。 object.FolderExists(folderspec) 参数 object 必选项
FileExists 方法 如果指定的文件存在返回 True;否则返回 False。 object.FileExists(filespec) 参数 object 必选项。应为 FileS
我是一名优秀的程序员,十分优秀!