Skip to content

Commit

Permalink
Public timer information
Browse files Browse the repository at this point in the history
  • Loading branch information
Gary Kerr committed Jul 11, 2021
1 parent c03950d commit 7cbb146
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
4 changes: 2 additions & 2 deletions Sources/MandelbrotEngine/MandelbrotSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ public struct MandelbrotSet {
public var imageSize: (width: Int, height: Int)


public init(config: MandelbrotSetConfig, progress: Progress) {
public init(config: MandelbrotSetConfig, progress: Progress, timer: @escaping Timer.Action) {
self.config = config
let ys = Array(stride(from: config.yMin, to: config.yMax, by: config.dy))
let xs = Array(stride(from: config.xMin, to: config.xMax, by: config.dx))
imageSize = (xs.count, ys.count)
grid.reserveCapacity(xs.count * ys.count)

let progressHelper = ProgressHelper(steps: imageSize.height, progress: progress)
let timerHelper = TimerHelper()
let timerHelper = Timer(action: timer)

for (i, y) in ys.enumerated() {
for x in xs {
Expand Down
12 changes: 9 additions & 3 deletions Sources/MandelbrotEngine/TimerHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,24 @@

import Foundation

struct TimerHelper {
public struct Timer {
public typealias Action = (TimeInterval) -> Void

private let nano = 1_000_000_000.0
let start: DispatchTime
let action: Action


init() {
init(action: @escaping Action) {
self.action = action
self.start = DispatchTime.now()
}


func end() {
let e = DispatchTime.now()
let dt = e.uptimeNanoseconds - start.uptimeNanoseconds
print(Double(dt) / nano)
let s = Double(dt) / nano
action(s)
}
}

0 comments on commit 7cbb146

Please sign in to comment.