- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
DurationOfRun:5
ThreadSize:10
ExistingRange:1-1000
NewRange:5000-10000
Percentage:55 - AutoRefreshStoreCategories Data:Previous/30,New/70 UserLogged:true/50,false/50 SleepTime:5000 AttributeGet:1,16,10106,10111 AttributeSet:2060/30,10053/27
Percentage:25 - CrossPromoEditItemRule Data:Previous/60,New/40 UserLogged:true/50,false/50 SleepTime:4000 AttributeGet:1,10107 AttributeSet:10108/34,10109/25
Percentage:20 - CrossPromoManageRules Data:Previous/30,New/70 UserLogged:true/50,false/50 SleepTime:2000 AttributeGet:1,10107 AttributeSet:10108/26,10109/21
我正在尝试解析上面的.txt文件(前四行是固定的,最后三行可以增加意味着它可以超过3行),因此我编写了下面的代码及其工作原理,但它看起来很困惑。那么有没有更好的方法来解析上面的 .txt 文件,如果我们考虑性能,那么这将是解析上面的 txt 文件的最佳方法。
private static int noOfThreads;
private static List<Command> commands;
public static int startRange;
public static int endRange;
public static int newStartRange;
public static int newEndRange;
private static BufferedReader br = null;
private static String sCurrentLine = null;
private static List<String> values;
private static String commandName;
private static String percentage;
private static List<String> attributeIDGet;
private static List<String> attributeIDSet;
private static LinkedHashMap<String, Double> dataCriteria;
private static LinkedHashMap<Boolean, Double> userLoggingCriteria;
private static long sleepTimeOfCommand;
private static long durationOfRun;
br = new BufferedReader(new FileReader("S:\\Testing\\PDSTest1.txt"));
values = new ArrayList<String>();
while ((sCurrentLine = br.readLine()) != null) {
if(sCurrentLine.startsWith("DurationOfRun")) {
durationOfRun = Long.parseLong(sCurrentLine.split(":")[1]);
} else if(sCurrentLine.startsWith("ThreadSize")) {
noOfThreads = Integer.parseInt(sCurrentLine.split(":")[1]);
} else if(sCurrentLine.startsWith("ExistingRange")) {
startRange = Integer.parseInt(sCurrentLine.split(":")[1].split("-")[0]);
endRange = Integer.parseInt(sCurrentLine.split(":")[1].split("-")[1]);
} else if(sCurrentLine.startsWith("NewRange")) {
newStartRange = Integer.parseInt(sCurrentLine.split(":")[1].split("-")[0]);
newEndRange = Integer.parseInt(sCurrentLine.split(":")[1].split("-")[1]);
} else {
attributeIDGet = new ArrayList<String>();
attributeIDSet = new ArrayList<String>();
dataCriteria = new LinkedHashMap<String, Double>();
userLoggingCriteria = new LinkedHashMap<Boolean, Double>();
percentage = sCurrentLine.split("-")[0].split(":")[1].trim();
values = Arrays.asList(sCurrentLine.split("-")[1].trim().split("\\s+"));
for(String s : values) {
if(s.startsWith("Data")) {
String[] data = s.split(":")[1].split(",");
for (String n : data) {
dataCriteria.put(n.split("/")[0], Double.parseDouble(n.split("/")[1]));
}
//dataCriteria.put(data.split("/")[0], value)
} else if(s.startsWith("UserLogged")) {
String[] userLogged = s.split(":")[1].split(",");
for (String t : userLogged) {
userLoggingCriteria.put(Boolean.parseBoolean(t.split("/")[0]), Double.parseDouble(t.split("/")[1]));
}
//userLogged = Boolean.parseBoolean(s.split(":")[1]);
} else if(s.startsWith("SleepTime")) {
sleepTimeOfCommand = Long.parseLong(s.split(":")[1]);
} else if(s.startsWith("AttributeGet")) {
String[] strGet = s.split(":")[1].split(",");
for(String q : strGet) attributeIDGet.add(q);
} else if(s.startsWith("AttributeSet:")) {
String[] strSet = s.split(":")[1].split(",");
for(String p : strSet) attributeIDSet.add(p);
} else {
commandName = s;
}
}
Command command = new Command();
command.setName(commandName);
command.setExecutionPercentage(Double.parseDouble(percentage));
command.setAttributeIDGet(attributeIDGet);
command.setAttributeIDSet(attributeIDSet);
command.setDataUsageCriteria(dataCriteria);
command.setUserLoggingCriteria(userLoggingCriteria);
command.setSleepTime(sleepTimeOfCommand);
commands.add(command);
最佳答案
嗯,一旦你深入到解析器的较低层,解析器通常会变得很困惑:-)
但是,至少在代码质量方面,一个可能的改进是认识到语法是分层的这一事实。
我的意思是每一行都是一个识别标记,后面跟着一些属性。
对于DurationOfRun
、ThreadSize
、ExistingRange
和NewRange
来说,属性相对简单。 百分比
有点复杂,但仍然可以。
我会将代码构造为(伪代码):
def parseFile (fileHandle):
while (currentLine = fileHandle.getNextLine()) != EOF:
if currentLine.beginsWith ("DurationOfRun:"):
processDurationOfRun (currentLine[14:])
elsif currentLine.beginsWith ("ThreadSize:"):
processThreadSize (currentLine[11:])
elsif currentLine.beginsWith ("ExistingRange:"):
processExistingRange (currentLine[14:])
elsif currentLine.beginsWith ("NewRange:"):
processNewRange (currentLine[9:])
elsif currentLine.beginsWith ("Percentage:"):
processPercentage (currentLine[11:])
else
raise error
然后,在每个 processWhatever()
函数中,您根据预期格式解析该行的其余部分。这使您的代码保持小而可读,并且将来可以轻松更改,而无需在泥沼中导航:-)
例如,processDurationOfRun()
只是从该行的其余部分获取一个整数:
def processDurationOfRun (line):
this.durationOfRun = line.parseAsInt()
类似地,两个范围的函数在 -
上分割字符串,并从结果值中获取两个整数:
def processExistingRange (line):
values[] = line.split("-")
this.existingRangeStart = values[0].parseAsInt()
this.existingRangeEnd = values[1].parseAsInt()
processPercentage()
函数是一个棘手的函数,但如果你也分层它也很容易实现。假设这些东西总是按相同的顺序排列,它包括:
-
;键:值
对。甚至这些对内的值也可以由较低级别进行解析,首先用逗号分隔以获得子值,例如 Previous/30
和 New/70
,然后用斜杠分割每个子值以获得单独的项目。这样,逻辑层次结构就可以反射(reflect)在您的代码中。
除非您希望每秒解析此文本文件很多次,或者除非它的大小很多兆字节,否则我会更关心可读性和代码的可维护性比解析的速度。
我们需要从代码中榨取最后一丝性能的日子已经一去不复返了,但当发现错误或需要增强时,我们仍然无法及时修复所述代码。
有时最好针对可读性进行优化。
关于java - 解析 .txt 文件(考虑性能指标),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10791918/
我一直认为使用“if”比捕获异常要好得多(就性能而言)。例如,这样做: User u = Users.getUser("Michael Jordan"); if(u!=null) System.
我正在尝试使用下一个格式将字符转换为日期。我有下一个数据框 i (我在最后添加了 dput() 版本的数据框): Date 1 Dec_28_2012_9:
考虑到 PHP 中的日期戳,我如何计算持续时间?我在日期之间使用的日期格式是“Y-m-d H:i:s”, 我的工作代码只能计算时间之间的持续时间而不考虑日期。 下面是我的代码: $assigned_t
我正在尝试解释 GLMM 中的自相关。我的响应变量是 bool 值,它表示一组 hive 的生命周期中是否存在 en 事件。我试图用一组描述每个巢状态的数值变量来预测此类事件的概率。因此,我在广义模型
我对如何解释 undefined variable 感到有点困惑(我不确定你现在是否可以)。我正在尝试使用以下代码底部附近的 if else 语句(已注释掉的行)。 这个想法是,如果请求歌曲的人不是与
Bjarne Stroustrup 的 The C++ Programming Language Fourth Edition 中的以下内容是什么意思? "Consider . (dot) suspe
我想要一个主元素,边 block 漂浮在它的右侧。我不知道边 block 的数量,也不知道它们的最终总高度。但是我的主要元素应该具有相同的高度(请参阅以下示例以更好地理解),而无需使用列。 (虚线部分
我在每个 TextView 上都有以下警告(来自 Lint),在我的 XML 中有一个 ID。 Consider making the text value selectable by specify
目前,我有 6 条曲线,以 6 种不同的颜色显示,如下所示。 这 6 条曲线实际上是由 一个相同实验 的 6 次试验生成的。这意味着,理想情况下它们应该是相同的曲线,但由于噪声和不同的试验参与者,它们
winner of a recent Wikipedia vandalism detection competition建议可以通过“检测考虑到 QWERTY 的随机键盘点击来改进检测键盘布局”。 示
多年来,我一直在编写 C 语言,主要是在嵌入式环境中,并且对指针有一个非常好的心智模型——我不必明确地考虑如何使用它们,我对指针算法 100% 感到满意,指针数组,指针指针等。 我写的 C++ 很少,
我正在使用 Boost.Date_time 来获取两个日期之间的时差。我希望代码在这些天也考虑夏令时的变化,并给我正确的时间间隔。 考虑这个例子。 2015 年 11 月 1 日,美国的 DST 将发
我有一个(人类)名字的向量,全部用大写字母表示: names <- c("FRIEDRICH SCHILLER", "FRANK O'HARA", "HANS-CHRISTIAN ANDERSEN")
我想呈现一个表单小部件。这是我要生成的原始 HTML: 使用这个: {{ form_row(form.email, { 'type' : 'email', 'attr' : { 'class' :
我正在开发一个 python 项目,它使用 pythonnet 和几个 C# dll 作为依赖项。 由于我不想将 dll 推送到 git 存储库,因此我调整了 .gitignore 文件。但是,现在
考虑到上午/下午,我想将字符串转换为 php 数据时间。 我想将 '03/06/2015 12:17 am' 转换为 php datatime。 我试过了, $myDateTime = DateTim
我想排除那些具有相同标题和同一年份的实例。 title votes ranking year 0 Wonderland 19 7.9 1931 1
例如对于一个 EditText,通常指定 android:inputType="numberDecimal"用于文本字段应该包含十进制数。但这假设“。”用作小数点分隔符,在某些国家/地区使用“,”代替
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 9 年前。 Improv
作为练习,我决定学习 Java 中的 lambda 表达式。我想重写我发现笨拙且冗长的旧代码。它检查命令行参数是否是(1)文件路径或(2)目录路径。在(1)场景中,它将命令行参数传递给方法。在 (2)
我是一名优秀的程序员,十分优秀!