Performance of TaskGroup

Batching allows 100_000 child tasks to complete in 0.125s.

@main
struct ConcurrencyTest {
    static func main() async {
        let start = CFAbsoluteTimeGetCurrent()
        await withTaskGroup(of: Void.self) { group in
            @Sendable
            func doNothing() {}

            var tasksToPerform = 99_990
            for _ in 0..<10 {
                group.addTask(operation: doNothing)
            }

            for await _ in group {
                if tasksToPerform > 0 {
                    group.addTask(operation: doNothing)
                    tasksToPerform -= 1
                }
            }
        }
        print(CFAbsoluteTimeGetCurrent() - start)
    }
}
3 Likes