yield

C# yield return performance

寵の児 提交于 2019-12-04 17:06:29
How much space is reserved to the underlying collection behind a method using yield return syntax WHEN I PERFORM a ToList() on it? There's a chance it will reallocate and thus decrease performance if compared to the standard approach where i create a list with predefined capacity? The two scenarios: public IEnumerable<T> GetList1() { foreach( var item in collection ) yield return item.Property; } public IEnumerable<T> GetList2() { List<T> outputList = new List<T>( collection.Count() ); foreach( var item in collection ) outputList.Add( item.Property ); return outputList; } yield return does not

Concurrency or Performance Benefits of yield return over returning a list

折月煮酒 提交于 2019-12-04 15:57:03
问题 I was wondering if there is any concurrency (now or future), or performance benefit to using yield return over returning a list. See the following examples Processing Method void Page_Load() { foreach(var item in GetPostedItems()) Process(item); } using yield return IEnumerable<string> GetPostedItems() { yield return Item1.Text; yield return Item2.Text; yield return Item3.Text; } returning a list IEnumerable<string> GetPostedItems() { var list = new List<string>(); list.Add(Item1.Text); list

What is yield and what is the benefit to use yield in asp .NET?

我的未来我决定 提交于 2019-12-04 15:53:39
问题 Can you help me in understanding of yield keyword in asp .NET(C#) . 回答1: Yield return automatically creates an enumerator for you. http://msdn.microsoft.com/en-us/library/9k7k7cf0.aspx So you can do something like //pseudo code: while(get_next_record_from_database) { yield return your_next_record; } It allows you to quickly create an object collection (an Enumerator) that you can loop through and retrieve records. The yield return statement handles all the of the code needed to create an

PHP的yield是个什么玩意

爱⌒轻易说出口 提交于 2019-12-04 15:19:19
来源: https://segmentfault.com/a/1190000018457194 其实,我并不是因为迭代或者生成器或者研究PHP手册才认识的yield,要不是协程,我到现在也不知道PHP中还有yield这么个鬼东西。人家这个东西是从PHP 5.5就开始引入了,官方名称叫做生成器。你要说为什么5.5年代的东西,现在才拿出来。我还想问你哟,PHP 5.3就有了的namespace为毛到最近这几年才开始正式投产。 那么,问题来了,这东西到底是有何用? 先来感受一个问题,给你100Kb的内存(是的,你没有看错,就是100Kb),然后让你迭代输出一个从1开始一直到10000的数组,步进为1。 愈先迭代数组,必先创造数组。 所以,脑门一拍,代码一坨如下: <?php $start_mem = memory_get_usage(); $arr = range( 1, 10000 ); foreach( $arr as $item ){ //echo $item.','; } $end_mem = memory_get_usage(); echo " use mem : ". ( $end_mem - $start_mem ) .'bytes'.PHP_EOL; 一顿操作猛如虎,运行一下成绩1-5,你们感受一下: 528440bytes,约莫就是528Kb,几乎是100Kb的五倍了

生成器

巧了我就是萌 提交于 2019-12-04 13:32:58
生成器 什么是生成器: ​ 生成的工具 ​ 生成器是一个“自定义”的迭代器, 本质上是一个迭代器 如何实现生成器? ​ 但凡函数内部定义了的yield ,调用函数时,函数体代码不会执行 ​ 会返回一个结果,该结果就是一个生成器 yield: ​ 每一次yield 都会往生成器对象中添加一个值 - yield 只能在函数内部定义 - yield 可以保存函数的暂停状态 yield 与 return : 相同点: ​ 返回的个数都是无限制的 不同点: ​ return 只能返回一次值,yield可以返回多次 自定义range功能,创建一个自定义的生成器 def my_range(start, end, move = 1): while start < end: yield start start += move for i in my_range(1,5) print(i) 来源: https://www.cnblogs.com/127-2933/p/11867952.html

PHP 7 值得期待的新特性(下)

我与影子孤独终老i 提交于 2019-12-04 13:22:14
这是我们期待已久的 PHP 7 系列文章的第二篇。点此阅读 第一篇 本文系 OneAPM 工程师编译整理。 也许你已经知道,重头戏 PHP 7 的发布将在今年到来!现在,让我们来了解一下,新版本有哪些新功能与改进。 在本系列的 第一篇 ,我们介绍了 PHP 7 中最重要的一些不兼容性修复以及两大新特性。在本文中,我们将了解 PHP 7 的另外六大功能。 Unicode 代码点转义语法 新增加的转义字符—— \u,允许我们在 PHP 字符串内明确指定 Unicode 字符代码点(以十六进制): 此处使用的语法为 \u{CODEPOINT} 。例如这个绿色的心形,💚, 可以表示为 PHP 字符串 "\u{1F49A}" 。 ##Null 合并操作符 另一个新的操作符—— Null 合并操作符 ?? ,其实是传说中的三目运算符 。如果它不是 Null ,将返回左操作数,否则返回右操作数。 重点在于,如果左操作数是一个不存在的变量,也不会引起注意。这就像 isset() ,而不像 ?: 短三目运算符。 你还可以链接该操作符,从而返回给定集合的第一个非 null 值。 $config = $config ?? $this->config ?? static::$defaultConfig; ##调用之上绑定闭包 之前,在 PHP 5.4 添加的 Closure->bindTo() 与

How does 'yield' work in this permutation generator?

前提是你 提交于 2019-12-04 12:12:46
def perm_generator(lst): if len(lst) == 1: yield lst else: for i in range(len(lst)): for perm in perm_generator(lst[:i] + lst[i+1:]): yield [lst[i]] + perm This code has been bugging me, since I don't understand how the yield s connect to each other. My understanding was that yield acts like a return , but it stops temporarily until it is called again. How do these yield s work? It might help seeing a version that doesn't use generators: def perm_generator(lst): res = [] if len(lst) == 1: return [lst] else: for i in range(len(lst)): for perm in perm_generator(lst[:i] + lst[i+1:]): res.append(

生成器

家住魔仙堡 提交于 2019-12-04 12:00:48
生成器 一、yield关键字 yield的英文单词意思是生产,在函数中但凡出现yield关键字,再调用函数,就不会继续执行函数体代码,而是会返回一个值。 def func(): print(1) yield print(2) yield g = func() print(g) <generator object func at 0x10ddb6b48> 生成器的本质就是迭代器,同时也并不仅仅是迭代器,不过迭代器之外的用途实在是不多,所以我们可以大声地说:生成器提供了非常方便的自定义迭代器的途径。并且从Python 2.5+开始,[PEP 342:通过增强生成器实现协同程序]的实现为生成器加入了更多的特性,这意味着生成器还可以完成更多的工作。这部分我们会在稍后的部分介绍。 def func(): print('from func 1') yield 'a' print('from func 2') yield 'b' g = func() print(F"g.__iter__ == g: {g.__iter__() == g}") res1 = g.__next__() print(f"res1: {res1}") res2 = next(g) print(f"res2: {res2}") # next(g) # StopIteration g.__iter__ == g: True

Use Nightmare.js without ES6 syntax and yield

此生再无相见时 提交于 2019-12-04 11:54:54
问题 I built a simple node script using nightmare.js to scrape websites var Nightmare = require('nightmare'); var vo = require('vo'); vo(run)(function(err, result) { if (err) throw err; }); function *run() { var x = Date.now(); var nightmare = Nightmare(); var html = yield nightmare .goto('http://google.com') .evaluate(function() { return document.getElementsByTagName('html')[0].innerHTML; }); console.log("done in " + (Date.now()-x) + "ms"); console.log("result", html); yield nightmare.end(); } I

python基础-生成器

隐身守侯 提交于 2019-12-04 11:24:57
生成器 概念 :但凡在函数内部定义了一个yield, 调用函数时,函数体代码不会执行 ,会返回一个结果,该结果就是生成器。本质上是迭代器,一个 自定义的迭代器 。 # python内获取迭代器的方式 def iter_func(): list1 = [1, 2, 3, 8, 4] # 获取一个迭代器 iter_list = list1.__iter__() while True: try: # 捕获异常 # 执行__next__取值 print(iter_list.__next__()) except StopIteration: break # 执行iter_func iter_func() # 自定义一个迭代器 def func(): print("hello world!") yield "dawn" res = func() # 直接调用,返回了一个生成器:<generator object func at 0x000002376172F1C8> print(res) # 如何获取生成器的内容呢? # 生成器的本质是迭代器,迭代器对象取值通过.__next__ # 通过__next__ 取值时,才会执行函数体代码。 print(res.__next__()) 输出结果: hello world! dawn 生成器如何实现:通过 yield 关键字实现。 yield