Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve performance of getTimeString #2179

Merged
merged 1 commit into from
Mar 4, 2019
Merged

Improve performance of getTimeString #2179

merged 1 commit into from
Mar 4, 2019

Conversation

Redirion
Copy link
Member

@Redirion Redirion commented Mar 4, 2019

This pull requests complements pull request #2178 by reducing general computational time for the method getTimeString.

On my local machine (Desktop PC with Java) my tests with a sample size of 10000 calls to the method with param 86400001 showed an average performance improvement of about 50%.

See sample code below to reproduce:

private static final StringBuilder stringBuilder = new StringBuilder();
private static final Formatter stringFormatter = new Formatter(stringBuilder, Locale.getDefault());

public static String getTimeString(int milliSeconds) {
    int seconds = (milliSeconds % 60000) / 1000;
    int minutes = (milliSeconds % 3600000) / 60000;
    int hours = (milliSeconds % 86400000) / 3600000;
    int days = (milliSeconds % (86400000 * 7)) / 86400000;

    stringBuilder.setLength(0);
    return days > 0 ? stringFormatter.format("%d:%02d:%02d:%02d", days, hours, minutes, seconds).toString()
            : hours > 0 ? stringFormatter.format("%d:%02d:%02d", hours, minutes, seconds).toString()
            : stringFormatter.format("%02d:%02d", minutes, seconds).toString();
}

public static String getTimeStringL(int milliSeconds) {
    long seconds = (milliSeconds % 60000L) / 1000L;
    long minutes = (milliSeconds % 3600000L) / 60000L;
    long hours = (milliSeconds % 86400000L) / 3600000L;
    long days = (milliSeconds % (86400000L * 7L)) / 86400000L;

    stringBuilder.setLength(0);
    return days > 0 ? stringFormatter.format("%d:%02d:%02d:%02d", days, hours, minutes, seconds).toString()
            : hours > 0 ? stringFormatter.format("%d:%02d:%02d", hours, minutes, seconds).toString()
            : stringFormatter.format("%02d:%02d", minutes, seconds).toString();
}

public static void main(String[] args) throws Exception {
	final int SAMPLE_SIZE = 10000;
	long[] results = new long[SAMPLE_SIZE];
	for(int i = 0; i < SAMPLE_SIZE; i++) {
		long now = System.nanoTime();
		getTimeString(86400001);
		results[i] = System.nanoTime() - now;
	}
	long sum = 0;
	for(int i = 0; i < SAMPLE_SIZE; i++) {
		sum += results[i];
	}
	System.out.println("Average execution time: " + (sum/SAMPLE_SIZE));
	results = new long[SAMPLE_SIZE];
	for(int i = 0; i < SAMPLE_SIZE; i++) {
		long now = System.nanoTime();
		getTimeStringL(86400001);
		results[i] = System.nanoTime() - now;
	}
	sum = 0;
	for(int i = 0; i < SAMPLE_SIZE; i++) {
		sum += results[i];
	}
	System.out.println("Average execution time: " + (sum/SAMPLE_SIZE));
}

This pull requests complements pull request  #2178 by reducing general computational time for the method getTimeString.

On my local machine (Desktop PC with Java) my tests with a sample size of 10000 calls to the method with param 86400001 showed a performance improvement of about 50%.

See sample code below to reproduce:

    private static final StringBuilder stringBuilder = new StringBuilder();
    private static final Formatter stringFormatter = new Formatter(stringBuilder, Locale.getDefault());
    
    public static String getTimeString(int milliSeconds) {
        int seconds = (milliSeconds % 60000) / 1000;
        int minutes = (milliSeconds % 3600000) / 60000;
        int hours = (milliSeconds % 86400000) / 3600000;
        int days = (milliSeconds % (86400000 * 7)) / 86400000;

        stringBuilder.setLength(0);
        return days > 0 ? stringFormatter.format("%d:%02d:%02d:%02d", days, hours, minutes, seconds).toString()
                : hours > 0 ? stringFormatter.format("%d:%02d:%02d", hours, minutes, seconds).toString()
                : stringFormatter.format("%02d:%02d", minutes, seconds).toString();
    }
    
    public static String getTimeStringL(int milliSeconds) {
        long seconds = (milliSeconds % 60000L) / 1000L;
        long minutes = (milliSeconds % 3600000L) / 60000L;
        long hours = (milliSeconds % 86400000L) / 3600000L;
        long days = (milliSeconds % (86400000L * 7L)) / 86400000L;

        stringBuilder.setLength(0);
        return days > 0 ? stringFormatter.format("%d:%02d:%02d:%02d", days, hours, minutes, seconds).toString()
                : hours > 0 ? stringFormatter.format("%d:%02d:%02d", hours, minutes, seconds).toString()
                : stringFormatter.format("%02d:%02d", minutes, seconds).toString();
    }
    
	public static void main(String[] args) throws Exception {
		final int SAMPLE_SIZE = 25000;
		long[] results = new long[SAMPLE_SIZE];
		for(int i = 0; i < SAMPLE_SIZE; i++) {
			long now = System.nanoTime();
			getTimeString(86400001);
			results[i] = System.nanoTime() - now;
		}
		long sum = 0;
		for(int i = 0; i < SAMPLE_SIZE; i++) {
			sum += results[i];
		}
		System.out.println("Average execution time: " + (sum/SAMPLE_SIZE));
		results = new long[SAMPLE_SIZE];
		for(int i = 0; i < SAMPLE_SIZE; i++) {
			long now = System.nanoTime();
			getTimeStringL(86400001);
			results[i] = System.nanoTime() - now;
		}
		sum = 0;
		for(int i = 0; i < SAMPLE_SIZE; i++) {
			sum += results[i];
		}
		System.out.println("Average execution time: " + (sum/SAMPLE_SIZE));
@Redirion Redirion changed the title Improved performance of getTimeString Improve performance of getTimeString Mar 4, 2019
Copy link
Contributor

@TobiGr TobiGr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a good question, why long was used before. Nice catch 👍

@TobiGr TobiGr merged commit aec3f19 into TeamNewPipe:dev Mar 4, 2019
@Redirion Redirion deleted the patch-2 branch March 5, 2019 20:12
This was referenced Apr 28, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants