[Android]BitmapFactory.Options inSampleSize の性能テスト

BitmapFactory.Options inSampleSize の性能テストとか動作とか

Doc

以下のような事が書いてありますが…

public int inSampleSize

Since: API Level 1
If set to a value > 1, requests the decoder to subsample the original image, returning a smaller image to save memory. The sample size is the number of pixels in either dimension that correspond to a single pixel in the decoded bitmap. For example, inSampleSize == 4 returns an image that is 1/4 the width/height of the original, and 1/16 the number of pixels. Any value <= 1 is treated the same as 1. Note: the decoder will try to fulfill this request, but the resulting bitmap may have different dimensions that precisely what has been requested. Also, powers of 2 are often faster/easier for the decoder to honor.

検証用ソース

1/1から、1/36の設定までを10回ずつループ。
出力された画像の幅と、処理時間の平均を出力。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Resources res = getResources();
BitmapFactory.Options options = new BitmapFactory.Options();
 
int sizeMax = 36;
int count = 10;
 
long[][] result = new long[sizeMax][count];
long start;
 
for (int i = 1; i <= sizeMax; i++) {
	for(int j = 0; j < count; j++) {
		options.inSampleSize = i;
		start = System.currentTimeMillis();
		BitmapFactory.decodeResource(res, R.drawable.image, options);
		result[i-1][j] = System.currentTimeMillis() - start;
	}
	Log.d("width" + i, String.valueOf(options.outWidth));
}
 
double sec;
 
for (int i = 0; i < sizeMax; i++) {
	sec = 0;
	for (int j = 0; j < count; j++) {
		sec += result[i][j];
	}
	Log.d("avg:" + (i+1), String.valueOf(sec/count));
}
 
return null;

出力された幅と処理平均時間

width1 1840 avg:1 519.5
width2 920 avg:2 228.8
width3 920 avg:3 233.9
width4 460 avg:4 141.2
width5 460 avg:5 142.0
width6 460 avg:6 141.0
width7 460 avg:7 141.3
width8 230 avg:8 104.7
width9 230 avg:9 103.1
width10 230 avg:10 142.2
width11 230 avg:11 103.6
width12 230 avg:12 108.0
width13 230 avg:13 102.1
width14 230 avg:14 104.3
width15 230 avg:15 105.5
width16 115 avg:16 101.4
width17 115 avg:17 104.5
width18 115 avg:18 101.0
width19 115 avg:19 102.8
width20 115 avg:20 102.1
width21 115 avg:21 104.3
width22 115 avg:22 102.5
width23 115 avg:23 101.1
width24 76 avg:24 101.0
width25 76 avg:25 101.1
width26 76 avg:26 101.6
width27 76 avg:27 100.5
width28 76 avg:28 100.8
width29 76 avg:29 101.8
width30 76 avg:30 100.5
width31 76 avg:31 101.8
width32 57 avg:32 100.6
width33 57 avg:33 100.7
width34 57 avg:34 100.8
width35 57 avg:35 100.6
width36 57 avg:36 100.6

言いたいこととか

javadoc的には、2の累乗なら速度が速いという話ですが、
むしろ、動作の条件の一つとなっております。
また、動作の法則的に、1/24で動作が変わるのは謎です。

別件ですが、WEB経由でInputStreamを使用する場合は、
InputStreamにバグが有るため、skip処理をオーバーライドしたFilterでラップする必要があります。

おまけ、生ログ

