0

I'm building a Java method to sum arithmetically two time periods, using JodaTime library. My code works fine, but I think that it's possible optimize to reduce the time execution... Unfortunately, I'm new on JodaTime.

This is my code:

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;
    }
}

Thanks!! :)

user3449772
  • 749
  • 1
  • 14
  • 27
  • 5
    This question appears to be better suited for [Code Review](http://codereview.stackexchange.com/). Although I suggest narrowing down the optimization requirements. – Mena Sep 05 '14 at 10:20
  • 2
    If you want to sum two periods, why are you using strings? Why are you not using Joda Time's `Period` class for *everything*? The input to your `sum` method should be two periods, IMO. (Or possible two durations - it's unclear what your real situation is.) You can then separate out period/duration parsing. – Jon Skeet Sep 05 '14 at 10:21
  • thanks for support! anyway, I'm using two strings because the input comes from a database...and the data are stored as is. – user3449772 Sep 05 '14 at 10:25

1 Answers1

2

The code is really in bad need for a CR.

If you want to make it faster, make it first shorter. The JVM sort of refuses to optimize overlong methods.

Concerning optimization, it looks like you're wasting most time with strings. Anyway, cleaning up the code, then profiling, and then optimizing is the proper order.

From you program isn't exactly clear what you're doing, however, it can't be really right. Doing

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

means that you transform e.g., --12:--34--- into 12:34. Is this intended? If not, where is the dash allowed? If only at the first position like -12:34, then time1_out.substring(1) is surely cleaner and faster. And so on...

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

Is there any database which can't store dates or numbers? If 12:34 means about half past twelf, then store it as DATE. If it means 12 hours and 34 minutes or 12 minutes and 34 seconds, then store it as INTEGER. Never ever manipulate strings if there's a better datatype.

maaartinus
  • 44,714
  • 32
  • 161
  • 320
  • thanks for reply! the replace("-", "") is used to manage the negative hours. In fact, for example, if I have the first input equals to "-8:00" and the second input equals to "-8:00", the result must be "-16:00" because is an arithmetic operation. So, I use this to manage the symbol only. – user3449772 Sep 05 '14 at 11:06
  • @user3449772 So for speed test `charAt(0)` and use `substring(1)`. – maaartinus Sep 05 '14 at 11:36