Which is more efficient: Creating a "var" and re-using it, or creating several "let"s?

Viewed 378

Just curious which is more efficient/better in swift:

  • Creating three temporary constants (using let) and using those constants to define other variables
  • Creating one temporary variable (using var) and using that variable to hold three different values which will then be used to define other variables

This is perhaps better explained through an example:

var one = Object()
var two = Object()
var three = Object()

func firstFunction() {
    let tempVar1 = //calculation1
    one = tempVar1

    let tempVar2 = //calculation2
    two = tempVar2

    let tempVar3 = //calculation3
    three = tempVar3

}

func seconFunction() {
    var tempVar = //calculation1
        one = tempVar

    tempVar = //calculation2
        two = tempVar

    tempVar = //calculation3
        three = tempVar

}

Which of the two functions is more efficient? Thank you for your time!

3 Answers
Related