Skip to content

Commit 9eb1e6e

Browse files
committed
more benchmarks
1 parent e644d22 commit 9eb1e6e

2 files changed

Lines changed: 98 additions & 92 deletions

File tree

Sources/Benchmarks/Benchmarks.swift

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,4 +295,94 @@ public class Benchmarks
295295
_ = b1 * b2
296296
}
297297
}
298+
299+
static func divisionBalanced()
300+
{
301+
let b1 = BIntMath.randomBInt(bits: 200_000)
302+
let b2 = BIntMath.randomBInt(bits: 100_000)
303+
304+
benchmarkAndPrint(title: "Divide 200_000-bit by 100_000-bit BInt")
305+
{
306+
_ = b1 / b2
307+
}
308+
}
309+
310+
static func divisionUnbalanced()
311+
{
312+
let b1 = BIntMath.randomBInt(bits: 10_000_000)
313+
let b2 = BIntMath.randomBInt(bits: 1_000)
314+
315+
benchmarkAndPrint(title: "Divide 10_000_000-bit by 1_000-bit BInt")
316+
{
317+
_ = b1 / b2
318+
}
319+
}
320+
321+
static func gcdLargeNumbers()
322+
{
323+
let b1 = BIntMath.randomBInt(bits: 130_000)
324+
let b2 = BIntMath.randomBInt(bits: 130_000)
325+
326+
benchmarkAndPrint(title: "GCD of two 130_000-bit BInts")
327+
{
328+
_ = BIntMath.gcd(b1, b2)
329+
}
330+
}
331+
332+
static func modularExponentiation()
333+
{
334+
let base = BIntMath.randomBInt(bits: 1800)
335+
let exp = BIntMath.randomBInt(bits: 1800)
336+
let modulus = BIntMath.randomBInt(bits: 1800)
337+
338+
benchmarkAndPrint(title: "mod_exp with 1800-bit base, exp, modulus")
339+
{
340+
_ = BIntMath.mod_exp(base, exp, modulus)
341+
}
342+
}
343+
344+
static func BDoubleDivision()
345+
{
346+
let num = BDouble(BIntMath.randomBInt(bits: 100_000), over: BInt(7))
347+
let den = BDouble(BIntMath.randomBInt(bits: 100_000), over: BInt(13))
348+
349+
benchmarkAndPrint(title: "BDouble division, 100_000-bit numerators")
350+
{
351+
_ = num / den
352+
}
353+
}
354+
355+
static func BDoubleDecimalExpansion()
356+
{
357+
let val = BDouble(1, over: 7)
358+
359+
benchmarkAndPrint(title: "BDouble decimal expansion, 70_000 digits")
360+
{
361+
_ = val.decimalExpansion(precisionAfterDecimalPoint: 70_000, rounded: false)
362+
}
363+
}
364+
365+
static func radixConversion()
366+
{
367+
let big = BInt(1_500).factorial()
368+
369+
benchmarkAndPrint(title: "1500! to hex string")
370+
{
371+
_ = big.asString(radix: 16)
372+
}
373+
}
374+
375+
static func bitShifting()
376+
{
377+
let big = BIntMath.randomBInt(bits: 1_000_000)
378+
379+
benchmarkAndPrint(title: "Shift 1_000_000-bit BInt left by 500_000, 700 times")
380+
{
381+
var x = big
382+
for _ in 0..<100
383+
{
384+
x = x << 5_000_000
385+
}
386+
}
387+
}
298388
}

Sources/Benchmarks/main.swift

Lines changed: 8 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -12,95 +12,11 @@ Benchmarks.StringToBInt()
1212
//Benchmarks.permutationsAndCombinations()
1313
Benchmarks.multiplicationBalanced()
1414
Benchmarks.multiplicationUnbalanced()
15-
16-
17-
18-
19-
20-
21-
22-
23-
24-
25-
26-
//public struct PixelData {
27-
// var a:UInt8 = 255
28-
// var r:UInt8
29-
// var g:UInt8
30-
// var b:UInt8
31-
//}
32-
//
33-
//private let rgbColorSpace = CGColorSpaceCreateDeviceRGB()
34-
//private let bitmapInfo:CGBitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedFirst.rawValue)
35-
//
36-
//public func imageFromARGB32Bitmap(pixels:[PixelData], width: Int, height: Int) -> CGImage {
37-
// let bitsPerComponent: Int = 8
38-
// let bitsPerPixel: Int = 32
39-
//
40-
// assert(pixels.count == Int(width * height))
41-
//
42-
// var data = pixels // Copy to mutable []
43-
// let providerRef = CGDataProvider(
44-
// data: NSData(bytes: &data, length: data.count * MemoryLayout<PixelData>.size)
45-
// )
46-
//
47-
// let cgim = CGImage(
48-
// width: width,
49-
// height: height,
50-
// bitsPerComponent: bitsPerComponent,
51-
// bitsPerPixel: bitsPerPixel,
52-
// bytesPerRow: width * MemoryLayout<PixelData>.size,
53-
// space: rgbColorSpace,
54-
// bitmapInfo: bitmapInfo,
55-
// provider: providerRef!,
56-
// decode: nil,
57-
// shouldInterpolate: true,
58-
// intent: CGColorRenderingIntent.defaultIntent
59-
// )
60-
// return cgim!
61-
//}
62-
//
63-
//
64-
//@discardableResult
65-
//func writeCGImage(_ image: CGImage, to destinationURL: URL) -> Bool {
66-
// guard let destination = CGImageDestinationCreateWithURL(destinationURL as CFURL, kUTTypePNG, 1, nil) else {
67-
// print("ERR")
68-
// return false
69-
//
70-
// }
71-
// CGImageDestinationAddImage(destination, image, nil)
72-
// return CGImageDestinationFinalize(destination)
73-
//}
74-
//
75-
//func makeAndSave()
76-
//{
77-
// let dimension = 4000
78-
//
79-
//
80-
// var pixels = [PixelData]()
81-
//
82-
// for i in 1...(dimension * dimension)
83-
// {
84-
// if math.isPrime(i)
85-
// {
86-
// pixels.append(PixelData(a: 255, r: 0, g: 0, b: 0))
87-
// }
88-
// else
89-
// {
90-
// pixels.append(PixelData(a: 0, r: 255, g: 255, b: 255))
91-
// }
92-
// }
93-
//
94-
//
95-
// let img = imageFromARGB32Bitmap(pixels: pixels, width: dimension, height: dimension)
96-
//
97-
// let writeURL = FileManager.default.urls(for: .downloadsDirectory, in: .userDomainMask).first!.appendingPathComponent("name.png")
98-
//
99-
// writeCGImage(img, to: writeURL)
100-
//}
101-
//
102-
//benchmarkAndPrint(title: "")
103-
//{
104-
// makeAndSave()
105-
//}
106-
15+
Benchmarks.divisionBalanced()
16+
Benchmarks.divisionUnbalanced()
17+
Benchmarks.gcdLargeNumbers()
18+
Benchmarks.modularExponentiation()
19+
Benchmarks.BDoubleDivision()
20+
Benchmarks.BDoubleDecimalExpansion()
21+
Benchmarks.radixConversion()
22+
Benchmarks.bitShifting()

0 commit comments

Comments
 (0)