ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Lazy Stored Properties
    카테고리 없음 2020. 5. 6. 23:28

     

    A lazy stored property is a property whose initial value is not calculated until the first time it is used.

    You indicate a lazy stored property by writing the lazy modifier before its declaration.

    lazy 저장 프로퍼티의 값은 처음 사용되기 전까지는 계산되지 않습니다.

    lazy로 선언해주려면 "lazy var something " var(변수) 앞에 lazy를 선언해주면 됩니다.

     

     

    NOTE

    You must always declare a lazy property as a variable (with the var keyword), because its initial value might not be retrieved until after instance initialization completes. Constant properties must always have a value before initialization completes, and therefore cannot be declared as lazy.

     

    lazy는 반드시 항상 var로 선언을 해야한다. 그 이유는, lazy 변수는 초기화가 완료될 때 까지 변수의 값에 값을 찾을 수 없을지도 모른다고 합니다. 반면, constant 변수는 항상 초기화가 완료되기 전에 값을 가지고 있어야 된다고 합니다. 그래서 이 let의 특성은 lazy와 맞지 않는다고 합니다. 

     

    Lazy properties are useful when the initial value for a property is dependent on outside factors whose values are not known until after an instance’s initialization is complete.

    Lazy properties are also useful when the initial value for a property requires complex or computationally expensive setup that should not be performed unless or until it is needed.

     

    lazy 프로퍼티는 외부의 요소에 의존하는  즉, 초기화가 완료될 때 까지 알 수 없는 값에 의존하는 변수에 유용하다고 합니다. 

    또한, 변수에 복잡하고 시간이 많이 소요되는 computed 변수에 유용하다고 합니다.

     

     

    The example below uses a lazy stored property to avoid unnecessary initialization of a complex class.

    밑에 예제는 불필요한 복잡한(?) 클래스의 초기화를 피하기위해  lazy 변수를 사용합니다. 

     

     

    This example defines two classes called DataImporter and DataManager, neither of which is shown in full:

    1. DataImpoter class

    2. DataManager class

     

    The DataManager class has a stored property called data, which is initialized with a new, empty array of String values.

    DataManager클래스는 data라고 불리는 저장 프로퍼티를 가지고 있으며, 비어있는 String array로 초기화가 되어있습니다.

     

    Although the rest of its functionality is not shown, the purpose of this DataManager class is to manage and provide access to this array of String data.

     DataManager 클래스 존재의 목적은 data(string array)를 관리하기 위해 있다고 합니다.

     

    Part of the functionality of the DataManager class is the ability to import data from a file.

    DataManager의 기능중 하나는 파일로부터 데이터를 읽어오는 것이다.

     

    This functionality is provided by the DataImporter class, which is assumed to take a nontrivial amount of time to initialize.

    DataImporter에 의해서 임포트의 기능이 제공된다고 합니다. ( 초기화 하는데 약간의 시간이 걸린다는 가정)

     

    This might be because a DataImporter instance needs to open a file and read its contents into memory when the DataImporter instance is initialized.

     

    DataImporter 인스턴스가 초기화 될 떄, 해당 인스턴스에 오픈 할 파일, 파일 내 메모리에 컨텐츠를 가지고 있을지 모릅니다

     

    It is possible for a DataManager instance to manage its data without ever importing data from a file,

    so there is no need to create a new DataImporter instance when the DataManager itself is created.

    DataManager 인스턴스는 파일 임포팅 없이 데이터를 관리를 할 수 있습니다. 그래서 DataManager를 초기화

    할 때, DataImporter 인스턴스를 생성할 필요가 없습니다.

    Instead, it makes more sense to create the DataImporter instance if and when it is first used.

    대신, DataImporter 인스턴스가 처음 사용될 때, DataImporter의 인스턴스를 생성하는게 더

    이해가 될 것이다.

     

     

    아래 코드는 https://www.youtube.com/watch?v=xKoua1Mi6qE&t=189s

    Sean Allen

     

    위 유튜브를 보며 공부한 코드들이다. 

     

    gamePlayed를 lazy 저장 변수로 둔다면, Person객체를 생성할 때 마다 시간이 오래 걸리는 gamePlayed를 생성할 필요가 없습니다.

    print를 사용하여 .gamePlayed를 출력할 때 각각의 계산 프로퍼티만 계산하여 출력한다. 

    jordan,jordan1,jordan2 객체가 생성되고, 각각의 40,000번의 계산을 마치고 출력된다. 

     

    만약 , gamePlayed를 lazy로 선언하지 않는다면  3개의 Person 객체가 다 왼성 될 때 까지 즉, 120,000 번 실행 후 jordan,jordan1,jordan2가 순서대로 실행된다.

     

    즉 이 말을 이해하기 어렵겠지만,  computed 변수가 객체 생성시 필요 없을때 lazy로 선언하는게 좋다.

     

    위에서도 봤듯이 , lazy로 선언 하지 않았다면, 객체의 수 만큼 computed 연산을 하게되고  그 이후, 원하는 결과를 얻을 수 있기 때문에 비효율적이라고 생각이 듭니다. 

    그 대신 lazy를 사용하여 객체에 접근할 때만 연산이 일어나도록 하는게 더 효율적이라고 생각합니다. 

     

     

    설명이 아직 어리숙 하지만 고쳐 나가겠습니다.

     

     

     

Designed by Tistory.