Skip to content

Instantly share code, notes, and snippets.

@kot331107
Created June 6, 2022 11:18
Show Gist options
  • Save kot331107/c391e54d7960944936bedef29297ece2 to your computer and use it in GitHub Desktop.
Save kot331107/c391e54d7960944936bedef29297ece2 to your computer and use it in GitHub Desktop.
simple function to measure time of execution of anything. it's pretty enough to measure some simple things.
//
// main.swift
// swift challenges
//
// Created by Filipp Mikheev on 06.06.22.
//
func measureTime(block : (() -> Void)) {
let start = DispatchTime.now()
block()
let end = DispatchTime.now()
let totalNanosec = end.uptimeNanoseconds - start.uptimeNanoseconds
let timeInterval = Double(totalNanosec) / 1_000_000_000
print("Time: \(timeInterval) seconds")
}
// USAGE
measureTime {
// Do your stuff here
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment