Skip to content

Instantly share code, notes, and snippets.

@leonid-shevtsov
Created June 11, 2024 11:09
Show Gist options
  • Save leonid-shevtsov/8cfb1b9842e2b5c00a24b86ab4fb9eb0 to your computer and use it in GitHub Desktop.
Save leonid-shevtsov/8cfb1b9842e2b5c00a24b86ab4fb9eb0 to your computer and use it in GitHub Desktop.
import Foundation
// Test impact of high-CPU actor on Swift app performance
// Task { let piActor = piActor; await piActor.calculatePI() }
// Code from https://alexwulff.medium.com/calculating-pi-with-swift-ed36730405ed
actor PiActor {
func calculatePI() async {
while(true) {
var GregoryLeibniz : Double = 0
var counter = 1
while GregoryLeibniz * 4 != 3.1415 {
var oddNumber = 2*counter - 1
if counter % 2 == 0 {
GregoryLeibniz -= 1/Double(oddNumber)
}
else {
GregoryLeibniz += 1/Double(oddNumber)
}
print(GregoryLeibniz*4)
counter += 1
}
print(counter)
// insert suspension point
do {
try await Task.sleep(nanoseconds: 1000*1000)
} catch {
print("error sleeping: \(error)")
return
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment