Skip to content

Benchmark {Float,Double,Float80}.description #15234

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions benchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ set(SWIFT_BENCH_MODULES
single-source/Exclusivity
single-source/ExistentialPerformance
single-source/Fibonacci
single-source/FloatingPointPrinting
single-source/Hanoi
single-source/Hash
single-source/HashQuadratic
Expand Down
204 changes: 204 additions & 0 deletions benchmark/single-source/FloatingPointPrinting.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
//===--- FloatingPointPrinting.swift -----------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2018 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
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

// This test verifies the performance of generating a text description
// from a binary floating-point value.

import TestsUtils

public let FloatingPointPrinting = [
BenchmarkInfo(
name: "FloatingPointPrinting_Float_description_small",
runFunction: run_FloatingPointPrinting_Float_description_small,
tags: [.validation, .api, .runtime, .String]),

BenchmarkInfo(
name: "FloatingPointPrinting_Double_description_small",
runFunction: run_FloatingPointPrinting_Double_description_small,
tags: [.validation, .api, .runtime, .String]),

BenchmarkInfo(
name: "FloatingPointPrinting_Float80_description_small",
runFunction: run_FloatingPointPrinting_Float80_description_small,
tags: [.validation, .api, .runtime, .String]),

BenchmarkInfo(
name: "FloatingPointPrinting_Float_description_uniform",
runFunction: run_FloatingPointPrinting_Float_description_uniform,
tags: [.validation, .api, .runtime, .String]),

BenchmarkInfo(
name: "FloatingPointPrinting_Double_description_uniform",
runFunction: run_FloatingPointPrinting_Double_description_uniform,
tags: [.validation, .api, .runtime, .String]),

BenchmarkInfo(
name: "FloatingPointPrinting_Float80_description_uniform",
runFunction: run_FloatingPointPrinting_Float80_description_uniform,
tags: [.validation, .api, .runtime, .String]),

BenchmarkInfo(
name: "FloatingPointPrinting_Float_interpolated",
runFunction: run_FloatingPointPrinting_Float_interpolated,
tags: [.validation, .api, .runtime, .String]),

BenchmarkInfo(
name: "FloatingPointPrinting_Double_interpolated",
runFunction: run_FloatingPointPrinting_Double_interpolated,
tags: [.validation, .api, .runtime, .String]),

BenchmarkInfo(
name: "FloatingPointPrinting_Float80_interpolated",
runFunction: run_FloatingPointPrinting_Float80_interpolated,
tags: [.validation, .api, .runtime, .String])
]

// Generate descriptions for 100,000 values around 1.0.
//
// Note that some formatting algorithms behave very
// differently for values around 1.0 than they do for
// less-common extreme values. Having a "small" test
// and a "uniform" test exercises both cases.
//
// Dividing integers 1...100000 by 101 (a prime) yields floating-point
// values from about 1e-2 to about 1e3, each with plenty of digits after
// the decimal:

@inline(never)
public func run_FloatingPointPrinting_Float_description_small(_ N: Int) {
let count = 100_000
for _ in 0..<N {
for i in 1...count {
let f = Float(i) / 101.0
blackHole(f.description)
}
}
}

@inline(never)
public func run_FloatingPointPrinting_Double_description_small(_ N: Int) {
let count = 100_000
for _ in 0..<N {
for i in 1...count {
let f = Double(i) / 101.0
blackHole(f.description)
}
}
}

@inline(never)
public func run_FloatingPointPrinting_Float80_description_small(_ N: Int) {
let count = 100_000
for _ in 0..<N {
for i in 1...count {
let f = Float80(i) / 101.0
blackHole(f.description)
}
}
}

// Generate descriptions for 100,000 values spread evenly across
// the full range of the type:

@inline(never)
public func run_FloatingPointPrinting_Float_description_uniform(_ N: Int) {
let count = 100_000
let step = UInt32.max / UInt32(count)
var s = ""
for _ in 0..<N {
for i in 0..<count {
let raw = UInt32(i) * step
let f = Float(bitPattern: raw)
blackHole(f.description)
}
}
}

@inline(never)
public func run_FloatingPointPrinting_Double_description_uniform(_ N: Int) {
let count = 100_000
let step = UInt64.max / UInt64(count)
var s = ""
for _ in 0..<N {
for i in 0..<count {
let raw = UInt64(i) * step
let f = Double(bitPattern: raw)
blackHole(f.description)
}
}
}

@inline(never)
public func run_FloatingPointPrinting_Float80_description_uniform(_ N: Int) {
let count = 100_000
let step = UInt64.max / UInt64(count)
var s = ""
for _ in 0..<N {
for i in 0..<count {
let fraction = UInt64(i) * step
let exponent = UInt(i) % 32768
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CC @stephentyrone as I don't know how to benchmark floats (or the magic numbers).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine.

let f = Float80(sign: .plus, exponentBitPattern: exponent, significandBitPattern: fraction)
blackHole(f.description)
}
}
}

// The "interpolated" tests verify that any storage optimizations used while
// producing the formatted numeric strings don't pessimize later use of the
// result.

@inline(never)
public func run_FloatingPointPrinting_Float_interpolated(_ N: Int) {
let count = 100_000
let step = UInt32.max / UInt32(count)
var s = ""
for _ in 0..<N {
for i in 0..<count {
let raw = UInt32(i) * step
let f = Float(bitPattern: raw)
blackHole("and the actual result was \(f)")
}
}
CheckResults(s.count > 1)
}

@inline(never)
public func run_FloatingPointPrinting_Double_interpolated(_ N: Int) {
let count = 100_000
let step = UInt64.max / UInt64(count)
var s = ""
for _ in 0..<N {
for i in 0..<count {
let raw = UInt64(i) * step
let f = Double(bitPattern: raw)
blackHole("and the actual result was \(f)")
}
}
CheckResults(s.count > 1)
}

@inline(never)
public func run_FloatingPointPrinting_Float80_interpolated(_ N: Int) {
let count = 100_000
let step = UInt64.max / UInt64(count)
var s = ""
for _ in 0..<N {
for i in 0..<count {
let fraction = UInt64(i) * step
let exponent = UInt(i) % 32768
let f = Float80(sign: .plus, exponentBitPattern: exponent, significandBitPattern: fraction)
blackHole("and the actual result was \(f)")
}
}
CheckResults(s.count > 1)
}

2 changes: 2 additions & 0 deletions benchmark/utils/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ import ErrorHandling
import Exclusivity
import ExistentialPerformance
import Fibonacci
import FloatingPointPrinting
import Hanoi
import Hash
import HashQuadratic
Expand Down Expand Up @@ -213,6 +214,7 @@ registerBenchmark(ErrorHandling)
registerBenchmark(Exclusivity)
registerBenchmark(ExistentialPerformance)
registerBenchmark(Fibonacci)
registerBenchmark(FloatingPointPrinting)
registerBenchmark(Hanoi)
registerBenchmark(HashTest)
registerBenchmark(HashQuadratic)
Expand Down