WP7 - 載入圖片速度比較
目前對於從IsolatedStorageFile裡存取圖片
我所知道的有幾種方法
儲存的方法有Extensions.SaveJpeg和IsolatedStorageFileStream.Write
載入的方法有Extensions.LoadJpeg和BitmapImage.SetSource
因為最近的APP必須大量存取IsolatedStorageFile裡的圖
所以做了一個效能測試
測試方法:
首先選擇一個圖片
然後儲存此圖到IsolatedStorage一百次~檔名用流水號
再從IsolatedStorage取得此一百張圖
最後全部刪除
時間計算就用簡單的DataTime來計算毫秒
第一種模式
使用Extensions.SaveJpeg 和 Extensions.LoadJpeg
程式碼片段如下
1: Result.Text += "Begin Mode 1\r\n";
2: using (var appStorage = IsolatedStorageFile.GetUserStoreForApplication()) {
3: DateTime start = DateTime.Now;
4: for (int i = 0; i < MaxCount; i++) {
5: using (var file = appStorage.OpenFile(string.Format("m1_{0}", i), System.IO.FileMode.OpenOrCreate)) {
6: image.SaveJpeg(file, image.PixelWidth, image.PixelHeight, 0, 75);
7: size += file.Length;
8: }
9: }
10: TimeSpan ts = DateTime.Now - start;
11: Result.Text += string.Format("Save MS={0} Size={1}\r\n", ts.TotalMilliseconds, size / MaxCount);
12: size = 0;
13: start = DateTime.Now;
14: for (int i = 0; i < MaxCount; i++) {
15: using (var file = appStorage.OpenFile(string.Format("m1_{0}", i), System.IO.FileMode.OpenOrCreate)) {
16: image.LoadJpeg(file);
17: size += file.Length;
18: }
19: }
20: ts = DateTime.Now - start;
21: Result.Text += string.Format("Load MS={0} Size={1}\r\n", ts.TotalMilliseconds, size / MaxCount);
22: start = DateTime.Now;
23: for (int i = 0; i < MaxCount; i++) {
24: appStorage.DeleteFile(string.Format("m1_{0}", i));
25: }
26: ts = DateTime.Now - start;
27: Result.Text += string.Format("Delete MS={0}\r\n", ts.TotalMilliseconds);
28:
29: }
第二種模式
使用IsolatedStorageFileStream.Write 和BitmapImage.SetSource
程式碼片段如下
1: Result.Text += "Begin mode 2\r\n";
2: using (var appStorage = IsolatedStorageFile.GetUserStoreForApplication()) {
3: DateTime start = DateTime.Now;
4: for (int i = 0; i < MaxCount; i++) {
5: using (var file = appStorage.OpenFile(string.Format("m2_{0}", i), System.IO.FileMode.OpenOrCreate)) {
6: byte[] bytes;
7: using (MemoryStream stream = new MemoryStream()) {
8: image.SaveJpeg(stream, image.PixelWidth, image.PixelHeight, 0, 75);
9: bytes = stream.ToArray();
10: }
11: file.Write(bytes, 0, bytes.Length);
12: size += file.Length;
13: }
14: }
15: TimeSpan ts = DateTime.Now - start;
16: Result.Text += string.Format("Save MS={0} Size={1}\r\n", ts.TotalMilliseconds, size / MaxCount);
17: size = 0;
18: start = DateTime.Now;
19: for (int i = 0; i < MaxCount; i++) {
20: using (var file = appStorage.OpenFile(string.Format("m2_{0}", i), System.IO.FileMode.OpenOrCreate)) {
21: BitmapImage bb = new BitmapImage();
22: bb.SetSource(file);
23: size += file.Length;
24: }
25: }
26: ts = DateTime.Now - start;
27: Result.Text += string.Format("Load MS={0} Size={1}\r\n", ts.TotalMilliseconds, size / MaxCount);
28: start = DateTime.Now;
29: for (int i = 0; i < MaxCount; i++) {
30: appStorage.DeleteFile(string.Format("m2_{0}", i));
31: }
32: ts = DateTime.Now - start;
33: Result.Text += string.Format("Delete MS={0}\r\n", ts.TotalMilliseconds);
34: }
第三種模式
和第一種一樣~但是載入的時候選擇比較小的SIZE
1: Result.Text += "Begin Mode 3\r\n";
2: using (var appStorage = IsolatedStorageFile.GetUserStoreForApplication()) {
3: DateTime start = DateTime.Now;
4: for (int i = 0; i < MaxCount; i++) {
5: using (var file = appStorage.OpenFile(string.Format("m3_{0}", i), System.IO.FileMode.OpenOrCreate)) {
6: image.SaveJpeg(file, image.PixelWidth, image.PixelHeight, 0, 75);
7: size += file.Length;
8: }
9: }
10: TimeSpan ts = DateTime.Now - start;
11: Result.Text += string.Format("Save MS={0} Size={1}\r\n", ts.TotalMilliseconds, size / MaxCount);
12: size = 0;
13: start = DateTime.Now;
14: for (int i = 0; i < MaxCount; i++) {
15: using (var file = appStorage.OpenFile(string.Format("m3_{0}", i), System.IO.FileMode.OpenOrCreate)) {
16: WriteableBitmap bit = new WriteableBitmap(150, 150);
17: bit.LoadJpeg(file);
18: size += file.Length;
19: }
20: }
21: ts = DateTime.Now - start;
22: Result.Text += string.Format("Load MS={0} Size={1}\r\n", ts.TotalMilliseconds, size / MaxCount);
23: start = DateTime.Now;
24: for (int i = 0; i < MaxCount; i++) {
25: appStorage.DeleteFile(string.Format("m3_{0}", i));
26: }
27: ts = DateTime.Now - start;
28: Result.Text += string.Format("Delete MS={0}\r\n", ts.TotalMilliseconds);
29:
30: }
接著是測試結果
M1 | ||
Save | Load | Delete |
14731 | 14337 | 349 |
14800 | 17437 | 398 |
16440 | 18838 | 489 |
17011 | 19886 | 506 |
15168 | 15497 | 398 |
平均 | ||
15630 | 17199 | 428 |
M2 | ||
Save | Load | Delete |
14665 | 7197 | 361 |
13541 | 7028 | 391 |
14018 | 7054 | 407 |
15559 | 7023 | 402 |
13776 | 7034 | 400 |
平均 | ||
14312 | 7067 | 392.2 |
M3 | ||
Save | Load | Delete |
14674 | 15115 | 343 |
14218 | 11323 | 400 |
14558 | 12555 | 403 |
15738 | 16345 | 409 |
19552 | 17785 | 413 |
平均 | ||
15748 | 14625 | 393.6 |
上面每個數據都是用HD7實機測試~OS:7.0.7008.0
單位都是毫秒
可以看的出來M2的儲存方式雖然是先用Extensions.SaveJpeg存到Memory裡再用IsolatedStorageFile.Write
卻還是比直接用Extensions.SaveJpeg還要快~雖然只快一點
而差異比較大的是Extensions.LoadJpeg 和 BitmapImage.SetSource
幾乎差了一倍的效能
而載入的時候把圖縮小是有提升些許效能的
雖然Extensions.LoadJpeg 和 BitmapImage.SetSource效能差一倍
但是Extensions.LoadJpeg 可以在非主執行續使用
BitmapImage.SetSource就一定要在主執行續使用
但是~~效能還是比不上Pucture Hub載入圖片的速度啊!