SlideShare una empresa de Scribd logo
1 de 58
Deferred Execution (延遲執行)
Jonathan, Titan
2013.07.01
延遲執行 = 執行被延遲
Execution is been deferred
一般經驗,程式跑到哪,
運算式會立刻被執行
一般執行的情形
Deferred Execution - 取值時才執行
對應的運算式
拿到一張票 (ticket),未來可以憑票取值
延遲執行案例
運算式什麼時候被執行?
真正需要值的時候 (ex: foreach)
Iterator 是延遲執行的基礎
Iterator Review
Iterator 背後相關的 interface 是?
典型的 Iterator 實作
Iterator 執行歷程 Trace
Console:
Iterator 執行歷程 Trace
1. 執行取值動作
Console:
Iterator 執行歷程 Trace
1. 執行取值動作
Console:
Iterator - Value=0
Iterator 執行歷程 Trace
1. 執行取值動作
執行到 yield,狀態停止
Console:
Iterator - Value=0
Iterator 執行歷程 Trace
執行到 yield,狀態停止
值 = 0
Console:
Iterator - Value=0
foreach - Value=0
Iterator 執行歷程 Trace
Console:
Iterator - Value=0
foreach - Value=0
Iterator - Value=1
2. 執行取值動作
Iterator 執行歷程 Trace
執行到 yield,狀態停止
Console:
Iterator - Value=0
foreach - Value=0
Iterator - Value=1
2. 執行取值動作
Iterator 執行歷程 Trace
執行到 yield,狀態停止
值 = 1
Console:
Iterator - Value=0
foreach - Value=0
Iterator - Value=1
foreach - Value=1
Deferred Execution (延遲執行) 取值
的方式也分兩種
Lazy Evaluation
Eager Evaluation
Lazy Evaluation (惰性取值)
每次需要一個值時,做一次取值的動作
Lazy Evaluation
每個元素被需要時,才做計算求 值
Eager Evaluation (熱情取值)
第一次取值時,把未來可能會用到的值先算好
Eager Evaluation
Eager Evaluation
在第一個值被需要時,將其他元素
值一併計算準備好
Eager Evaluation
第一次進入,value = 0
Eager Evaluation
第一次進入,value = 0
Eager Evaluation
Eager Evaluation
Eager Evaluation
Eager Evaluation
Eager Evaluation
第二次進入,value = 1
Eager Evaluation
第二次進入,value = 1
Recap
■ 延遲執行的原理
■ 延遲執行下的 Lazy Evaluation 與 Eager Evaluation
■ Lazy = 服務生每次來要飲料,才做一杯給他
■ Eager = 第一次有顧客來要飲料時,就做好 10 杯等
著,未來慢慢發
為什麼要使用 Deferred Execution?
■ 想像如果我們要讀取一個 100MB 的文字檔
效能與資源
■ 想像如果我們要讀取一個 100MB 的文字檔
效能與資源
語法上的彈性
■ 允分發揮 influent interface 的特性
Deferred Execution
在 LINQ 中的應用
Sample Code
Name="John", Age=36
Name="Steve", Age=23
我們原本以為程式會這樣跑......
IEnumerable<Person>
.Where(person =>
person.Age > 18)
.Select(person =>
person.Name)
所有 Person 物件
Name="John", Age=36
Name="Bill", Age=6
Name="Steve", Age=23
18 歲以上的 Person
18 歲以上的人的姓名 "John"
"Steve"
代誌不是大家想得那麼簡單!
LINQ 延遲執行的 Sequence Diagram
LINQ 延遲執行的 Sequence Diagram
LINQ 延遲執行的 Sequence Diagram
LINQ 延遲執行的 Sequence Diagram
LINQ 延遲執行的 Sequence Diagram
LINQ 延遲執行的 Sequence Diagram
LINQ 延遲執行的 Sequence Diagram
LINQ 延遲執行的 Sequence Diagram
LINQ 延遲執行的 Sequence Diagram
LINQ 延遲執行的 Sequence Diagram
LINQ 延遲執行的 Sequence Diagram
事情可能會更複雜!
加上 Closure 會如何?
延遲執行 + Closure
加入 Closure 後的執行時序
什麼情形下延遲執行不會發生?
■ 回傳值會做轉型
■ ToList()
■ ToArray()
■ ToDictionary()
■ ......
■ 回傳值是一個單值
■ Count()
■ Max()
■ Min()
■ First()
■ FirstOrDefault()
■ ......
Thank You!

Más contenido relacionado

Destacado

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 

Destacado (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Deferred Execution (延遲執行)