Skip to content

Commit

Permalink
Translate C to Swift
Browse files Browse the repository at this point in the history
Split the `CMicrophonePitchTracker` C module into new `ZenFFT` and
`ZenPTrack` Swift modules.

When compiling the new modules with `-Ounchecked` performance is within
2% of the C version. Without `-Ounchecked` it has a ~15% overhead.

With `-Ounchecked`:

```console
$ hyperfine --warmup 5 -m 20 ./pitchbench-c ./pitchbench-swift
Benchmark 1: ./pitchbench-c
  Time (mean ± σ):     392.8 ms ±   0.8 ms    [User: 379.5 ms, System: 9.4 ms]
  Range (min … max):   391.8 ms … 394.9 ms    20 runs
 
Benchmark 2: ./pitchbench-swift
  Time (mean ± σ):     401.5 ms ±   0.7 ms    [User: 386.9 ms, System: 10.7 ms]
  Range (min … max):   400.6 ms … 402.8 ms    20 runs
 
Summary
  './pitchbench-c' ran
    1.02 ± 0.00 times faster than './pitchbench-swift'
```

Without `-Ounchecked`:

```console
$ hyperfine --warmup 5 -m 20 ./pitchbench-c ./pitchbench-swift
Benchmark 1: ./pitchbench-c
  Time (mean ± σ):     393.0 ms ±   1.4 ms    [User: 380.3 ms, System: 8.7 ms]
  Range (min … max):   391.9 ms … 398.4 ms    20 runs

Benchmark 2: ./pitchbench-swift
  Time (mean ± σ):     451.1 ms ±   1.0 ms    [User: 436.0 ms, System: 11.0 ms]
  Range (min … max):   449.7 ms … 454.7 ms    20 runs

Summary
  './pitchbench-c' ran
    1.15 ± 0.00 times faster than './pitchbench-swift'
```
  • Loading branch information
jpsim committed Oct 18, 2023
1 parent 9197e4d commit 1a2a02a
Show file tree
Hide file tree
Showing 94 changed files with 2,349 additions and 2,652 deletions.
26 changes: 21 additions & 5 deletions Packages/MicrophonePitchDetector/Package.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// swift-tools-version:5.9
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

Expand All @@ -16,10 +15,27 @@ let package = Package(
)
],
targets: [
.executableTarget(name: "pitchbench", dependencies: ["PitchRecording"]),
.target(name: "PitchRecording", dependencies: ["MicrophonePitchDetector"]),
.target(name: "MicrophonePitchDetector", dependencies: ["CMicrophonePitchDetector"]),
.target(name: "CMicrophonePitchDetector"),
.executableTarget(
name: "pitchbench",
dependencies: ["PitchRecording"]
),
.target(
name: "PitchRecording",
dependencies: ["MicrophonePitchDetector"]
),
.target(
name: "MicrophonePitchDetector",
dependencies: ["ZenPTrack"]
),
.target(
name: "ZenPTrack",
dependencies: ["ZenFFT"],
swiftSettings: [.unsafeFlags(["-Ounchecked"], .when(configuration: .release))]
),
.target(
name: "ZenFFT",
swiftSettings: [.unsafeFlags(["-Ounchecked"], .when(configuration: .release))]
),
.testTarget(
name: "MicrophonePitchDetectorTests",
dependencies: [
Expand Down

This file was deleted.

Loading

0 comments on commit 1a2a02a

Please sign in to comment.