Data compression method and device and electronic equipment
By dynamically selecting a compression algorithm based on a preset probability distribution during data compression, the problem that the compression algorithm in the existing technology cannot be adjusted is solved, the flexibility and performance of data compression are improved, and the risk of data leakage is reduced.
Patent Information
- Application Number
- CN202310394053.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2023-04-12
- Publication Date
- 2024-10-18
AI Technical Summary
Existing data compression methods are insufficient in flexibility and cannot dynamically adjust the compression algorithm, resulting in reduced system performance and increased risk of data leakage when memory is insufficient.
After obtaining the data to be compressed, the target compression algorithm is selected from multiple preset compression algorithms based on a preset probability distribution, and the compression algorithm is dynamically changed to improve flexibility and is suitable for different data types and storage conditions.
It improves the flexibility and performance of data compression, reduces the frequency of hard disk read and write, extends the life of the hard disk, reduces the risk of data leakage, and improves memory utilization.
Smart Images

Figure CN118796786A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer technology, and more specifically, to a data compression method, device and electronic device. Background Art
[0002] With the rapid development of technologies such as big data, cloud computing, artificial intelligence, and machine learning, the processing of high-concurrency massive data has put forward higher requirements on existing memory. In related methods, the content in the memory can be compressed through a preset compression algorithm so that the operating system can run with less physical memory, thereby improving the response speed and performance of the system. However, related compression methods still have the problem of low flexibility. Summary of the invention
[0003] In view of the above problems, the present application proposes a data compression method, device and electronic device to improve the above problems.
[0004] In a first aspect, the present application provides a data compression method, the method comprising: obtaining data to be compressed; selecting a target compression algorithm from a plurality of preset compression algorithms based on a preset probability distribution, wherein the preset probability distribution represents the probability that each of the plurality of preset compression algorithms is the target compression algorithm when there is data to be compressed; and compressing the data to be compressed based on the target compression algorithm.
[0005] In a second aspect, the present application provides a data compression device, which includes: a data acquisition unit for acquiring data to be compressed, used to acquire data to be compressed; a target compression algorithm acquisition unit, used to select a target compression algorithm from multiple preset compression algorithms based on a preset probability distribution, wherein the preset probability distribution represents the probability that each of the multiple preset compression algorithms is the target compression algorithm when there is data to be compressed; and a compression unit, used to compress the data to be compressed based on the target compression algorithm.
[0006] In a third aspect, the present application provides an electronic device comprising one or more processors and a memory; one or more programs are stored in the memory and configured to be executed by the one or more processors, and the one or more programs are configured to execute the above-mentioned method.
[0007] In a fourth aspect, the present application provides a computer-readable storage medium, in which program code is stored, wherein the above method is executed when the program code is run.
[0008] The present application provides a data compression method, device, electronic device, and storage medium. After obtaining the data to be compressed, a target compression algorithm is selected from multiple preset compression algorithms based on a preset probability distribution that represents the probability that each of the multiple preset compression algorithms is the target compression algorithm when the data to be compressed exists, and the data to be compressed is compressed based on the target compression algorithm. Through the above-mentioned method, after obtaining the data to be compressed, a target compression algorithm can be selected from multiple preset compression algorithms based on the preset probability distribution, and the data to be compressed can be compressed based on the target compression algorithm, so that each time the data to be compressed needs to be compressed, the corresponding target compression algorithm can be dynamically changed, thereby improving the flexibility of compression. BRIEF DESCRIPTION OF THE DRAWINGS
[0009] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings required for use in the description of the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For those skilled in the art, other drawings can be obtained based on these drawings without creative work.
[0010] Figure 1 A flow chart of a data compression method proposed in an embodiment of the present application is shown;
[0011] Figure 2 A flow chart of a data compression method proposed in another embodiment of the present application is shown;
[0012] Figure 3 A flowchart of a data compression method proposed in another embodiment of the present application is shown;
[0013] Figure 4 A flow chart of a data compression method proposed in another embodiment of the present application is shown;
[0014] Figure 5 A structural block diagram of a data compression device proposed in an embodiment of the present application is shown;
[0015] Figure 6 A structural block diagram of an electronic device proposed in this application is shown;
[0016] Figure 7 It is a storage unit of an embodiment of the present application for storing or carrying a program code for implementing a data compression method according to an embodiment of the present application. DETAILED DESCRIPTION
[0017] The following will be combined with the drawings in the embodiments of the present application to clearly and completely describe the technical solutions in the embodiments of the present application. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.
[0018] With the continuous advancement of science and technology and the advent of the big data era, data compression has become a hot topic in research. In related methods, the content in the memory can be compressed through a preset compression algorithm so that the operating system can run with less physical memory, thereby improving the system's response speed and performance.
[0019] The inventors found in the relevant research that the relevant compression methods still have the problem of low flexibility. For example, ZRAM can be configured with different compression algorithms to compress data, but once the configuration scheme is determined, it cannot be changed unless it is reconfigured. Among them, ZRAM can refer to a technology of the Linux kernel that achieves memory optimization by providing virtual memory compression. ZRAM can improve performance by paging on compressed block devices in RAM (Random Access Memory) until the swap space on the hard disk must be used to avoid paging on the disk.
[0020] Therefore, the inventors have proposed a data compression method, device, and electronic device in the present application. After obtaining the data to be compressed, a target compression algorithm is selected from multiple preset compression algorithms based on a preset probability distribution that characterizes the probability that each of the multiple preset compression algorithms is the target compression algorithm when the data to be compressed exists, and the data to be compressed is compressed based on the target compression algorithm. Through the above-mentioned method, after obtaining the data to be compressed, a target compression algorithm can be selected from multiple preset compression algorithms based on the preset probability distribution, and the data to be compressed is compressed based on the target compression algorithm, so that each time the data to be compressed needs to be compressed, the corresponding target compression algorithm can be dynamically changed, thereby improving the flexibility of compression.
[0021] See also Figure 1 , an embodiment of the present application provides a data compression method, the method comprising:
[0022] S110: Obtain data to be compressed.
[0023] The data to be compressed may refer to data that needs to be compressed to avoid insufficient data storage space. For example, in memory compression, a page of an application may be used as the data to be compressed.
[0024] As a method, the data to be compressed can be obtained when the compression task is triggered.
[0025] In the embodiment of the present application, there are multiple ways to trigger the compression task.
[0026] As a method, the electronic device can detect the remaining capacity of the data storage space (such as memory, etc.), and when the remaining capacity cannot meet the needs of the application, the compression task can be triggered. For example, when the electronic device detects that the remaining memory space is 100MB, and the application may need to occupy 150MB of memory space, the compression task can be triggered.
[0027] As another way, the user can also trigger the compression task according to his own needs through interaction with the electronic device (such as voice control, screen sliding, screen tapping, etc.).
[0028] The electronic device may refer to a user terminal device, such as a mobile phone, a computer, etc., or an IoT device. It should be noted that the data compression algorithm provided in the present application may also be applied to a virtual machine, a data center, etc. of a virtualization platform.
[0029] S120: Selecting a target compression algorithm from a plurality of preset compression algorithms based on a preset probability distribution, wherein the preset probability distribution represents the probability that each of the plurality of preset compression algorithms is the target compression algorithm when there is data to be compressed.
[0030] The plurality of preset compression algorithms may include at least two of LZ77, LZ78, DEFLATE, GZIP, LZW, etc. The preset probability distribution may refer to the probability of each preset compression algorithm being determined as the target compression algorithm obtained through long-term experimental statistics.
[0031] As a method, an algorithm selector may be provided in the operating system of the electronic device. After the electronic device obtains the data to be compressed, the algorithm selector may select a target compression algorithm from a plurality of preset compression algorithms based on a preset probability distribution.
[0032] Exemplarily, multiple preset compression algorithms may include algorithm A, algorithm B, and algorithm C, and the preset probability distribution may be: {algorithm A: 60%, algorithm B: 30%, algorithm C: 10%}, which may indicate that at the current moment, algorithm A has a 60% probability of being determined by the algorithm selector as the target compression algorithm for the data to be compressed, at the current moment, algorithm B has a 30% probability of being determined by the algorithm selector as the target compression algorithm for the data to be compressed, and at the current moment, algorithm C has a 10% probability of being determined by the algorithm selector as the target compression algorithm for the data to be compressed.
[0033] S130: Compress the data to be compressed based on the target compression algorithm.
[0034] As one approach, the data to be compressed may be compressed based on the target compression algorithm determined in step S120.
[0035] In the embodiment of the present application, compressing the data to be compressed by a target compression algorithm can reduce the frequency of page replacement to the disk and the operating system's read and write operations on the hard disk, thereby improving the operating system's response speed and performance, and extending the hard disk life and reducing the risk of hard disk failure. And by compressing the data to be compressed by a variable target compression algorithm, the operating memory utilization rate can be improved, so that the system can still operate normally when the memory is insufficient. At the same time, because it is not easy for malware to steal and use the compressed data, compressing the data can also reduce the risk of data leakage.
[0036] Optionally, ZRAM can be used as a storage medium for compression results, thereby reducing the system's reliance on physical memory and improving the system's requirements on hardware configuration.
[0037] The present embodiment provides a data compression method, after obtaining the data to be compressed, based on a preset probability distribution that characterizes the probability that each of the multiple preset compression algorithms is the target compression algorithm when the data to be compressed exists, a target compression algorithm is selected from multiple preset compression algorithms, and the data to be compressed is compressed based on the target compression algorithm. Through the above manner, after obtaining the data to be compressed, a target compression algorithm can be selected from multiple preset compression algorithms based on the preset probability distribution, and the data to be compressed can be compressed based on the target compression algorithm, so that each time the data to be compressed needs to be compressed, the corresponding target compression algorithm can be dynamically changed, thereby improving the flexibility of compression.
[0038] See also Figure 2 , an embodiment of the present application provides a data compression method, the method comprising:
[0039] S210: Obtain data to be compressed.
[0040] S220: Obtain the data type of the data to be compressed.
[0041] Among them, data types can be divided into images, strings, texts, etc.
[0042] As a method, the data types of all data in the current data to be compressed may be counted to obtain statistical results, and the data type with the highest proportion in the statistical results may be used as the data type of the data to be compressed.
[0043] As another method, the data types of all data in the current data to be compressed can be counted to obtain statistical results, and the top N data types with the highest proportion in the statistical results are used as the data types of the data to be compressed. Wherein, N is a positive integer, and N is less than the number of preset compression algorithms.
[0044] S230: Determine a target probability distribution from the plurality of preset probability distributions based on the data type, wherein the target probability distribution is used to maximize the probability that a compression algorithm applicable to the data type is the target compression algorithm.
[0045] In the embodiment of the present application, there may be multiple preset probability distributions. Exemplarily, when the multiple preset compression algorithms include algorithm A, algorithm B, and algorithm C, the first preset probability distribution may be: {algorithm A: 60%, algorithm B: 30%, algorithm C: 10%}, the second preset probability distribution may be: {algorithm A: 40%, algorithm B: 50%, algorithm C: 10%}, the third preset probability distribution may be: {algorithm A: 10%, algorithm B: 60%, algorithm C: 30%}, etc.
[0046] As one approach, the electronic device may determine a target probability distribution from a plurality of preset probability distributions based on the data type.
[0047] Exemplarily, multiple preset compression algorithms may include algorithm A for image compression, algorithm B for string compression, and algorithm C for text compression. The first preset probability distribution may be: {algorithm A: 60%, algorithm B: 30%, algorithm C: 10%}, the second preset probability distribution may be: {algorithm A: 40%, algorithm B: 50%, algorithm C: 10%}, and the third preset probability distribution may be: {algorithm A: 10%, algorithm B: 60%, algorithm C: 30%}. When it is determined based on step S130 that the data type of the data to be compressed is an image, the preset probability distribution with the largest probability of algorithm A, that is, the first probability distribution, may be selected as the target probability distribution. When it is determined based on step S130 that the data type of the data to be compressed is an image and a string, the preset probability distribution with the larger probability of algorithm A and algorithm B, that is, the second probability distribution, may be selected as the target probability distribution.
[0048] S240: Selecting a target compression algorithm from a plurality of preset compression algorithms based on the target probability distribution.
[0049] As one approach, after obtaining the target probability distribution based on step S230, a target compression algorithm may be selected from a plurality of preset compression algorithms based on the target probability distribution in an algorithm selector.
[0050] S250: Compress the data to be compressed based on the target compression algorithm.
[0051] The data compression method provided by the present embodiment enables, through the above-mentioned method, after obtaining the data to be compressed, to select a target compression algorithm from a plurality of preset compression algorithms based on a preset probability distribution, and to compress the data to be compressed based on the target compression algorithm, so that each time the data to be compressed needs to be compressed, the corresponding target compression algorithm can be dynamically changed, thereby improving the flexibility of compression. Moreover, in the present embodiment, when there are multiple preset probability distributions, the target probability distribution can be determined based on the data type, so that when compressing the data to be compressed, the probability that the compression algorithm applicable to the data type of the data to be compressed is determined as the target compression algorithm can be increased, so that while making the compression flexible, the compression effect can be taken into account, and the operating pressure of the operating system can be reduced.
[0052] See also Figure 3 , an embodiment of the present application provides a data compression method, the method comprising:
[0053] S310: Obtain data to be compressed.
[0054] S320: Obtain the generation speed of the data to be compressed.
[0055] The generation speed of the data to be compressed may refer to the amount of data to be compressed that can be generated in a unit time.
[0056] As a method, the initial data amount of the data to be compressed can be obtained first, and then the current data amount of the data to be compressed can be obtained after a preset time. The generation speed of the data to be compressed can be obtained through the formula: (current data amount-initial data amount) / preset time.
[0057] S330: Determine a target probability distribution from the plurality of preset probability distributions based on the generation speed.
[0058] As a method, if the generation speed of the data to be compressed is greater than or equal to the preset speed, the target probability distribution can be determined as the probability distribution with the highest probability that the algorithm with the fastest compression speed among multiple preset compression algorithms is the target compression algorithm, that is, the target probability distribution is determined from multiple preset probability distributions, and the target probability distribution makes the algorithm with the fastest compression speed have the highest probability of being selected as the target compression algorithm.
[0059] The compression speed may refer to the amount of data that can be compressed by the reference compression algorithm per unit time.
[0060] Exemplarily, the plurality of preset compression algorithms may include algorithm A, algorithm B and algorithm C, algorithm A has the highest compression speed, algorithm B is second, and algorithm C has the lowest compression speed. The first preset probability distribution may be: {algorithm A: 60%, algorithm B: 30%, algorithm C: 10%}, and the second preset probability distribution may be: {algorithm A: 40%, algorithm B: 50%, algorithm C:
[0061] 10%}, and the third preset probability distribution can be: {algorithm A: 10%, algorithm B: 60%, algorithm C: 30%}. When the generation speed of the data to be compressed is greater than or equal to the preset speed, the preset probability distribution with the largest probability of algorithm A, that is, the first probability distribution, can be selected as the target probability distribution.
[0062] Optionally, before compressing the data to be compressed for the first time, the initial data volume of the test data can be obtained, and the test data can be compressed respectively by multiple preset compression algorithms, and the compression time corresponding to each preset compression algorithm can be recorded. The compression speed corresponding to each preset compression algorithm can be obtained by the formula: initial data volume / compression time.
[0063] Optionally, the preset speed may be determined based on multiple tests.
[0064] As a method, if the generation speed of the data to be compressed is lower than the preset speed, a preset probability distribution may be randomly selected from a plurality of preset probability distributions, and the selected preset probability distribution may be used as the target probability distribution.
[0065] Exemplarily, the plurality of preset compression algorithms may include algorithm A, algorithm B and algorithm C, wherein algorithm A has the highest compression speed, algorithm B has the second highest compression speed, and algorithm C has the lowest compression speed. The first preset probability distribution may be: {algorithm A: 60%, algorithm B: 30%, algorithm C: 10%}, the second preset probability distribution may be: {algorithm A: 40%, algorithm B: 50%, algorithm C: 10%}, and the third preset probability distribution may be: {algorithm A: 10%, algorithm B: 60%, algorithm C: 30%}. When the generation speed of the data to be compressed is greater than or equal to the preset speed, one probability distribution may be randomly selected from the three preset probability distributions as the target probability distribution.
[0066] As a method, if the generation speed of the data to be compressed is lower than the preset speed, a preset probability distribution can be selected from multiple preset probability distributions, and the selected preset probability distribution can be used as the target probability distribution, wherein the selected preset probability distribution can make the algorithm with the highest compression rate have the highest probability of being selected as the target compression algorithm.
[0067] The compression rate may refer to the ratio between the amount of data compressed by the compression algorithm and the original amount of data, or the compression rate may refer to the ratio between the difference between the amount of data compressed by the compression algorithm and the original amount of data and the original amount of data. The compression speed may refer to the amount of data that can be compressed by the compression algorithm per unit time.
[0068] Optionally, before compressing the data to be compressed for the first time, the initial data size of the test data can be obtained, and the test data can be compressed respectively by multiple preset compression algorithms, and the compression time and compressed data size corresponding to each preset compression algorithm can be recorded, and the compression rate corresponding to each preset compression algorithm can be obtained by the formula: compressed data size / initial data size or the formula: (initial data size-compressed data size) / initial data size.
[0069] Exemplarily, the plurality of preset compression algorithms may include algorithm A, algorithm B and algorithm C, algorithm A has the highest compression speed, algorithm B is second, and algorithm C has the lowest compression speed; algorithm C has the highest compression rate, algorithm B is second, and algorithm A has the lowest compression rate. The first preset probability distribution may be: {algorithm A: 60%, algorithm B: 30%, algorithm C: 10%}, the second preset probability distribution may be: {algorithm A: 40%, algorithm B: 50%, algorithm C: 10%}, and the third preset probability distribution may be: {algorithm A: 10%, algorithm B: 30%, algorithm C: 60%}. When the generation speed of the data to be compressed is less than the preset speed, the preset probability distribution with the highest probability of algorithm C, that is, the third probability distribution, may be selected as the target probability distribution.
[0070] In an embodiment of the present application, by randomly selecting a preset probability distribution from multiple preset probability distributions as the target probability distribution when the generation speed of the data to be compressed is less than the preset speed, the flexibility of compression can be improved; and when the generation speed of the data to be compressed is less than the preset speed, randomly selecting from multiple preset probability distributions the preset probability distribution with the highest probability that the algorithm with the highest compression rate is selected as the target compression algorithm as the target probability distribution can save storage space.
[0071] S340: Selecting a target compression algorithm from a plurality of preset compression algorithms based on the target probability distribution.
[0072] As one approach, after obtaining the target probability distribution based on step S230, a target compression algorithm may be selected from a plurality of preset compression algorithms based on the target probability distribution in an algorithm selector.
[0073] S350: Compress the data to be compressed based on the target compression algorithm.
[0074] The data compression method provided by the present embodiment enables, through the above-mentioned method, after obtaining the data to be compressed, to select a target compression algorithm from a plurality of preset compression algorithms based on a preset probability distribution, and to compress the data to be compressed based on the target compression algorithm, so that each time the data to be compressed needs to be compressed, the corresponding target compression algorithm can be dynamically changed, thereby improving the flexibility of compression. Moreover, in the present embodiment, when there are multiple preset probability distributions, the target probability distribution can be determined based on the generation speed of the data to be compressed, so that when compressing the data to be compressed, the probability of the compression algorithm with the fastest compression speed being determined as the target compression algorithm can be increased, so that while making the compression flexible, the compression effect can be taken into account, and the operating pressure of the operating system can be reduced.
[0075] See also Figure 4 , an embodiment of the present application provides a data compression method, the method comprising:
[0076] S410: Obtain data to be compressed.
[0077] S420: Obtain the remaining capacity of a target storage area, where the target storage area is used to store the compression result of the data to be compressed.
[0078] The remaining capacity may refer to the amount of data that can be stored in the target storage area.
[0079] As one approach, the total capacity of the target storage area may be first obtained, and then the currently used capacity of the target storage area may be obtained, and the difference between the total capacity and the used capacity may be used as the remaining capacity of the target storage area.
[0080] S430: Determine a target probability distribution from the plurality of preset probability distributions based on the remaining capacity.
[0081] As a method, if the remaining capacity is less than or equal to the preset capacity, the target probability distribution may be a probability distribution that maximizes the probability of selecting the algorithm with the highest compression rate among multiple preset compression algorithms as the target compression algorithm. That is, the target probability distribution is determined from multiple preset probability distributions, and the target probability distribution makes the algorithm with the highest compression rate the most likely to be selected as the target compression algorithm.
[0082] Optionally, the preset speed may be determined based on multiple tests.
[0083] Exemplarily, the plurality of preset compression algorithms may include algorithm A, algorithm B and algorithm C, algorithm C has the highest compression rate, algorithm B is second, and algorithm A has the lowest compression rate. The first preset probability distribution may be: {algorithm A: 60%, algorithm B: 30%, algorithm C: 10%}, the second preset probability distribution may be: {algorithm A: 40%, algorithm B: 50%, algorithm C: 10%}, and the third preset probability distribution may be: {algorithm A: 10%, algorithm B: 30%, algorithm C: 60%}. When the remaining capacity of the target storage area is less than or equal to the preset capacity, the preset probability distribution with the highest probability of algorithm C, i.e., the third probability distribution, may be selected as the target probability distribution.
[0084] As a method, if the remaining capacity is greater than the preset capacity, a preset probability distribution may be randomly selected from a plurality of preset probability distributions, and the selected preset probability distribution may be used as the target probability distribution.
[0085] Exemplarily, the plurality of preset compression algorithms may include algorithm A, algorithm B and algorithm C, algorithm C has the highest compression rate, algorithm B is second, and algorithm A has the lowest compression rate. The first preset probability distribution may be: {algorithm A: 60%, algorithm B: 30%, algorithm C: 10%}, the second preset probability distribution may be: {algorithm A: 40%, algorithm B: 50%, algorithm C: 10%}, and the third preset probability distribution may be: {algorithm A: 10%, algorithm B: 30%, algorithm C: 60%}. When the remaining capacity is greater than the preset capacity, one probability distribution may be randomly selected from the three preset probability distributions as the target probability distribution.
[0086] As a method, if the remaining capacity is greater than the preset capacity, a preset probability distribution can be selected from multiple preset probability distributions, and the selected preset probability distribution can be used as the target probability distribution, wherein the selected preset probability distribution can make the algorithm with the highest compression speed be selected as the target compression algorithm with the highest probability.
[0087] Exemplarily, the plurality of preset compression algorithms may include algorithm A, algorithm B and algorithm C, algorithm A has the highest compression speed, algorithm B is second, and algorithm C has the lowest compression speed; algorithm C has the highest compression rate, algorithm B is second, and algorithm A has the lowest compression rate. The first preset probability distribution may be: {algorithm A: 60%, algorithm B: 30%, algorithm C: 10%}, the second preset probability distribution may be: {algorithm A: 40%, algorithm B: 50%, algorithm C: 10%}, and the third preset probability distribution may be: {algorithm A: 10%, algorithm B: 30%, algorithm C: 60%}. When the remaining capacity is greater than the preset capacity, the preset probability distribution with the highest probability of algorithm A, that is, the third probability distribution, may be selected as the target probability distribution.
[0088] In an embodiment of the present application, by randomly selecting a preset probability distribution from multiple preset probability distributions as the target probability distribution when the remaining capacity is greater than the preset capacity, the flexibility of compression can be improved; and when the remaining capacity is greater than the preset capacity, randomly selecting from multiple preset probability distributions the preset probability distribution with the highest probability of selecting the algorithm with the highest compression speed as the target compression algorithm as the target probability distribution can improve the compression speed and enhance the real-time performance of the operating system in processing data.
[0089] S440: Selecting a target compression algorithm from a plurality of preset compression algorithms based on the target probability distribution.
[0090] As one approach, after obtaining the target probability distribution based on step S230, a target compression algorithm may be selected from a plurality of preset compression algorithms based on the target probability distribution in an algorithm selector.
[0091] In an embodiment of the present application, multiple preset compression algorithms can be divided into multiple groups of candidate compression algorithms based on data type, compression speed, compression rate, etc.
[0092] Exemplarily, the plurality of preset compression algorithms may include algorithm A, algorithm B, algorithm C, algorithm D, algorithm E and algorithm F, wherein algorithm A is applicable to image compression and has a relatively fast compression speed, algorithm B is applicable to string compression and has a relatively high compression rate, algorithm C is applicable to text compression and has a relatively fast compression speed, algorithm D is applicable to binary data and has a relatively high compression rate, algorithm E is applicable to multiple data types and has a relatively high compression rate, and algorithm F is applicable to multiple data types and has a relatively fast compression speed. The plurality of preset compression algorithms may be divided into three groups, wherein the first group of candidate compression algorithms may be algorithm A, algorithm B and algorithm C related to data types, the second group of candidate compression algorithms may be algorithm A, algorithm C and algorithm F related to compression speed, and the third group of candidate compression algorithms may be algorithm B, algorithm D and algorithm E related to compression rate.
[0093] As another approach, a target algorithm group can be selected from multiple groups of candidate compression algorithms based on the data type of the data to be compressed, the generation speed of the data to be compressed, and the remaining capacity of the target storage area; and a target probability distribution can be selected from multiple preset probability distributions based on the data type of the data to be compressed, the generation speed of the data to be compressed, the remaining capacity of the target storage area, and the target algorithm group, so as to select a target compression algorithm from the target algorithm group based on the target probability distribution.
[0094] Exemplarily, the first group of candidate compression algorithms may be Algorithm A, Algorithm B and Algorithm C related to data types, the second group of candidate compression algorithms may be Algorithm A, Algorithm C and Algorithm F related to compression speed, and the third group of candidate compression algorithms may be Algorithm B, Algorithm D and Algorithm E related to compression ratio. When a certain data type of data to be compressed accounts for a high proportion, the generation speed of the data to be compressed is less than the preset speed, and the remaining capacity of the target storage area is greater than the preset capacity, the first group of candidate compression algorithms may be selected as the target algorithm group.
[0095] Exemplarily, the first preset probability distribution may be: {60%, 30%, 10%}, the second preset probability distribution may be: {10%, 60%, 30%}, and the third preset probability distribution may be: {30%, 10%, 60%}. When the target algorithm group is the first group of candidate compression algorithms, the target probability distribution may be obtained based on the data type of the data to be compressed and the target algorithm group, based on considerations similar to step S230. When the target algorithm group is the second group of candidate compression algorithms, the target probability distribution may be obtained based on considerations similar to step S330, so that the algorithm with the highest compression speed in the target algorithm group is most likely to be selected.
[0096] In the embodiment of the present application, by introducing probability distribution and algorithm selector, it is possible to dynamically select between different compression algorithms, thereby achieving the optimal compression effect and speed balance. In addition, since the compression algorithm is deformable, the reliability and stability of the system can be improved, the probability of system crash and freeze can be reduced, and user satisfaction can be improved.
[0097] S450: Compress the data to be compressed based on the target compression algorithm.
[0098] The data compression method provided by the present embodiment enables, through the above-mentioned method, after obtaining the data to be compressed, to select a target compression algorithm from a plurality of preset compression algorithms based on a preset probability distribution, and to compress the data to be compressed based on the target compression algorithm, so that each time the data to be compressed needs to be compressed, the corresponding target compression algorithm can be dynamically changed, thereby improving the flexibility of compression. Moreover, in the present embodiment, when there are multiple preset probability distributions, the target probability distribution can be determined based on the remaining capacity of the target storage area, so that when compressing the data to be compressed, the probability of the compression algorithm with the highest compression rate being determined as the target compression algorithm can be increased, so that while making the compression flexible, the compression effect can be taken into account, and the operating pressure of the operating system can be reduced.
[0099] See also Figure 5 The present application provides a data compression device 600, the device 600 comprising:
[0100] The to-be-compressed data acquisition unit 610 is used to acquire the to-be-compressed data.
[0101] A target compression algorithm acquisition unit 620 is used to select a target compression algorithm from a plurality of preset compression algorithms based on a preset probability distribution, wherein the preset probability distribution represents the probability that each of the plurality of preset compression algorithms is the target compression algorithm when there is data to be compressed;
[0102] The compression unit 630 is used to compress the data to be compressed based on the target compression algorithm.
[0103] As a method, there are multiple preset probability distributions, and the target compression algorithm acquisition unit 620 is specifically used to obtain the data type of the data to be compressed; based on the data type, a target probability distribution is determined from the multiple preset probability distributions, and the target probability distribution is used to maximize the probability that the compression algorithm applicable to the data type is the target compression algorithm; based on the target probability distribution, a target compression algorithm is selected from multiple preset compression algorithms.
[0104] As another way, there are multiple preset probability distributions, and the target compression algorithm acquisition unit 620 is specifically used to obtain the generation speed of the data to be compressed; determine the target probability distribution from the multiple preset probability distributions based on the generation speed; and select the target compression algorithm from multiple preset compression algorithms based on the target probability distribution.
[0105] Optionally, the target compression algorithm acquisition unit 620 is specifically used to, if the generation speed is greater than or equal to a preset speed, the target probability distribution is a probability distribution with the highest probability that the algorithm with the fastest compression speed among the multiple preset compression algorithms is the target compression algorithm.
[0106] As another way, there are multiple preset probability distributions, and the target compression algorithm acquisition unit 620 is specifically used to obtain the remaining capacity of the target storage area, and the target storage area is used to store the compression result of the data to be compressed; determine the target probability distribution from the multiple preset probability distributions based on the remaining capacity; and select the target compression algorithm from multiple preset compression algorithms based on the target probability distribution.
[0107] Optionally, the target compression algorithm acquisition unit 620 is specifically used to, if the remaining capacity is less than or equal to the preset capacity, the target probability distribution is a probability distribution with the highest probability of using the algorithm with the highest compression rate among the multiple preset compression algorithms as the target compression algorithm.
[0108] Optionally, the multiple preset compression algorithms include at least two of LZ77, LZ78, DEFLATE, and GZIP.
[0109] The following will be combined Figure 6 An electronic device provided by the present application is described.
[0110] See also Figure 6 Based on the above data compression method and device, the embodiment of the present application also provides another electronic device 100 that can execute the above data compression method. The electronic device 100 includes a processor 102 and a memory 104, wherein the memory 104 stores a program that can execute the content of the above embodiment, and the processor 102 can execute the program stored in the memory 104.
[0111] Among them, the processor 102 may include one or more processing cores. The processor 102 uses various interfaces and lines to connect various parts within the entire electronic device 100, and executes various functions and processes data of the electronic device 100 by running or executing instructions, programs, code sets or instruction sets stored in the memory 104, and calling data stored in the memory 104. Optionally, the processor 102 can be implemented in at least one hardware form of a network processor (Neural network Processing Unit, NPU), a digital signal processing (Digital Signal Processing, DSP), a field programmable gate array (Field-Programmable Gate Array, FPGA), and a programmable logic array (Programmable Logic Array, PLA). The processor 102 can integrate one or a combination of a central processing unit (Central Processing Unit, CPU), a graphics processor (Graphics Processing Unit, GPU), a network processor (Neural network Processing Unit, NPU) and a modem. Among them, the CPU mainly processes the operating system, user interface and application programs; the GPU is responsible for rendering and drawing display content; the NPU is responsible for processing multimedia data such as video and images; and the modem is used to process wireless communications. It is understandable that the above-mentioned modem may not be integrated into the processor 102, but may be implemented separately through a communication chip.
[0112] The memory 104 may include a random access memory (RAM), and may also include a read-only memory (RAM) and a double data rate synchronous dynamic random access memory (DDR). The memory 104 may be used to store instructions, programs, codes, code sets or instruction sets. The memory 104 may include a program storage area and a data storage area, wherein the program storage area may store instructions for implementing an operating system, instructions for implementing at least one function (such as a touch function, a sound playback function, an image playback function, etc.), instructions for implementing the following various method embodiments, etc. The data storage area may also store data (such as a phone book, audio and video data, chat record data) created by the terminal 100 during use.
[0113] Please refer to Figure 7 , which shows a structural block diagram of a computer-readable storage medium provided in an embodiment of the present application. The computer-readable storage medium 800 stores program codes, which can be called by a processor to execute the method described in the above method embodiment.
[0114] The computer readable storage medium 800 may be an electronic memory such as a flash memory, an EEPROM (electrically erasable programmable read-only memory), an EPROM, a hard disk, or a ROM. Optionally, the computer readable storage medium 800 includes a non-transitory computer-readable storage medium. The computer readable storage medium 800 has storage space for program code 810 that performs any method step in the above method. These program codes can be read from or written to one or more computer program products. The program code 810 can be compressed, for example, in an appropriate form.
[0115] In summary, the present application provides a data compression method, device, and electronic device. After obtaining the data to be compressed, a target compression algorithm is selected from multiple preset compression algorithms based on a preset probability distribution that characterizes the probability that each of the multiple preset compression algorithms is the target compression algorithm when the data to be compressed exists, and the data to be compressed is compressed based on the target compression algorithm. Through the above manner, after obtaining the data to be compressed, a target compression algorithm can be selected from multiple preset compression algorithms based on the preset probability distribution, and the data to be compressed is compressed based on the target compression algorithm, so that each time the data to be compressed needs to be compressed, the corresponding target compression algorithm can be dynamically changed, thereby improving the flexibility of compression.
[0116] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present application, rather than to limit it. Although the present application has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of the present application.
Claims
1. A data compression method, characterized in that: The method comprises: Get the data to be compressed; Selecting a target compression algorithm from a plurality of preset compression algorithms based on a preset probability distribution, wherein the preset probability distribution represents the probability that each of the plurality of preset compression algorithms is the target compression algorithm when there is data to be compressed; The data to be compressed is compressed based on the target compression algorithm.
2. The method according to claim 1, characterized in that There are multiple preset probability distributions, and selecting a target compression algorithm from multiple preset compression algorithms based on the preset probability distribution includes: Acquire the data type of the data to be compressed; Determine a target probability distribution from the plurality of preset probability distributions based on the data type, the target probability distribution being used to maximize the probability that the compression algorithm applicable to the data type is the target compression algorithm; A target compression algorithm is selected from a plurality of preset compression algorithms based on the target probability distribution.
3. The method according to claim 1, characterized in that: There are multiple preset probability distributions, and selecting a target compression algorithm from multiple preset compression algorithms based on the preset probability distribution includes: Acquire the generation speed of the data to be compressed; determining a target probability distribution from the plurality of preset probability distributions based on the generation speed; A target compression algorithm is selected from a plurality of preset compression algorithms based on the target probability distribution.
4. The method according to claim 3, characterized in that The determining a target probability distribution from the plurality of preset probability distributions based on the generation speed comprises: If the generation speed is greater than or equal to the preset speed, the target probability distribution is a probability distribution in which the algorithm with the fastest compression speed among the plurality of preset compression algorithms has the highest probability of being the target compression algorithm.
5. The method according to claim 1, characterized in that There are multiple preset probability distributions, and selecting a target compression algorithm from multiple preset compression algorithms based on the preset probability distribution includes: Acquire the remaining capacity of a target storage area, where the target storage area is used to store the compression result of the data to be compressed; Determining a target probability distribution from the plurality of preset probability distributions based on the remaining capacity; A target compression algorithm is selected from a plurality of preset compression algorithms based on the target probability distribution.
6. The method according to claim 5, characterized in that The determining a target probability distribution from the plurality of preset probability distributions based on the remaining capacity includes: If the remaining capacity is less than or equal to the preset capacity, the target probability distribution is a probability distribution with the highest probability of using the algorithm with the highest compression rate among the multiple preset compression algorithms as the target compression algorithm.
7. The method according to any one of claims 1 to 6, characterized in that: The plurality of preset compression algorithms include at least two of LZ77, LZ78, DEFLATE, and GZIP.
8. A data compression device, characterized in that: The device comprises: A to-be-compressed data acquisition unit, used for acquiring the to-be-compressed data; a target compression algorithm acquisition unit, configured to select a target compression algorithm from a plurality of preset compression algorithms based on a preset probability distribution, wherein the preset probability distribution represents a probability that each of the plurality of preset compression algorithms is the target compression algorithm when there is data to be compressed; A compression unit is used to compress the data to be compressed based on the target compression algorithm.
9. An electronic device, characterized in that: comprising one or more processors and memory; One or more programs are stored in the memory and configured to be executed by the one or more processors, and the one or more programs are configured to execute the method according to any one of claims 1 to 7.
10. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores program code, wherein when the program code is run, the method according to any one of claims 1 to 7 is executed.
Citation Information
Patent Citations
Content-based dynamic hybrid data compression
CN115391298A
Dynamic data compression selection
US9385749B1
