Created
April 20, 2020 08:08
-
-
Save jcampbell05/56f85f2ed24b4837261c9939f4983537 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// main.swift | |
// StructSize | |
// | |
// Created by James Campbell on 20/04/2020. | |
// Copyright © 2020 James Campbell. All rights reserved. | |
// | |
import Foundation | |
import AppKit | |
struct CellA { | |
var borderWidth: Int {4} | |
} | |
struct CellB { | |
var borderWidth: Int = 4 | |
} | |
struct CellC { | |
static var borderWidth: Int = 4 | |
} | |
struct CellD { | |
static var _borderWidth: Int = 4 | |
var borderWidth: Int { Self._borderWidth } | |
} | |
struct CellE { | |
static var _borderWidth: Int = 4 | |
var borderWidth: Int = Self._borderWidth | |
} | |
func measureExecutionTime(label: String, subject: () -> Void) { | |
let operationsPerTrial = 100_000_000 | |
var minTime = Double.infinity | |
print("\(label):") | |
for _ in 0 ..< operationsPerTrial { | |
let t0 = CACurrentMediaTime() | |
subject() | |
let t1 = CACurrentMediaTime() | |
let time = t1 - t0 | |
print(" time:", time, "seconds") | |
minTime = min(minTime, time) | |
} | |
print(" minimum time:", minTime, "seconds\n") | |
} | |
let cellA = { | |
let c = CellA() | |
_ = c.borderWidth + 1 | |
} | |
let cellB = { | |
let c = CellB() | |
_ = c.borderWidth + 1 | |
} | |
let cellC = { | |
_ = CellC.borderWidth + 1 | |
} | |
let cellD = { | |
let c = CellD() | |
_ = c.borderWidth + 1 | |
} | |
measureExecutionTime(label: "Cell A", subject: cellA) | |
measureExecutionTime(label: "Cell B", subject: cellB) | |
measureExecutionTime(label: "Cell C", subject: cellC) | |
measureExecutionTime(label: "Cell D", subject: cellD) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment