Latest 0.5
Homepage https://github.com/untouchable741/Watches
License MIT
Platforms ios 8.0, osx 10.9, tvos 9.0, watchos 2.0
Authors

A simple stopwatches use to logging out the elapsed time between 2 timestamp, easily find out execution time of any function, a block of code or async callback.

Installation

Cocoapods

Add below line to your project Podfile

pod 'Watches'

Then run pod install from Terminal

Manual

Drag and drop Watches.swift source file into your project

Usage

By default, Watches will automatically print out the debugging log in format

"Elapsed interval for (identifier) is (interval)"

If you want to disable this auto log feature, set

Watches.printElapsedTimeAutomatically = false

Track elapsed time of any async callback (e.g : network request)
call tick to tickoff the time with specific identifier right before calling async task

Watches.tick("LoadProfile")

Then call tock with the corresponding identifier inside async callback closure

Watches.tock("LoadProfile")

For example with Alamofire

Watches.tick("LoadProfile")
Alamofire.request(loadProfileUrl).responseJSON { response in
    Watches.tock("LoadProfile")
}

It will print out the elapsed intervals in default format

Result: Elapsed interval for LoadProfile is 1.20113898515701

Feel free to provide custom format that you desire

Watches.tock("LoadProfile") { debugPrint("Time needed for ($0) is ($1)") }

If you want the elapsed interval for your own purpose, get it from the tock return value

//You can disable auto print feature if you want
Watches.printElapsedTimeAutomatically = false

//Then get the result from tock function
let elapsedTime = Watches.tock("LoadProfile")

Determine the execution time of any block of code.

Watches.create("1 million times loop").tick {
    //Block of code to measure
    for _ in 0...10000000 { }

}.tock()

Result: Elapsed interval for 1 million times loop is 0.214788019657135

If you want custom print format, just need to provide the tock callback closure.

Watches.create("2 million times loop").tick {
    //Block of code to measure
    for _ in 0...20000000 { }

}.tock { debugPrint("Time for finishing ($0) is ($1)")}

Result: Time for finishing 2 million times loop is 0.411646008491516

If you just want to get the result

let executionTime = Watches.create("MyClosure").tick { 
    //Block of code to measure
    sleep(3)
}.tock()

debugPrint(executionTime)

Result: 3.0011550188064575

License

Watches is released under the MIT License. See LICENSE file for more detail. Copyright © Vuong Huu Tai

Latest podspec

{
    "name": "Watches",
    "version": "0.5",
    "summary": "A Simple Stopwatches written in Swift help you easily measure the execution time of function, block of code or async callback.",
    "description": "A Simple swift stopwatches helps you easily logging out the elapsed time between 2 timestamps , find out execution time of function, a block of code or async callback.",
    "homepage": "https://github.com/untouchable741/Watches",
    "platforms": {
        "ios": "8.0",
        "osx": "10.9",
        "tvos": "9.0",
        "watchos": "2.0"
    },
    "swift_version": "4.2",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Tai Vuong": "[email protected]"
    },
    "source": {
        "git": "https://github.com/untouchable741/Watches.git",
        "tag": "0.5"
    },
    "source_files": "Watches/*.swift"
}

Pin It on Pinterest

Share This