[Programming Pearl] Chapter 10 space saving

Source: Internet
Author: User

I. Overview

1) benefits of space saving

Smaller programs make loading faster; easier to read into high-speed cache; less data to be operated, thus reducing the operation time.

2) space saving methods

Mathematical functions Replace the data to be stored. A simple example is a [I] = I. If you want to read the array element I, you only need to use the function f (I) = I to get the element value, instead of storing all the data in the array.

Change the data structure. If a number ranges from 0 to 200, there is no need to use 32-bit int-type storage. You only need to use 8-bit unsigned char-type storage.

The linked list replaces the sparse two-dimensional array. Although pointers also require space storage, if a two-dimensional array stores a small amount of data, consider balancing whether to use a linked list instead of a sparse two-dimensional array. (Arrays represent columns, and linked lists represent rows. Instead of a single-chain table)

3) technologies that reduce the storage space for data required by programs

1> do not store, re-calculate.

A typical time-to-space change policy is to judge the prime number: Use a function to determine whether a prime number is compared to a query prime number table. This is an example of Time-to-space change.

In network transmission, time is often very important. Use local cache to reduce the amount of data to be transferred.

2> sparse data structure.

Using pointers to share large objects eliminates the overhead of storing multiple copies of the same object

Other methods, you know ...... Hey


3> data compression.

Replace the 32-bit int with an 8-bit char.

Use the function to place two decimal numbers in one byte. Encryption: c = 10 * A + B; decryption: A = C/10; B = C % 10;

Note: The overhead of the/and % operators is large, that is, the runtime occupies a large amount of memory. If this method is used, it is equivalent to using a small memory in exchange for the disk storage space of the storage bytes.

4> allocation policy.

Dynamic Allocation replaces static allocation. That is, space is allocated only when necessary.

5> garbage collection.

4) code space technology


Ii. Exercise

6) encryption: c = 10 * A + B; decryption: A = C/10; B = C % 10;

Optimization Strategy:

Encryption: c = A <4 | B; decryption: A = C> 4; B = C & 0xf;

C is 8 bytes. Generally, only the four digits in the lower position are used. During encryption, A is increased to four bits. If it is the same as B, B is placed at the bottom of the byte. During decryption, the last four bits of B are removed to obtain. Convert the number of BITs C and the four lower bits to 1111 to B.

10) large storage space is occupied by the high sampling rate of original audio files (WAV. Reduce the sampling rate and compress MP3 audio files without listening distortion.

Original image files (raw, BMP) are lossless compressed into GIF format. Lossy compression into JPEG

Original video file (AVI: audio video interleaved )). Compress to (3GP, mpg)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.