powered by simpleCommunicator - 2.0.56     © 2025 Programmizd 02
Целевая тема:
Создать новую тему:
Автор:
Закрыть
Цитировать
Форумы / WinForms, .Net Framework [игнор отключен] [закрыт для гостей] / Как повысить производительность параллельной обработки
4 сообщений из 4, страница 1 из 1
Как повысить производительность параллельной обработки
    #38328852
Testor1
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
Всем привет!

На основе примера Microsoft по работе с библиотекой GZipStream создал код для параллельной распаковки файлов .gz.
При обработке больших и тяжелых .gz параллельная распаковка работает медленее чем последовательная.
Как можно изменить код, чтобы повысить производительность распаковки в параллельном режиме ?

Код: c#
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.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
88.
89.
90.
91.
92.
93.
94.
95.
96.
97.
98.
99.
100.
101.
102.
103.
104.
105.
106.
107.
108.
109.
110.
111.
112.
113.
114.
using System;
using System.IO;
using System.IO.Compression;
using System.Threading;
using System.Threading.Tasks;


namespace zip
{

    public class Program
    {

        public static void Main()
        {
            // Path to directory of files to compress and decompress.
            string dirpath = @"c:\temp";

            DirectoryInfo di = new DirectoryInfo(dirpath);

            // Compress the directory's files.
            //foreach (FileInfo fi in di.GetFiles())
            //{
            //    Compress(fi);

            //}

            //Decompress all *.gz files in the directory.
            
            Console.WriteLine("{0}", DateTime.Now);
            foreach (FileInfo fi in di.GetFiles("*.gz"))
            {
                Decompress(fi);
            }
            Console.WriteLine("{0}", DateTime.Now);



            FileInfo[] files = di.GetFiles("*.gz");

            Console.WriteLine("{0}", DateTime.Now);
            Parallel.ForEach(files, currentFile =>
            {
                Decompress(currentFile);
            });
            Console.WriteLine("{0}", DateTime.Now);


            Console.ReadKey();


        }

        public static void Compress(FileInfo fi)
        {
            // Get the stream of the source file.
            using (FileStream inFile = fi.OpenRead())
            {
                // Prevent compressing hidden and 
                // already compressed files.
                if ((File.GetAttributes(fi.FullName)
                    & FileAttributes.Hidden)
                    != FileAttributes.Hidden & fi.Extension != ".gz")
                {
                    // Create the compressed file.
                    using (FileStream outFile =
                                File.Create(fi.FullName + ".gz"))
                    {
                        using (GZipStream Compress =
                            new GZipStream(outFile,
                            CompressionMode.Compress))
                        {
                            // Copy the source file into 
                            // the compression stream.
                            inFile.CopyTo(Compress);

                            Console.WriteLine("Compressed {0} from {1} to {2} bytes.",
                                fi.Name, fi.Length.ToString(), outFile.Length.ToString());
                        }
                    }
                }
            }
        }

        public static void Decompress(FileInfo fi)
        {
            // Get the stream of the source file.
            using (FileStream inFile = fi.OpenRead())
            {
                // Get original file extension, for example
                // "doc" from report.doc.gz.
                string curFile = fi.FullName;
                string origName = curFile.Remove(curFile.Length -
                        fi.Extension.Length);

                //Create the decompressed file.
                using (FileStream outFile = File.Create(origName))
                {
                    using (GZipStream Decompress = new GZipStream(inFile,
                            CompressionMode.Decompress))
                    {
                        // Copy the decompression stream 
                        // into the output file.
                        Decompress.CopyTo(outFile);
                        
                        Console.WriteLine("Decompressed: {0} from {1} to {2} bytes.", fi.Name, fi.Length, outFile.Length);

                    }
                }
            }
        }

    }
}
...
Рейтинг: 0 / 0
Как повысить производительность параллельной обработки
    #38328983
Arm79
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
попробуйте писать на разные диски
...
Рейтинг: 0 / 0
Как повысить производительность параллельной обработки
    #38329077
netivan
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
Testor1,

диск тормозит. Попробуйте читать и писать последовательно , а саму архивацию делать " в памяти
...
Рейтинг: 0 / 0
Как повысить производительность параллельной обработки
    #38329248
Testor1
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
netivanTestor1,

диск тормозит. Попробуйте читать и писать последовательно , а саму архивацию делать " в памяти

Может попробывать асинхронное чтение и запись ?

А как делать декомпрессию в памяти ? Кусками?
...
Рейтинг: 0 / 0
4 сообщений из 4, страница 1 из 1
Форумы / WinForms, .Net Framework [игнор отключен] [закрыт для гостей] / Как повысить производительность параллельной обработки
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]