-
-
Save leonid-shevtsov/8cfb1b9842e2b5c00a24b86ab4fb9eb0 to your computer and use it in GitHub Desktop.
This file contains 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
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