Enviar pesquisa
Carregar
Deferred execution
•
Transferir como PPTX, PDF
•
0 gostou
•
701 visualizações
C
cuipengfei
Seguir
Deferred execution
Leia menos
Leia mais
Tecnologia
Denunciar
Compartilhar
Denunciar
Compartilhar
1 de 18
Baixar agora
Recomendados
《Python 3.5 技術手冊》第 3 章投影片
型態與運算子
型態與運算子
Justin Lin
認識內建型態 學習字串格式化 瞭解變數與運算子 運用切片運算
型態與運算子
型態與運算子
Justin Lin
認識基本流程語法 使用 for Comprehension 認識函式與變數範圍 運用一級函式特性 使用 yield 建立產生器 初探型態提示
流程語法與函式
流程語法與函式
Justin Lin
小米第二期scala交流演讲
Scala再探
Scala再探
afeihehe
《Python 3.5 技術手冊》第 14 章投影片
進階主題
進階主題
Justin Lin
08.第八章用Matlab求二元泰勒展开式
08.第八章用Matlab求二元泰勒展开式
Xin Zheng
《Python 3.5 技術手冊》第 5 章投影片
從模組到類別
從模組到類別
Justin Lin
認識 hashable、iterable、orderable 對物件進行排序 認識群集架構 運用 collections 模組 運用 collections.abc 模組
資料結構
資料結構
Justin Lin
Recomendados
《Python 3.5 技術手冊》第 3 章投影片
型態與運算子
型態與運算子
Justin Lin
認識內建型態 學習字串格式化 瞭解變數與運算子 運用切片運算
型態與運算子
型態與運算子
Justin Lin
認識基本流程語法 使用 for Comprehension 認識函式與變數範圍 運用一級函式特性 使用 yield 建立產生器 初探型態提示
流程語法與函式
流程語法與函式
Justin Lin
小米第二期scala交流演讲
Scala再探
Scala再探
afeihehe
《Python 3.5 技術手冊》第 14 章投影片
進階主題
進階主題
Justin Lin
08.第八章用Matlab求二元泰勒展开式
08.第八章用Matlab求二元泰勒展开式
Xin Zheng
《Python 3.5 技術手冊》第 5 章投影片
從模組到類別
從模組到類別
Justin Lin
認識 hashable、iterable、orderable 對物件進行排序 認識群集架構 運用 collections 模組 運用 collections.abc 模組
資料結構
資料結構
Justin Lin
Java tutorials - String
12, string
12, string
ted-xu
python 3
關於Import
關於Import
志宇 許
這是談論有關 CPython 運作時,背後的資料結構如何實做出來的。
C python 原始碼解析 投影片
C python 原始碼解析 投影片
kao kuo-tung
中山女高資訊研究社20th 教學用ppt
C++基礎程式設計 ch5 陣列
C++基礎程式設計 ch5 陣列
requiemformemories
中山女高資訊研究社20th 教學用ppt
C++基礎程式設計 ch2 變數與運算子
C++基礎程式設計 ch2 變數與運算子
requiemformemories
介绍苹果新编程语言 Swift 的一些特性
Swift 程序语言介绍
Swift 程序语言介绍
明 李
中山女高資訊研究社20th 教學用ppt
C++基礎程式設計 ch3 條件判斷
C++基礎程式設計 ch3 條件判斷
requiemformemories
Lesson 2 Basicstructure
Lesson 2 Basicstructure
Ryan Chung
定義結構, 巢狀結構, 結構陣列, 結構指標, 鏈結串列, 使用鏈結串, 實作堆疊, 使用鏈結串列實作佇列, 結構參數傳遞
C語言結構與串列
C語言結構與串列
吳錫修 (ShyiShiou Wu)
Javascript 培训第三节 基础下
Javascript 培训第三节 基础下
liziqi7
SCJP ch18
SCJP ch18
r82093403
js培训ppt,主要介绍javascript面向对象相关知识。
Javascript oop-o52tiger
Javascript oop-o52tiger
o52tiger
Presentatie tijdens workshop voor Vereniging Kleine LogiesVerstrekkers Texel. Specifiek gericht op starten met social media, vnml Facebook en Twitter
Texel 120115
Texel 120115
Hermannus Stegeman
Web 2.0 vodcast
Web 2.0 vodcast
onebel
presentatie voor workshop werkconferentie duurzame binnenstad, Utrecht Energie. Presentatie betreft het duurzaamheidsvignet kleine horeca
Khn werkconf-duurzamebinnenstad-120124
Khn werkconf-duurzamebinnenstad-120124
Hermannus Stegeman
Pragmatic+unit+testing+in+c%23+with+n unit%2 c+second+edition
Pragmatic+unit+testing+in+c%23+with+n unit%2 c+second+edition
cuipengfei
Thinking about your sales team's goals for 2017? Drift's VP of Sales shares 3 things you can do to improve conversion rates and drive more revenue. Read the full story on the Drift blog here: http://blog.drift.com/sales-team-tips
3 Things Every Sales Team Needs to Be Thinking About in 2017
3 Things Every Sales Team Needs to Be Thinking About in 2017
Drift
Are bloggers thought leaders? Here are some tips on how you can become one. Provide great value, put awesome content out there on a regular basis, and help others.
How to Become a Thought Leader in Your Niche
How to Become a Thought Leader in Your Niche
Leslie Samuel
Mais conteúdo relacionado
Mais procurados
Java tutorials - String
12, string
12, string
ted-xu
python 3
關於Import
關於Import
志宇 許
這是談論有關 CPython 運作時,背後的資料結構如何實做出來的。
C python 原始碼解析 投影片
C python 原始碼解析 投影片
kao kuo-tung
中山女高資訊研究社20th 教學用ppt
C++基礎程式設計 ch5 陣列
C++基礎程式設計 ch5 陣列
requiemformemories
中山女高資訊研究社20th 教學用ppt
C++基礎程式設計 ch2 變數與運算子
C++基礎程式設計 ch2 變數與運算子
requiemformemories
介绍苹果新编程语言 Swift 的一些特性
Swift 程序语言介绍
Swift 程序语言介绍
明 李
中山女高資訊研究社20th 教學用ppt
C++基礎程式設計 ch3 條件判斷
C++基礎程式設計 ch3 條件判斷
requiemformemories
Lesson 2 Basicstructure
Lesson 2 Basicstructure
Ryan Chung
定義結構, 巢狀結構, 結構陣列, 結構指標, 鏈結串列, 使用鏈結串, 實作堆疊, 使用鏈結串列實作佇列, 結構參數傳遞
C語言結構與串列
C語言結構與串列
吳錫修 (ShyiShiou Wu)
Javascript 培训第三节 基础下
Javascript 培训第三节 基础下
liziqi7
SCJP ch18
SCJP ch18
r82093403
js培训ppt,主要介绍javascript面向对象相关知识。
Javascript oop-o52tiger
Javascript oop-o52tiger
o52tiger
Mais procurados
(12)
12, string
12, string
關於Import
關於Import
C python 原始碼解析 投影片
C python 原始碼解析 投影片
C++基礎程式設計 ch5 陣列
C++基礎程式設計 ch5 陣列
C++基礎程式設計 ch2 變數與運算子
C++基礎程式設計 ch2 變數與運算子
Swift 程序语言介绍
Swift 程序语言介绍
C++基礎程式設計 ch3 條件判斷
C++基礎程式設計 ch3 條件判斷
Lesson 2 Basicstructure
Lesson 2 Basicstructure
C語言結構與串列
C語言結構與串列
Javascript 培训第三节 基础下
Javascript 培训第三节 基础下
SCJP ch18
SCJP ch18
Javascript oop-o52tiger
Javascript oop-o52tiger
Destaque
Presentatie tijdens workshop voor Vereniging Kleine LogiesVerstrekkers Texel. Specifiek gericht op starten met social media, vnml Facebook en Twitter
Texel 120115
Texel 120115
Hermannus Stegeman
Web 2.0 vodcast
Web 2.0 vodcast
onebel
presentatie voor workshop werkconferentie duurzame binnenstad, Utrecht Energie. Presentatie betreft het duurzaamheidsvignet kleine horeca
Khn werkconf-duurzamebinnenstad-120124
Khn werkconf-duurzamebinnenstad-120124
Hermannus Stegeman
Pragmatic+unit+testing+in+c%23+with+n unit%2 c+second+edition
Pragmatic+unit+testing+in+c%23+with+n unit%2 c+second+edition
cuipengfei
Thinking about your sales team's goals for 2017? Drift's VP of Sales shares 3 things you can do to improve conversion rates and drive more revenue. Read the full story on the Drift blog here: http://blog.drift.com/sales-team-tips
3 Things Every Sales Team Needs to Be Thinking About in 2017
3 Things Every Sales Team Needs to Be Thinking About in 2017
Drift
Are bloggers thought leaders? Here are some tips on how you can become one. Provide great value, put awesome content out there on a regular basis, and help others.
How to Become a Thought Leader in Your Niche
How to Become a Thought Leader in Your Niche
Leslie Samuel
Destaque
(6)
Texel 120115
Texel 120115
Web 2.0 vodcast
Web 2.0 vodcast
Khn werkconf-duurzamebinnenstad-120124
Khn werkconf-duurzamebinnenstad-120124
Pragmatic+unit+testing+in+c%23+with+n unit%2 c+second+edition
Pragmatic+unit+testing+in+c%23+with+n unit%2 c+second+edition
3 Things Every Sales Team Needs to Be Thinking About in 2017
3 Things Every Sales Team Needs to Be Thinking About in 2017
How to Become a Thought Leader in Your Niche
How to Become a Thought Leader in Your Niche
Deferred execution
1.
浅析延迟执行 实现,优点,陷阱以及题外话
2.
背景 假设一个场景: 我们需要获取并遍历一个包含大量元素的序列,从中找出我们需要的某个元素。
在此为了简单起见,我们假设该序列包含1000万个int值,我们需要找到的是100万这个值。
3.
第一类实现方式:使用已有类型 要从一个序列中过滤出一个值则首先需要生成这个序列。在此我们随意挑选三个序列类型: int[]
Collection<int> List<int>
4.
第二种方式:延迟执行 要实现延迟执行(Deferred Execution)有两种方式可选:
自己创建实现了IEnumerable<T>接口的类型 使用yield关键字 这两种方式具体是如何实现延迟执行的?我们稍后根据代码讲解。
5.
实现(1) 第一种方式中的三个已有类型的实现都相当简单,请看代码。 生成数组的代码:
生成Collection<int>的代码:
6.
实现(1) 生成List<int>的代码:
7.
实现(2) 自己创建实现了IEnumerable<T>的类型的方式是最复杂,最难写的。这个方式的基本思路就是构造一个状态机,由于要在多个状态之间做切换,所以很容易出错。请看代码: 由于代码过长,在此只给出代码结构图。这个类型同时实现了IEnumerable<T>和IEnumerator<T>两个接口。其MoveNext方法和Current属性每被调用一次时,才即时生成一个元素,这样就避免了一次性填充整个序列,从而实现了延迟执行。
8.
实现(3) 使用yield关键字是最简单,最偷懒的方式。 实际上,yield背后对应的实现和我们讲到的上一种方式基本是一样的。编译器会把包含yield的代码块构造成一个同时实现了IEnumerable<T>和IEnumerator<T>的类型。
9.
测试(1) 测试中有几个辅助方法(TestTime,IterateSequence和TestSpeed)需要简单说明,请看代码: TestTime的代码:
TestTime接受一个Action类型的参数,在方法体内执行action并为其计时,最后输出所耗时间。
10.
测试(1) IterateSequence的代码: 这段代码很简单:迭代一个序列,当找到要找的值之后则break出去。
11.
测试(1) TestSpeed的代码: 这段代码测试所有五种实现方式的效率。把创建序列和过滤序列的代码包裹在lambda表达式中传入TestTime。
12.
测试(2) 从1000万个元素中筛选 从1亿个元素中筛选
可以发现,当序列中元素数量增加时,前三种实现方式的耗时量都在呈线性增长。 而后两种实现方式的耗时量则基本没有变化。
13.
总结(1):延迟执行的好处 从前面的测试结果中可以看出,延迟执行的最明显的优势即在于不会立即创建整个序列,而是在调用方索取时才即时生成元素。 这也正好解释了为什么将序列容量从1000万增加为1亿时延迟执行的方式执行时间基本不变。因为延迟执行的方法总是只生成100万个元素而已。
14.
总结(2):可能的陷阱 由于用来生成序列的算法被封装在了状态机内,所以每次用foreach迭代这个序列时,整个序列都会被重新生成一次。 如果需要避免这种行为,可以通过在延迟执行的返回结果上调用ToArray()或ToList()。然后在每次迭代中都使用已经填充好的Array或List。
其实这种特性在有的场景下是很有益的,比如生成序列的算法依赖于某些外部的变化条件(数据库,网络数据或者系统时间)。
15.
题外话(1):foreach 我们每天都会用到的foreach究竟是如何实现的呢? 可以看出,一个foreach的“空转”循环基本等价于一个while循环加一个try/finally代码块。
请注意在finally代码块中调用了Dispose方法。如果这个foreach作用于一个延迟执行方法的返回值上,那么对Dispose的调用就相当于把状态机的状态清零。
16.
题外话(2):序列的重新生成 前面讲foreach的题外话其实是为了讲解序列的重新生成做基础。 前面已经讲过,foreach的尾部会调用迭代器的Dispose方法,把状态机的状态清零。这样,如果有下一个foreach来迭代同一个序列的话,则会将封装在状态机内的生成元素的算法重新执行一遍,也就相当于重新生成了整个序列。
这样说或许过于晦涩,请看下一页的图解。
17.
题外话(2):序列的重新生成 请看这把春田步枪,你装入子弹(调用GetEnumerator),撞针顶住了第一颗子弹(第一次调用MoveNext),开枪(访问Current属性),然后撞针顶住下一颗子弹(又一次调用MoveNext),反复开枪(反复调用MoveNext并访问Current属性),直到子弹耗尽(MoveNext返回了false),枪膛打开了(调用了Dispose)。然后再装入子弹开始下一轮的射击(序列的重新生成)。
18.
谢谢
Baixar agora