NewPipe/app/src/main/java/org/schabi/newpipe
Redirion d8b80f961a
Improved performance of getTimeString
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));
2019-03-04 15:45:59 +01:00
..
about Java language level + javadoc + xml 2018-09-11 19:20:10 +02:00
database error handling + imports + unboxing 2018-09-11 19:18:50 +02:00
download MP4 muxer +misc modifications 2019-01-22 18:53:31 -03:00
fragments Merge branch 'dev' into enqueue-playlist 2019-03-03 20:50:00 +01:00
info_list handling timestamp links in comments 2019-03-02 05:12:06 +05:30
local Merge branch 'master' into dev 2019-03-01 09:53:43 +01:00
player Improved performance of getTimeString 2019-03-04 15:45:59 +01:00
report merged upstream/dev 2018-09-29 15:46:47 +05:30
settings Conflict resolution 2018-11-23 01:41:47 +05:30
streams more fixes 2018-11-22 23:33:34 -03:00
util handling timestamp links in comments 2019-03-02 05:12:06 +05:30
views data flow issue + declaration redundancy 2018-09-11 19:18:41 +02:00
ActivityCommunicator.java Update extractor and refactored NewPipe 2017-09-03 13:57:12 -03:00
App.java Conflict resolution 2018-11-23 01:41:47 +05:30
BaseFragment.java error handling + imports + unboxing 2018-09-11 19:18:50 +02:00
CheckForNewAppVersionTask.java Update CheckForNewAppVersionTask.java 2019-02-26 19:33:01 +01:00
Downloader.java merged upstream/dev 2018-12-20 08:51:44 +05:30
ExitActivity.java Update extractor and refactored NewPipe 2017-09-03 13:57:12 -03:00
ImageDownloader.java -Changed thumbnail toggle in disabled mode to load dark dummy image. 2018-03-19 16:44:17 -07:00
MainActivity.java error handling + imports + unboxing 2018-09-11 19:18:50 +02:00
NewPipeDatabase.java Implement subscriptions import/export 2018-03-08 10:39:24 -03:00
PanicResponderActivity.java Update extractor and refactored NewPipe 2017-09-03 13:57:12 -03:00
ReCaptchaActivity.java data flow issue + declaration redundancy 2018-09-11 19:18:41 +02:00
RouterActivity.java handling timestamp links in comments 2019-03-02 05:12:06 +05:30