Skip to content

Commit 4d78511

Browse files
improved HugeInt addition and subtraction performance, and...
- updated the benchmarks in README to reflect the improvements
1 parent 3a4fe20 commit 4d78511

File tree

3 files changed

+16
-34
lines changed

3 files changed

+16
-34
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ The tables below display the nanoseconds longer it took this library to calculat
3535
|:------------|:---------------:|:---------------:|:---------------:|:---------------:|:---------------:|:---------------:|:---------------:|
3636
|1.0.6 |DEBUG |8237502387529357 |397653549738 |~2,800 |~2,400 |~43,000\* |~225,000 |
3737
|1.0.13 |DEBUG |8237502387529357 |397653549738 |~2,450 |~2,350 |~38,100\* |~208,700 |
38-
|1.0.14 |RELEASE |8237502387529357 |397653549738 |~200 |~210 |~4,150\* |~15,500 |
38+
|1.0.14 |RELEASE |8237502387529357 |397653549738 |~170 |~200 |~3,950\* |~15,400 |
3939
#### `HugeFloat`
4040
|Version |Scheme |Precision |Left Number |Right Number |Addition |Subtraction |Multiplication |Division |
4141
|:------------|:---------------:|:---------------:|:---------------:|:---------------:|:---------------:|:---------------:|:---------------:|:---------------:|
4242
|1.0.6 |DEBUG |100 |12345.678 |54321.012 |~8,200 |~8,300 |~24,000 |~13,000,000 |
4343
|1.0.13 |DEBUG |6 |12345.678 |54321.012 |~8,000 |~8,300 |~22,000 |~410,000 |
4444
|1.0.13 |DEBUG |100 |12345.678 |54321.012 |- |- |- |~9,306,000 |
45-
|1.0.14 |RELEASE |6 |12345.678 |54321.012 |~840 |~1,120 |~3,400 |~41,000 |
46-
|1.0.14 |RELEASE |100 |12345.678 |54321.012 |- |- |- |~680,000 |
45+
|1.0.14 |RELEASE |6 |12345.678 |54321.012 |~790 |~1,080 |~3,300 |~39,500 |
46+
|1.0.14 |RELEASE |100 |12345.678 |54321.012 |- |- |- |~660,000 |
4747

4848
## Contributing
4949
Adding/improving functionality is always welcome, just make a PR.

Sources/huge-numbers/HugeInt.swift

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ public struct HugeInt : Hashable, Comparable, Codable, CustomStringConvertible {
174174
let this:HugeInt = self
175175
var maximum:HugeInt = maximum
176176
let two:HugeInt = HugeInt(is_negative: false, [2]), one:HugeInt = HugeInt.one
177-
let factors:Set<HugeInt> = await withTaskGroup(of: HugeInt?.self, body: { group in
177+
return await withTaskGroup(of: HugeInt?.self, body: { group in
178178
while maximum >= two {
179179
let target_number:HugeInt = maximum
180180
group.addTask {
@@ -190,7 +190,6 @@ public struct HugeInt : Hashable, Comparable, Codable, CustomStringConvertible {
190190
}
191191
return array
192192
})
193-
return factors
194193
}
195194
/// - Warning: This function assumes self is less than or equal to the given number.
196195
/// - Warning: Very resource intensive when using big numbers.
@@ -480,22 +479,13 @@ internal extension HugeInt {
480479
static func add(bigger_numbers: [Int8], smaller_numbers: [Int8]) -> [Int8] {
481480
let smaller_numbers_length:Int = smaller_numbers.count
482481
let result_count:Int = bigger_numbers.count + 1
483-
var result:[Int8] = [Int8].init(repeating: 0, count: result_count)
482+
var result:[Int8] = bigger_numbers
483+
result.append(0)
484484

485-
var remainder:Int8 = 0
486485
for index in 0..<smaller_numbers_length {
487-
var new_value:Int8 = smaller_numbers[index] + bigger_numbers[index] + remainder
488-
if new_value > 9 {
489-
new_value -= 10
490-
remainder = 1
491-
} else {
492-
remainder = 0
493-
}
494-
result[index] = new_value
486+
result[index] += smaller_numbers[index]
495487
}
496-
result[smaller_numbers_length] = remainder
497-
for i in smaller_numbers_length..<result_count-1 {
498-
result[i] += bigger_numbers[i]
488+
for i in 0..<result_count {
499489
if result[i] > 9 {
500490
result[i] -= 10
501491
result[i+1] += 1
@@ -539,22 +529,14 @@ internal extension HugeInt {
539529
static func subtract(bigger_numbers: [Int8], smaller_numbers: [Int8]) -> [Int8] {
540530
let smaller_numbers_length:Int = smaller_numbers.count
541531
let result_count:Int = bigger_numbers.count
542-
var result:[Int8] = [Int8].init(repeating: 0, count: result_count)
532+
var result:[Int8] = bigger_numbers
543533

544534
for index in 0..<smaller_numbers_length {
545-
result[index] += bigger_numbers[index]
546-
var value:Int8 = result[index] - smaller_numbers[index]
547-
if value < 0 {
548-
value += 10
549-
result[index+1] -= 1
550-
}
551-
result[index] = value
535+
result[index] -= smaller_numbers[index]
552536
}
553-
for i in smaller_numbers_length..<result_count {
554-
result[i] += bigger_numbers[i]
537+
for i in 0..<result_count {
555538
if result[i] < 0 {
556-
let value:Int8 = result[i] + 10
557-
result[i] = value
539+
result[i] += 10
558540
result[i+1] -= 1
559541
}
560542
}

Tests/huge-numbersTests/huge_numbersTests.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import HugeNumbers
1010

1111
final class huge_numbersTests: XCTestCase {
1212
func testExample() async throws {
13-
//try await test_benchmarks()
13+
try await test_benchmarks()
1414

1515
await test_int()
1616
test_float()
@@ -36,12 +36,12 @@ extension huge_numbersTests {
3636
//try await test_benchmark_integer_addition()
3737
//try await test_benchmark_integer_subtraction()
3838
//try await test_benchmark_integer_multiplication()
39-
try await test_benchmark_integer_division()
39+
//try await test_benchmark_integer_division()
4040

4141
//try await test_benchmark_float_addition()
4242
//try await test_benchmark_float_subtraction()
4343
//try await test_benchmark_float_multiplication()
44-
//try await test_benchmark_float_division()
44+
try await test_benchmark_float_division()
4545
}
4646
}
4747
@available(macOS 13.0, *)
@@ -119,7 +119,7 @@ extension huge_numbersTests {
119119
private func test_benchmark_float_division() async throws {
120120
let left_native:Float = 12345.678, right_native:Float = 54321.012
121121
let left:HugeFloat = HugeFloat("12345.678"), right:HugeFloat = HugeFloat("54321.012")
122-
let precision:HugeInt = HugeInt("100")
122+
let precision:HugeInt = HugeInt.float_precision
123123
try await benchmark_compare_is_faster(key1: "HugeFloat.divide", {
124124
let _:HugeFloat = left.divide_by(right, precision: precision)
125125
}, key2: "Float.divide", code2: {

0 commit comments

Comments
 (0)