Esqarrouth/EZSwiftExtensions

Would it make sense to have a "benchmark" extension for all methods?

Khalian opened this issue · 4 comments

Something along the lines of:-

func executionTimeInterval(block: () -> ()) -> CFTimeInterval {
let start = CACurrentMediaTime()
block();
let end = CACurrentMediaTime()
return end - start
}

Referenced from : https://gist.github.com/kristopherjohnson/4201fbe86473f6edb207

But here block can be any function (in essence executionTimeInterval is a higher order function on everything).

hmmmm.... I don't think I'd use that

I might use it, I'm currently using this:

struct DebugTimer {
    private static var startTime = NSDate()
    private static var lastCheckTime = NSDate()
    
    static func start() {
        startTime = NSDate()
        lastCheckTime = startTime
    }
    
    static func elapsed() -> Double {
        return -startTime.timeIntervalSinceNow
    }
    
    static func elapsedSinceLastCheck() -> String {
        let elapsedTime = -lastCheckTime.timeIntervalSinceNow
        lastCheckTime = NSDate()
        return elapsedTime.toString
    }
}

@lfarah So as a backend engineer I deeply care about profiling code. I have requirements that particular pieces of code do not exceed a particular time threshhold.

This extension would act as a profiling tool.

@goktugyil Yes this is a useful bit of code. What I was imagining is a higher order method that takes as an argument all possible methods and returning both the return value from the method and the time required to run the method.

Ok so XCTest Already has this :

open func measure(_ block: @escaping () -> Swift.Void)

Closing.