Jump to content

Hh:mm:ss Run Time Method?


Supercharger

Recommended Posts

This is the same thing as above but even worse.

 

I really don't see why people insist on making their code unreadable just because "it saves lines". I know the ternary is actually slower in C# when compared to nested if statements because of the JIT trying to make optimizations, not sure about Java.

 

Apaec is faster on average by 90ms on my computer.

 

Apaec          vs.           Czar

69.194                     159.609

 

Baseline code:

package dateBaseLiner;

public class IWannaBeLikeKanye {

	public static void main(String[] args) {
		for (int f = 0; f < 1000; f++) {
		long startTime = System.currentTimeMillis();
		for (int a = 0; a < 1000; a++) {
			for (int y = 0; y < 1000; y++) {
				format(startTime);
			}
		}
		long endTime = System.currentTimeMillis();
		System.out.println((endTime - startTime));
		}
	}

	public static String format(final long time) {
		final int sec = (int) (time / 1000), h = sec / 3600, m = sec / 60 % 60, s = sec % 60;
		return (h < 10 ? "0" + h : h) + ":" + (m < 10 ? "0" + m : m) + ":" + (s < 10 ? "0" + s : s);
	}

	public static String tformat(long time) {
		StringBuilder t = new StringBuilder();
		long total_secs = time / 1000L;
		long total_mins = total_secs / 60L;
		long total_hrs = total_mins / 60L;
		int secs = (int) total_secs % 60;
		int mins = (int) total_mins % 60;
		int hrs = (int) total_hrs % 24;
		if (hrs < 10) {
			t.append("0");
		}
		t.append(hrs).append(":");
		if (mins < 10) {
			t.append("0");
		}
		t.append(mins).append(":");
		if (secs < 10) {
			t.append("0");
		}
		t.append(secs);
		return t.toString();
	}

}

Edited by Fay
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...