Created
August 9, 2016 23:57
-
-
Save thenickreynolds/228e8cee7f133d6521ed692156038654 to your computer and use it in GitHub Desktop.
Slow Compilation Examples
This file contains hidden or 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
// Code snippet 1 | |
func test() { | |
let f: Int = 5 | |
// Original: | |
CGPoint(x: f + f * (f + f) + f * f, y: 1) // (1.5786 sec) | |
// Faster: | |
let x = f + f * (f + f) + f * f; let y = 1; CGPoint(x: x, y: y) // put in variables (0.0066 sec) | |
CGPoint(x: f + f * (f + f) + f * f, y: CGFloat(1)) // hint to compiler to expect a CGFloat by casting the y parameter (0.0089 sec) | |
CGPoint(x: f + f * (f + f) + f * f, y: f) // same as above but with variable (0.0063 sec) | |
CGPoint(x: /*f + */f * (f + f) + f * f, y: 1) // get rid of one of the expressions (0.1 sec) | |
CGPoint(x: f + 3 * f * f, y: 1) // (0.0245 sec) | |
CGPoint(x: f + f * (f/* + f*/) + f * f, y: 1) // get rid of subexpression (0.3 sec) | |
CGPoint(x: f * f * (f * f) * f * f, y: 1) // all multiplications (0.0304 sec) | |
// Slower: | |
CGPoint(x: f + f + (f + f) + f + f, y: 1) // all plusses (4.0897 sec) | |
CGPoint(x: f + f * f + f * f + f * f, y: 1) // (4.2004 sec) | |
CGPoint(x: 1 + 1 * (1 + 1) + 1 * 1, y: 1) // all literals (4.9021 sec) | |
} | |
// Code snippet 2 | |
func test() -> Double { // 1502.4ms | |
let a = 1.0 | |
let b = 3.0 | |
let c = 5.0 | |
let d = 6.0 | |
return a - b + c + ceil(d * 0.55) + 2.0 | |
} | |
// Code snippet 3 | |
func test() -> Double { // 0.7ms | |
let a = 1.0 | |
let b = 3.0 | |
let c = 5.0 | |
let d = 6.0 | |
let e = ceil(d * 0.55) | |
return a - b + c + e + 2.0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment