Skip to content

Commit bd0977a

Browse files
committed
Update README with usage instructions and example
1 parent 8163295 commit bd0977a

File tree

1 file changed

+65
-95
lines changed

1 file changed

+65
-95
lines changed

README.md

Lines changed: 65 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,72 @@
1-
# swift-benchmark
2-
3-
A Swift library for benchmarking code snippets, similar to
4-
[google/benchmark](https://github.com/google/benchmark).
5-
6-
Example:
7-
1+
# Swift Benchmark
2+
Swift Benchmark is a library for writing and running benchmarks in Swift.
3+
## Getting Started
4+
To use `swift-benchmark` in your project, add it to your `Package.swift` dependencies:
5+
```swift
6+
// swift-tools-version:5.3
7+
import PackageDescription
8+
let package = Package(
9+
name: "YourProject",
10+
dependencies: [
11+
.package(url: "https://github.com/google/swift-benchmark.git", from: "0.1.0"),
12+
],
13+
targets: [
14+
.target(
15+
name: "YourProject",
16+
dependencies: ["Benchmark"]),
17+
]
18+
)
19+
```
20+
Then build your project:
21+
```bash
22+
swift build
23+
```
24+
## Example Benchmark
825
```swift
926
import Benchmark
10-
11-
benchmark("add string reserved capacity") {
12-
var x: String = ""
13-
x.reserveCapacity(2000)
14-
for _ in 1...1000 {
15-
x += "hi"
16-
}
27+
let benchmark = BenchmarkSuite(name: "Sample") { suite in
28+
suite.benchmark("Loop") {
29+
for _ in 0..<1_000_000 { }
30+
}
1731
}
18-
19-
Benchmark.main()
32+
Benchmark.main([benchmark])
2033
```
21-
22-
At runtime, you can filter which benchmarks to run by using the `--filter` command line flag. For
23-
more details on what options are available, pass either the `-h` or `--help` command line flags.
24-
25-
Example:
26-
27-
```terminal
28-
$ swift run -c release BenchmarkMinimalExample --help
29-
USAGE: benchmark-command [--allow-debug-build] [--filter <filter>] [--filter-not <filter-not>] [--iterations <iterations>] [--warmup-iterations <warmup-iterations>] [--min-time <min-time>] [--max-iterations <max-iterations>] [--time-unit <time-unit>] [--inverse-time-unit <inverse-time-unit>] [--columns <columns>] [--format <format>] [--quiet]
30-
31-
OPTIONS:
32-
--allow-debug-build Overrides check to verify optimized build.
33-
--filter <filter> Run only benchmarks whose names match the regular expression.
34-
--filter-not <filter-not>
35-
Exclude benchmarks whose names match the regular expression.
36-
--iterations <iterations>
37-
Number of iterations to run.
38-
--warmup-iterations <warmup-iterations>
39-
Number of warm-up iterations to run.
40-
--min-time <min-time> Minimal time to run when automatically detecting number iterations.
41-
--max-iterations <max-iterations>
42-
Maximum number of iterations to run when automatically detecting number iterations.
43-
--time-unit <time-unit> Time unit used to report the timing results.
44-
--inverse-time-unit <inverse-time-unit>
45-
Inverse time unit used to report throughput results.
46-
--columns <columns> Comma-separated list of column names to show.
47-
--format <format> Output format (valid values are: json, csv, console, none).
48-
--quiet Only print final benchmark results.
49-
-h, --help Show help information.
50-
51-
$ swift run -c release BenchmarkMinimalExample
52-
running add string no capacity... done! (1832.52 ms)
53-
running add string reserved capacity... done! (1813.96 ms)
54-
55-
name time std iterations
56-
-----------------------------------------------------------
57-
add string no capacity 37435 ns ± 6.22 % 37196
58-
add string reserved capacity 37022 ns ± 1.75 % 37749
34+
## Running Benchmarks
35+
Use Swift Package Manager:
36+
```bash
37+
swift run BenchmarkMinimalExample
5938
```
60-
61-
For more examples, see
62-
[Sources/BenchmarkMinimalExample](./Sources/BenchmarkMinimalExample) and
63-
[Sources/BenchmarkSuiteExample](./Sources/BenchmarkSuiteExample).
64-
65-
## Usage
66-
67-
Add this library as a SwiftPM dependency:
68-
69-
```swift
70-
let package = Package(
71-
name: ... ,
72-
products: [
73-
.executable(name: "Benchmarks", targets: ["Benchmarks"])
74-
],
75-
dependencies: [
76-
.package(url: "https://github.com/google/swift-benchmark", from: "0.1.0")
77-
],
78-
targets: [
79-
.target(
80-
name: "Benchmarks",
81-
dependencies: [.product(name: "Benchmark", package: "swift-benchmark")]
82-
)
83-
]
84-
)
39+
Example output:
8540
```
86-
87-
## Roadmap
88-
89-
The project is in an early stage and offers only a basic set of benchmarking
90-
utilities. Feel free to file issues and feature requests to help us prioritize
91-
what to do next.
92-
93-
## License
94-
95-
Please see [LICENSE](LICENSE) for details.
96-
41+
name time std iterations
42+
------------------------------------------------
43+
Loop 12.3 ms ±0.2 ms 100
44+
```
45+
## Writing Good Benchmarks
46+
- Keep your benchmark small and focused
47+
- Avoid doing I/O inside benchmark loops
48+
- Prefer micro-benchmarks to isolate performance of small code blocks
49+
- Benchmark consistent workloads
9750
## Contributing
98-
99-
Please see [CONTRIBUTING.md] for details.
100-
101-
[CONTRIBUTING.md]: CONTRIBUTING.md
102-
51+
We welcome contributions! If you want to improve this README or add new
52+
benchmarks:
53+
1. Fork the repository
54+
2. Create a new branch:
55+
```bash
56+
git checkout -b improve-readme
57+
```
58+
3. Make your changes
59+
4. Stage your changes:
60+
```bash
61+
git add README.md
62+
```
63+
5. Commit your changes:
64+
```bash
65+
git commit -m "Improve README with usage and example"
66+
```
67+
6. Push your branch:
68+
```bash
69+
git push origin improve-readme
70+
```
71+
7. Open a Pull Request to the main repository with a clear description
72+
8. Don't forget to sign the Google CLA

0 commit comments

Comments
 (0)