-
Notifications
You must be signed in to change notification settings - Fork 297
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[benchmark] Review and extend Heap benchmarks (#76)
* [benchmark] Review and extend Heap benchmarks * FoundationBenchmark only supports Apple platforms * Rewrite CFBinaryHeap benchmarks in pure Swift
- Loading branch information
Showing
19 changed files
with
476 additions
and
196 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift Collections open source project | ||
// | ||
// Copyright (c) 2021 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import CollectionsBenchmark | ||
|
||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS) | ||
import Foundation | ||
|
||
extension CFBinaryHeap { | ||
internal static func _create(capacity: Int) -> CFBinaryHeap { | ||
var callbacks = CFBinaryHeapCallBacks( | ||
version: 0, | ||
retain: nil, | ||
release: nil, | ||
copyDescription: { value in | ||
let result = "\(Int(bitPattern: value))" as NSString | ||
return Unmanaged.passRetained(result) | ||
}, | ||
compare: { left, right, context in | ||
let left = Int(bitPattern: left) | ||
let right = Int(bitPattern: right) | ||
if left == right { return .compareEqualTo } | ||
if left < right { return .compareLessThan } | ||
return .compareGreaterThan | ||
}) | ||
return CFBinaryHeapCreate(kCFAllocatorDefault, capacity, &callbacks, nil) | ||
} | ||
} | ||
#endif | ||
|
||
extension Benchmark { | ||
public mutating func addFoundationBenchmarks() { | ||
self.add( | ||
title: "CFBinaryHeapAddValue", | ||
input: [Int].self | ||
) { input in | ||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS) | ||
return { timer in | ||
let heap = CFBinaryHeap._create(capacity: 0) | ||
timer.measure { | ||
for value in input { | ||
CFBinaryHeapAddValue(heap, UnsafeRawPointer(bitPattern: value)) | ||
} | ||
} | ||
blackHole(heap) | ||
} | ||
#else | ||
// CFBinaryHeap isn't available | ||
return nil | ||
#endif | ||
} | ||
|
||
self.add( | ||
title: "CFBinaryHeapAddValue, reserving capacity", | ||
input: [Int].self | ||
) { input in | ||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS) | ||
return { timer in | ||
let heap = CFBinaryHeap._create(capacity: input.count) | ||
timer.measure { | ||
for value in input { | ||
CFBinaryHeapAddValue(heap, UnsafeRawPointer(bitPattern: value)) | ||
} | ||
} | ||
blackHole(heap) | ||
} | ||
#else | ||
return nil | ||
#endif | ||
} | ||
|
||
self.add( | ||
title: "CFBinaryHeapRemoveMinimumValue", | ||
input: [Int].self | ||
) { input in | ||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS) | ||
return { timer in | ||
let heap = CFBinaryHeap._create(capacity: input.count) | ||
for value in input { | ||
CFBinaryHeapAddValue(heap, UnsafeRawPointer(bitPattern: value)) | ||
} | ||
timer.measure { | ||
for _ in 0 ..< input.count { | ||
blackHole(CFBinaryHeapGetMinimum(heap)) | ||
CFBinaryHeapRemoveMinimumValue(heap) | ||
} | ||
} | ||
blackHole(heap) | ||
} | ||
#else | ||
return nil | ||
#endif | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.