|
| 1 | +//===--- RangeOverlaps.swift ----------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import TestsUtils |
| 14 | + |
| 15 | +public let RangeOverlaps = [ |
| 16 | + BenchmarkInfo( |
| 17 | + name: "RangeOverlapsRange", |
| 18 | + runFunction: run_RangeOverlapsRange, |
| 19 | + tags: [.validation, .api], |
| 20 | + setUpFunction: buildRanges), |
| 21 | + BenchmarkInfo( |
| 22 | + name: "RangeOverlapsClosedRange", |
| 23 | + runFunction: run_RangeOverlapsClosedRange, |
| 24 | + tags: [.validation, .api], |
| 25 | + setUpFunction: buildRanges), |
| 26 | + BenchmarkInfo( |
| 27 | + name: "ClosedRangeOverlapsClosedRange", |
| 28 | + runFunction: run_ClosedRangeOverlapsClosedRange, |
| 29 | + tags: [.validation, .api], |
| 30 | + setUpFunction: buildRanges) |
| 31 | +] |
| 32 | + |
| 33 | +private func buildRanges() { |
| 34 | + blackHole(ranges) |
| 35 | + blackHole(closedRanges) |
| 36 | +} |
| 37 | + |
| 38 | +private let ranges: [Range<Int>] = (-8...8).flatMap { a in (0...16).map { l in a..<(a+l) } } |
| 39 | +private let closedRanges: [ClosedRange<Int>] = (-8...8).flatMap { a in (0...16).map { l in a...(a+l) } } |
| 40 | + |
| 41 | +@inline(never) |
| 42 | +public func run_RangeOverlapsRange(_ N: Int) { |
| 43 | + var check: UInt64 = 0 |
| 44 | + for _ in 0..<N { |
| 45 | + for lhs in ranges { |
| 46 | + for rhs in ranges { |
| 47 | + if lhs.overlaps(rhs) { check += 1 } |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + CheckResults(check == 47872 * UInt64(N)) |
| 52 | +} |
| 53 | + |
| 54 | +@inline(never) |
| 55 | +public func run_RangeOverlapsClosedRange(_ N: Int) { |
| 56 | + var check: UInt64 = 0 |
| 57 | + for _ in 0..<N { |
| 58 | + for lhs in ranges { |
| 59 | + for rhs in closedRanges { |
| 60 | + if lhs.overlaps(rhs) { check += 1 } |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + CheckResults(check == 51680 * UInt64(N)) |
| 65 | +} |
| 66 | + |
| 67 | +@inline(never) |
| 68 | +public func run_ClosedRangeOverlapsClosedRange(_ N: Int) { |
| 69 | + var check: UInt64 = 0 |
| 70 | + for _ in 0..<N { |
| 71 | + for lhs in closedRanges { |
| 72 | + for rhs in closedRanges { |
| 73 | + if lhs.overlaps(rhs) { check += 1 } |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + CheckResults(check == 55777 * UInt64(N)) |
| 78 | +} |
0 commit comments