01:11:16.633 D/width1(20161) 1840
01:11:16.854 D/dalvikvm(20161) GC_FOR_ALLOC freed 4966K, paused 12ms
01:11:17.134 D/dalvikvm(20161) GC_FOR_ALLOC freed 1243K, paused 13ms
01:11:17.404 D/dalvikvm(20161) GC_FOR_ALLOC freed 1243K, paused 24ms
01:11:17.725 D/dalvikvm(20161) GC_FOR_ALLOC freed 1243K, paused 14ms
01:11:17.955 D/dalvikvm(20161) GC_FOR_ALLOC freed 1243K, paused 12ms
01:11:18.145 D/dalvikvm(20161) GC_FOR_ALLOC freed 1243K, paused 12ms
01:11:18.335 D/dalvikvm(20161) GC_FOR_ALLOC freed 1243K, paused 12ms
01:11:18.535 D/dalvikvm(20161) GC_FOR_ALLOC freed 1243K, paused 12ms
01:11:18.725 D/dalvikvm(20161) GC_FOR_ALLOC freed 1243K, paused 12ms
01:11:18.906 D/dalvikvm(20161) GC_FOR_ALLOC freed 1243K, paused 12ms
01:11:18.926 D/width2(20161) 920
01:11:19.096 D/dalvikvm(20161) GC_FOR_ALLOC freed 1243K, paused 12ms
01:11:19.286 D/dalvikvm(20161) GC_FOR_ALLOC freed 1243K, paused 12ms
01:11:19.506 D/dalvikvm(20161) GC_FOR_ALLOC freed 1243K, paused 14ms
01:11:19.746 D/dalvikvm(20161) GC_FOR_ALLOC freed 1243K, paused 13ms
01:11:19.977 D/dalvikvm(20161) GC_FOR_ALLOC freed 1243K, paused 14ms
01:11:20.217 D/dalvikvm(20161) GC_FOR_ALLOC freed 1243K, paused 12ms
01:11:20.457 D/dalvikvm(20161) GC_FOR_ALLOC freed 1243K, paused 11ms
01:11:20.717 D/dalvikvm(20161) GC_FOR_ALLOC freed 1243K, paused 12ms
01:11:20.998 D/dalvikvm(20161) GC_FOR_ALLOC freed 1243K, paused 12ms
01:11:21.248 D/dalvikvm(20161) GC_FOR_ALLOC freed 1243K, paused 11ms
01:11:21.268 D/width3(20161) 920
01:11:21.548 D/dalvikvm(20161) GC_FOR_ALLOC freed 1554K, paused 12ms
01:11:22.249 D/dalvikvm(20161) GC_FOR_ALLOC freed 1553K, paused 11ms
01:11:22.679 D/width4(20161) 460
01:11:22.980 D/dalvikvm(20161) GC_FOR_ALLOC freed 1554K, paused 12ms
01:11:23.680 D/dalvikvm(20161) GC_FOR_ALLOC freed 1554K, paused 12ms
01:11:24.101 D/width5(20161) 460
01:11:24.381 D/dalvikvm(20161) GC_FOR_ALLOC freed 1554K, paused 12ms
01:11:25.092 D/dalvikvm(20161) GC_FOR_ALLOC freed 1554K, paused 21ms
01:11:25.512 D/width6(20161) 460
01:11:25.792 D/dalvikvm(20161) GC_FOR_ALLOC freed 1554K, paused 12ms
01:11:26.503 D/dalvikvm(20161) GC_FOR_ALLOC freed 1554K, paused 11ms
01:11:26.913 D/width7(20161) 460
01:11:27.554 D/dalvikvm(20161) GC_CONCURRENT freed 1639K, paused 1ms+1ms
01:11:27.965 D/width8(20161) 230
01:11:28.996 D/width9(20161) 230
01:11:30.227 D/dalvikvm(20161) GC_CONCURRENT freed 1743K, paused 1ms+3ms
01:11:30.417 D/width10(20161) 230
01:11:31.458 D/width11(20161) 230
01:11:32.539 D/dalvikvm(20161) GC_CONCURRENT freed 1745K, paused 1ms+1ms
01:11:32.539 D/width12(20161) 230
01:11:33.560 D/width13(20161) 230
01:11:34.601 D/width14(20161) 230
01:11:34.831 D/dalvikvm(20161) GC_CONCURRENT freed 1745K, paused 1ms+2ms
01:11:35.662 D/width15(20161) 230
01:11:36.663 D/width16(20161) 115
01:11:37.714 D/width17(20161) 115
01:11:38.725 D/width18(20161) 115
01:11:39.756 D/width19(20161) 115
01:11:40.777 D/width20(20161) 115
01:11:41.117 D/dalvikvm(20161) GC_CONCURRENT freed 1770K, paused 1ms+1ms
01:11:41.818 D/width21(20161) 115
01:11:42.849 D/width22(20161) 115
01:11:43.860 D/width23(20161) 115
01:11:44.871 D/width24(20161) 76
01:11:45.882 D/width25(20161) 76
01:11:46.883 D/width26(20161) 76
01:11:47.894 D/width27(20161) 76
01:11:48.905 D/width28(20161) 76
01:11:49.926 D/width29(20161) 76
01:11:50.927 D/width30(20161) 76
01:11:51.948 D/width31(20161) 76
01:11:52.959 D/width32(20161) 57
01:11:53.960 D/width33(20161) 57
01:11:54.971 D/width34(20161) 57
01:11:55.982 D/width35(20161) 57
01:11:56.983 D/width36(20161) 57
01:11:56.983 D/avg:1(20161) 519.5
01:11:56.983 D/avg:2(20161) 228.8
01:11:56.983 D/avg:3(20161) 233.9
01:11:56.983 D/avg:4(20161) 141.2
01:11:56.983 D/avg:5(20161) 142.0
01:11:56.983 D/avg:6(20161) 141.0
01:11:56.983 D/avg:7(20161) 141.3
01:11:56.983 D/avg:8(20161) 104.7
01:11:56.983 D/avg:9(20161) 103.1
01:11:56.983 D/avg:10(20161) 142.2
01:11:56.983 D/avg:11(20161) 103.6
01:11:56.983 D/avg:12(20161) 108.0
01:11:56.983 D/avg:13(20161) 102.1
01:11:56.983 D/avg:14(20161) 104.3
01:11:56.983 D/avg:15(20161) 105.5
01:11:56.983 D/avg:16(20161) 101.4
01:11:56.983 D/avg:17(20161) 104.5
01:11:56.983 D/avg:18(20161) 101.0
01:11:56.983 D/avg:19(20161) 102.8
01:11:56.983 D/avg:20(20161) 102.1
01:11:56.983 D/avg:21(20161) 104.3
01:11:56.983 D/avg:22(20161) 102.5
01:11:56.983 D/avg:23(20161) 101.1
01:11:56.983 D/avg:24(20161) 101.0
01:11:56.983 D/avg:25(20161) 101.1
01:11:56.983 D/avg:26(20161) 101.6
01:11:56.983 D/avg:27(20161) 100.5
01:11:56.983 D/avg:28(20161) 100.8
01:11:56.983 D/avg:29(20161) 101.8
01:11:56.993 D/avg:30(20161) 100.5
01:11:56.993 D/avg:31(20161) 101.8
01:11:56.993 D/avg:32(20161) 100.6
01:11:56.993 D/avg:33(20161) 100.7
01:11:56.993 D/avg:34(20161) 100.8
01:11:56.993 D/avg:35(20161) 100.6
01:11:56.993 D/avg:36(20161) 100.6

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください