1

コードをアーカイブからリソースを読み取るように変換してから、アプリケーションのパフォーマンスがわずかに低下していることに気付きました。フラットファイルシステムを使用しているとき、または可能な限り近いパフォーマンスを元に戻すために、次のコードをどのように改善しますか?

class Resource
{
    private static readonly string ResourcePath = AppDomain.CurrentDomain.BaseDirectory + "resources.pak";
    private const string ResourcePassword = "0ORzjHcFxV0QXTuOizu0";

    private static ZipFile Read(string filepath, string password = null)
    {
        var file = ZipFile.Read(filepath);
        file.Password = password;
        return file;
    }

    internal static List<string> GetFiles(string haystack, string needle = "*")
    {
        var resource = Read(ResourcePath, ResourcePassword).SelectEntries(needle, haystack);
        return resource.Select(filepath => filepath.ToString().Replace("ZipEntry::", "")).ToList();
    }

    internal static MemoryStream ReadFile(string filepath)
    {
        var file = Read(ResourcePath, ResourcePassword);
        var stream = new MemoryStream();
        file[filepath].Extract(stream);
        return stream;
    }

    internal static ImageSource StreamImage(string filepath)
    {
        var getFile = Resource.ReadFile(filepath);
        var imageSource = new BitmapImage();
        imageSource.BeginInit();
        imageSource.StreamSource = getFile;
        imageSource.EndInit();
        return imageSource;
    }

    internal static int[] ImageSize(string filepath)
    {
        int[] sizeValues = {0, 0};
        var readImage = Resource.ReadFile(filepath);
        var image = new BitmapImage();
        image.BeginInit();
        image.StreamSource = readImage;
        image.EndInit();
        sizeValues[0] = image.PixelWidth;
        sizeValues[1] = image.PixelHeight;
        return sizeValues;
    }

}

コードを短く、要点を明確にするために、エラーチェックを削除したことに注意してください。

4

1 に答える 1

0

パフォーマンスの低下とはどういう意味ですか?私はあなたが応答性を意味すると思いますか?これは、メインスレッドでこれらのリソースを解凍していることが原因である可能性があります。この作品をバックグラウンドスレッドに移動します。

これは、async/awaitを使用した.Net4.5の例です。(未テスト)

class Resource
{
    private static readonly string ResourcePath = AppDomain.CurrentDomain.BaseDirectory + "resources.pak";
    private const string ResourcePassword = "0ORzjHcFxV0QXTuOizu0";

    private static ZipFile Read(string filepath, string password = null)
    {
        var file = ZipFile.Read(filepath);
        file.Password = password;
        return file;
    }

    internal static List<string> GetFiles(string haystack, string needle = "*")
    {
        var resource = Read(ResourcePath, ResourcePassword).SelectEntries(needle, haystack);
        return resource.Select(filepath => filepath.ToString().Replace("ZipEntry::", "")).ToList();
    }

    internal static Task<MemoryStream> ReadFile(string filepath)
    {
        return Task.Run(() =>
            {
                var file = Read(ResourcePath, ResourcePassword);
                var stream = new MemoryStream();
                file[filepath].Extract(stream);
                return stream;
            });
    }

    internal async static Task<ImageSource> StreamImage(string filepath)
    {
        var imageSource = new BitmapImage();
        imageSource.BeginInit();
        imageSource.StreamSource = await ReadFile(filepath);
        imageSource.EndInit();
        return imageSource;
    }

    internal async static Task<int[]> ImageSize(string filepath)
    {
        int[] sizeValues = { 0, 0 };
        var image = new BitmapImage();
        image.BeginInit();
        image.StreamSource = await ReadFile(filepath);
        image.EndInit();
        sizeValues[0] = image.PixelWidth;
        sizeValues[1] = image.PixelHeight;
        return sizeValues;
    }
}
于 2012-12-16T12:38:02.203 に答える