Skip to content

Instantly share code, notes, and snippets.

@kyungpyoda
Last active June 3, 2021 01:06
Show Gist options
  • Select an option

  • Save kyungpyoda/02c2ea13b23cd6b1d31712032ba093b4 to your computer and use it in GitHub Desktop.

Select an option

Save kyungpyoda/02c2ea13b23cd6b1d31712032ba093b4 to your computer and use it in GitHub Desktop.
[Swift] Measuring execution time by using Date object
//
// measure.swift
//
// Created by 홍경표 on 2021/05/31.
//
import Foundation
final class Util {
class func measure(_ description: String = "", _ function: () -> ()) {
let start = Date()
function()
let end = Date()
let description = description.isEmpty ? "" : "[\(description)] "
print("\(description)Execution Time:", end.timeIntervalSince(start))
}
}
/*
var randoms = (1...100000).map { _ in Int.random(in: 1...1000) }
Util.measure {
randoms.sort()
}
// let hugeStr = "..."
Util.measure("Split") {
_ = hugeStr.split(separator: " ")
}
Util.measure("Components") {
_ = hugeStr.components(separatedBy: " ")
}
*/
@kyungpyoda
Copy link
Author

kyungpyoda commented Jun 1, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment