Skip to content

Commit

Permalink
Add benchmarks for random arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
nvzqz committed Nov 24, 2016
1 parent 6054d39 commit d60e10a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
20 changes: 20 additions & 0 deletions Sources/benchmark/Benchmark.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,23 @@ func benchmarkRandomWithinClosedRange<T: RandomWithinClosedRange>(with closedRan
print("")
}
}

func benchmarkRandomArray<T: Random>(for type: T.Type, randomCount: Int, count: Int = count, using generators: [RandomGenerator] = generators) {
for randomGenerator in generators {
print("Generating random arrays for " + style(type) + " of " + style(randomCount) + " using " + style(randomGenerator))
benchmark(count: count) {
let _ = [T](randomCount: randomCount, using: randomGenerator)
}
print("")
}
}

func benchmarkUnsafeRandomArray<T: UnsafeRandom>(for type: T.Type, randomCount: Int, count: Int = count, using generators: [RandomGenerator] = generators) {
for randomGenerator in generators {
print("Generating unsafe random arrays for " + style(type) + " of " + style(randomCount) + " using " + style(randomGenerator))
benchmark(count: count) {
let _ = [T](unsafeRandomCount: randomCount, using: randomGenerator)
}
print("")
}
}
34 changes: 26 additions & 8 deletions Sources/benchmark/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ func contains(_ s: String) -> Bool {
return CommandLine.arguments.contains(s)
}

func argument(after arg: String) -> String? {
if let index = CommandLine.arguments.index(of: arg) {
return CommandLine.arguments[safe: index + 1]
} else {
return nil
}
}

func int(after arg: String) -> Int? {
return argument(after: arg).flatMap { Int($0) }
}

let styleOutput = !contains("--no-color")

let benchmarkAll = contains("--all")
Expand All @@ -44,14 +56,12 @@ let benchmarkRandomThroughValue = benchmarkAllProtocols || contains("Random
let benchmarkRandomWithinRange = benchmarkAllProtocols || contains("RandomWithinRange")
let benchmarkRandomWithinClosedRange = benchmarkAllProtocols || contains("RandomWithinClosedRange")

let count: Int = {
let args = CommandLine.arguments
if let index = args.index(of: "--count"), let count = args[safe: index + 1].flatMap({ Int($0) }) {
return count
} else {
return 10_000_000
}
}()
let benchmarkRandomArray = contains("--array")
let benchmarkRandomArrayCount = int(after: "--array") ?? 100
let benchmarkUnsafeRandomArray = contains("--array-unsafe")
let benchmarkUnsafeRandomArrayCount = int(after: "--array-unsafe") ?? 100

let count = int(after: "--count") ?? 10_000_000

let generators: [RandomGenerator]
if benchmarkAllGenerators {
Expand Down Expand Up @@ -178,3 +188,11 @@ if benchmarkRandomWithinClosedRange {
benchmarkRandomWithinClosedRange(with: UInt8.minMaxClosedRange)
}
}

if benchmarkRandomArray {
benchmarkRandomArray(for: Int.self, randomCount: benchmarkRandomArrayCount)
}

if benchmarkUnsafeRandomArray {
benchmarkUnsafeRandomArray(for: Int.self, randomCount: benchmarkUnsafeRandomArrayCount)
}

0 comments on commit d60e10a

Please sign in to comment.