Coder Social home page Coder Social logo

timely's Introduction

Timely

A tiny timing library for Swift. Timely allows you to easily time the execution of blocks of code.

Usage

Timely.time("Timer 1") { doSomething() }

let answer: String = Timely.time("Timer 2") { returnsAString("A", "B") }

Timely wraps your existing code and times its execution. It is easy to drop into existing code when performance measurements are needed.

However, no timing will actually occur until Timely knows what to do with its measurements. To remain light and highly customisable, Timely deliberately makes no assumptions about what you want to do with its timing data. To start receiving timing reports, you have to set a report function:

Timely.setReportFunction { timer, seconds in
    // With the examples above, `timer` will be "Timer 1" and "Timer 2".
    print("Timer with name \(timer) took \(seconds) seconds.")
}

This closure will be executed synchronously after a timer completes. Its arguments are the name of the timer just executed and the number of seconds it measured. Timely's measurements are as accurate as Foundation's Date class is for the execution platform.

You can of course dispatch asynchronous code in the report function. You might want to aggregate the data, log it, write it out to a file, send it to a Splunk instance, etc.

If you want to stop Timely from timing (for example during tests) you can call:

Timely.noop()

Timely will then simply execute the code blocks it is passed without any timing or reporting (as it did before it was provided a report function). If you want to start timing again, simply pass another report function.

There might be times when you have already set up Timely's report function, but want to use a different reporting mechanism for a particular timer. This can be done with an overload of the time method that applies a custom report function to that timer only:

Timely.time("Timer", withReportFunction: CUSTOM_REPORT_FUNCTION) { doSomething() }

License

Timely is released under the MIT License. See LICENSE.md for details.

timely's People

Contributors

davidrpiper avatar

Watchers

Suhaib Abdulghani avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.