Advanced Precision Algorithm Series
Chapter 1 Simple Implementation
Chapter 2 Compression Optimization
Chapter 3 Binary Optimization (this chapter)
Article directory
foreword
The previous chapter "C/C++ High Precision (Addition, Subtraction, Multiplication and Division) Algorithm Pressure Bit Optimization" realized optimized high-precision calculations. Each element of an integer array using int32 can store 9 decimal numbers. I want to further optimize The calculation speed can change the data storage method and use binary storage numbers. The int32 array is still used to store numbers in binary, which not only has high computing efficiency, but also achieves the highest space utilization.
1. Basic principles
1. Storage method
Storage binary order is stored from low to high
2. Calculation method
The calculation method is basically the same as the decimal storage calculation method. The calculation method of int8 is given below, int16, int32 and so on are essentially the same.
2. Key realization
1. Integer to high-precision array (binary)
It can be realized by bit operation (element type int32 as an example):
/// <summary>
/// 通过无符号整型初始化
/// </summary>
/// <param name="a">[in]高精度数组</param>
/// <param name="value">[in]整型值</param>
static void loadInt(int* a, uint64_t value) {
a[1] = (uint32_t)value;
a[2] = value >> (sizeof(int) * 8);
a[0] = a[2] ? 2 : 1;
}
2. String to high-precision array (binary)
A method is provided here, which needs to realize high-precision addition and multiplication first.
(1) Initialize the high-precision array value to 0
(2) Obtain the numbers in the string one by one
(3) Multiply the high-precision array to equal 10
(4) Add the acquired number to the high-precision array (refer to the above for integer to high-precision array Section)
(5) The string has not been read and returns to (2)
3. Convert high-precision array (binary) to string
A method is provided here, which requires the implementation of the previous chapter as an auxiliary "C/C++ High Precision (Addition, Subtraction, Multiplication and Division) Algorithm Compression Optimization"
(1) Initialize the 9-bit high progress array value to 0
(2) Acquire high precision one by one The element of the array (binary)
(3) multiplied by the 9-bit high progress array is equal to 2^32 (binary array element type int32 is an example)
(4) The acquired element is added to the 9-bit high progress array
(5) The element is unread After taking it back to (2)
(6) Press the 9-digit high progress array to convert the string
3. Complete code
Because the interface and usage are exactly the same as Chapter 1 "C/C++ High Precision (Addition, Subtraction, Multiplication and Division) Algorithm Simple Implementation" , so only the complete code is provided here, please refer to Chapter 1 for usage examples . High-precision algorithm based on
int32 array binary storage :
https://download.csdn.net/download/u013113678/87720242
4. Performance comparison
Test platform: Windows 11
Test equipment: i7 8750h
Test method: Take the average value of 5 tests
Table 1, test cases
test case | describe |
---|---|
1 | Integer range digital calculation 500000 times |
2 | Long numbers and integer range numbers are calculated 500,000 times |
3 | Long numbers and long numbers calculated 500,000 times |
Write a program based on the above use cases for testing, the test results are shown in the following table Table
2, test results
calculate | test case | Compressing 9-bit optimization (previous chapter) time-consuming | Binary optimized int32 (this chapter) time consuming |
---|---|---|---|
addition | test case 1 | 0.002620s | 0.0024862s |
addition | test case 2 | 0.005711s | 0.0034712s |
addition | Test case 3 | 0.005384s | 0.003857s |
accumulate | test case 1 | 0.002536s | 0.0027246s |
accumulate | test case 2 | 0.002592s | 0.0029876s |
accumulate | Test case 3 | 0.006474s | 0.0043758s |
subtraction | test case 1 | 0.002078s | 0.0022704s |
subtraction | test case 2 | 0.004939s | 0.0032914s |
subtraction | Test case 3 | 0.004929s | 0.0041246s |
accumulative | test case 1 | 0.002034s | 0.0020808s |
accumulative | test case 2 | 0.001942s | 0.0023542s |
accumulative | Test case 3 | 0.004282s | 0.0044144s |
multiplication | test case 1 | 0.004751s | 0.0038996s |
multiplication | test case 2 | 0.028358s | 0.0117986s |
multiplication | Test case 3 | 0.064259s | 0.0185958s |
multiplication | Test case 1 only calculates 1000 times | 0.000137s | 0.000062s |
multiplication | Test case 2 only calculates 1000 times | 0.000187s | 0.0000816s |
multiplication | Test case 3 only calculates 1000 times | 0.081988s | 0.0292832s |
division | test case 1 | 0.024763s | 0.0196498s |
division | test case 2 | 0.516090s | 0.3556564s |
division | Test case 3 | 0.073812s | 0.1716874s |
cumulative division | Test case 1 only calculates 1000 times | 0.035722s | 0.0009416s |
cumulative division | Test case 2 only calculates 1000 times | 0.060936s | 0.0131722s |
cumulative division | Test case 3 only calculates 500 times | 25.126072s | 2.6210544s |
Classify the data in the above table into the same type and take the mean value to calculate the improvement speed, as shown in the figure below, which is for reference only.
Figure 1. Speed improvement
Summarize
The above is what I want to talk about today. Binary storage optimization has improved speed compared with 9-bit optimization, and the storage method is the same as integer, which makes good use of space. Compared with the compressed 9-bit algorithm, the binary algorithm has a greater improvement in multiplication and division, especially the improvement of long data operations is more obvious. The implementation of the binary conversion method of the algorithm in this chapter refers to the BigInt of c#. Generally speaking, this is a high-precision algorithm with better performance, which is more suitable for project development.