-
Notifications
You must be signed in to change notification settings - Fork 10.5k
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
stephentyrone
merged 6 commits into
swiftlang:master
from
tbkka:tbkka-float-description-benchmark
Mar 15, 2018
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
45cdec4
Benchmark {Float,Double,Float80}.description
tbkka 47df609
Benchmark {Float,Double,Float80}.description
tbkka 0ab3316
Merge branch 'tbkka-float-description-benchmark' of github.com:tbkka/…
tbkka 0608035
Add benchmarks for values close to 1 + other review suggestions
tbkka 1d346e8
Merge branch 'master' into tbkka-float-description-benchmark
tbkka d31be0d
Merge branch 'tbkka-float-description-benchmark' of github.com:tbkka/…
tbkka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,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 | ||
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) | ||
} | ||
|
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is fine.