gpt4 book ai didi

java - 我该如何优化这段代码? (需要 JodaTime)

转载 作者:行者123 更新时间:2023-12-02 05:29:34 25 4
gpt4 key购买 nike

我正在构建一个 Java 方法,使用 JodaTime 库对两个时间段进行算术求和。我的代码工作正常,但我认为可以优化以减少执行时间......不幸的是,我是 JodaTime 的新手。

这是我的代码:

import org.joda.time.Period;
import org.joda.time.PeriodType;
import org.joda.time.format.PeriodFormatter;
import org.joda.time.format.PeriodFormatterBuilder;


public class Test {

public static void main(String[] args)
{
String time1 = "08:00";
String time2 = "08:00";

System.out.println(Operation_Sum(time1,time2));
}

private static String Operation_Sum(String time1, String time2)
{
String output;
long start = System.currentTimeMillis();

if (time1.equals("00:00") && time2.equals("00:00"))
{
output = "00:00";
}
else if (time1.equals("00:00"))
{
output = time2;
}
else if (time2.equals("00:00"))
{
output = time1;
}
else
{
boolean sign_time1 = false;
boolean sign_time2 = false;
String[] time1_out, time2_out;

boolean negative = false;
String output_split[];

if (time1.contains("-"))
{
time1_out = time1.split(":");
time1_out[0] = time1_out[0].replace("-", "");
time1_out[1] = time1_out[1].replace("-", "");
time1 = time1_out[0] + ':' + time1_out[1];
sign_time1 = true;
}

if (time2.contains("-"))
{
time2_out = time2.split(":");
time2_out[0] = time2_out[0].replace("-", "");
time2_out[1] = time2_out[1].replace("-", "");
time2 = time2_out[0] + ':' + time2_out[1];
sign_time2 = true;
}

PeriodFormatterBuilder builder = new PeriodFormatterBuilder();
builder.minimumPrintedDigits(2);
builder.printZeroAlways();
builder.appendHours();
builder.appendLiteral(":");
builder.appendMinutes();
PeriodFormatter pf = builder.toFormatter();

Period period1 = pf.parsePeriod(time1);
Period period2 = pf.parsePeriod(time2);
Period normalized;
Period total = null;


if (sign_time1 && sign_time2)
{
total = period1.plus(period2);
negative = true;
}

if (sign_time1 && !sign_time2)
{
total = period2.minus(period1);
}

if (!sign_time1 && sign_time2)
{
total = period1.minus(period2);
}

if (!sign_time1 && !sign_time2)
{
total = period1.plus(period2);
negative = false;
}

normalized = total.normalizedStandard(PeriodType.time());
output_split = pf.print(normalized).split(":");

if (output_split[1].contains("-"))
{
output = output_split[0] + ":" + output_split[1].replace("-", "");
}
else
{
output = (negative ? "-" : "") + output_split[0] + ":" + output_split[1];
}
}

long end = System.currentTimeMillis();

System.out.println("exec time sum1: " +(end - start) + " ms");
return output;
}
}

谢谢!! :)

最佳答案

该代码确实非常需要 CR。

如果你想让它更快,就先让它更短。 JVM 有点拒绝优化过长的方法。

关于优化,看起来您在字符串上浪费了大部分时间。不管怎样,清理代码,然后分析,然后优化是正确的顺序。

从你的程序来看,你在做什么并不完全清楚,但是,它不可能真的正确。正在做

time1_out[0] = time1_out[0].replace("-", "");

表示您将 --12:--34--- 转换为 12:34。这是故意的吗?如果不是,哪里允许破折号?如果仅在第一个位置(例如 -12:34),那么 time1_out.substring(1) 肯定更干净、更快。等等...

I'm using two strings because the input comes from a database

是否有任何数据库不能存储日期或数字?如果 12:34 表示大约十二点半,则将其存储为 DATE。如果表示 12 小时 34 分钟或 12 分 34 秒,则将其存储为 INTEGER。如果有更好的数据类型,永远不要操作字符串。

关于java - 我该如何优化这段代码? (需要 JodaTime),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25683624/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com