惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

GbyAI
GbyAI
博客园_首页
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
阮一峰的网络日志
阮一峰的网络日志
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 司徒正美
V
V2EX
Cloudbric
Cloudbric
Hugging Face - Blog
Hugging Face - Blog
腾讯CDC
量子位
博客园 - 三生石上(FineUI控件)
博客园 - 叶小钗
K
Kaspersky official blog
博客园 - 【当耐特】
T
Tenable Blog
L
Lohrmann on Cybersecurity
The Cloudflare Blog
S
Schneier on Security
A
Arctic Wolf
Latest news
Latest news
C
Cyber Attacks, Cyber Crime and Cyber Security
罗磊的独立博客
T
The Exploit Database - CXSecurity.com
Cisco Talos Blog
Cisco Talos Blog
小众软件
小众软件
P
Privacy & Cybersecurity Law Blog
WordPress大学
WordPress大学
Simon Willison's Weblog
Simon Willison's Weblog
雷峰网
雷峰网
NISL@THU
NISL@THU
人人都是产品经理
人人都是产品经理
月光博客
月光博客
J
Java Code Geeks
V
Visual Studio Blog
S
Security Affairs
博客园 - Franky
T
Tailwind CSS Blog
Apple Machine Learning Research
Apple Machine Learning Research
H
Heimdal Security Blog
有赞技术团队
有赞技术团队
V2EX - 技术
V2EX - 技术
AWS News Blog
AWS News Blog
G
GRAHAM CLULEY
T
Troy Hunt's Blog
SecWiki News
SecWiki News
Spread Privacy
Spread Privacy
宝玉的分享
宝玉的分享
www.infosecurity-magazine.com
www.infosecurity-magazine.com
博客园 - 聂微东

博客园 - Jeffrey Sun

Composite Application Guidance (Prism) V4 Releases! 关于文件流Seek以及Read操作的一点不满 VS2010 "内存不足" 错误补丁 微软Visual Studio 2010 第三集:幸福要敏捷 详解.NET 4.0平行任务库中必须注意的三种关系 山雨欲来风满楼, 却待山花烂漫时 - 写在.NET 4.0和Visual Studio 2010发布前夜 - Jeffrey Sun [转载]微软4月13日发布Silverlight 4 团队基础生成自动化流程之最佳实践(VI) - 系统模块化条件编译 团队基础生成自动化流程之最佳实践(V) - 使用Desktop Build 团队基础生成自动化流程之最佳实践(IV) - 重写团队基础默认生成流程 谁是你的下一行CODE 团队基础生成自动化流程之最佳实践总论(III) – 重写生成号产生机制 团队基础生成自动化流程之最佳实践总论(I) – 自动化生成流程基础 团队基础生成自动化流程之最佳实践总论(II) – 程序集版本信息 Memory Reordering/Memory Model 及其对.NET的影响 从DWG到XAML (III) – .NET中的 XPS Packaging类库及一个DWFx Packaging类库的实现 (源码) 从DWG到XAML (II) - DWFx格式解析及其和XPS的关系 从DWG到XAML (I) - 浅谈DWG历史,现状及方向 - Jeffrey Sun 当心Dictionary带来的一种隐式内存泄漏
关于Duck Typing的性能分析 - Draft
Jeffrey Sun · 2011-07-04 · via 博客园 - Jeffrey Sun

using System;
using System.Threading;
using System.Collections.Generic;
using System.Diagnostics;namespace DuckTyping
{
class Program
{
public static int Range = 10000000;static void Main(string[] args)
{
string newRange = Console.ReadLine();
if (!string.IsNullOrEmpty(newRange))
{
Range
= Int32.Parse(newRange);
}

FakeEnumerable fake

= new FakeEnumerable();
RealEnumerable real
= new RealEnumerable();
YieldEnumerable
yield = new YieldEnumerable();

Stopwatch sw

= new Stopwatch();
sw.Start();
foreach (var item in fake)
{
}

sw.Stop();
Console.WriteLine(

"Duck Typing : {0}",sw.ElapsedMilliseconds);
sw.Reset();
sw.Start();
foreach (var item in real)
{
}

sw.Stop();
Console.WriteLine(

"Interface compl: {0}", sw.ElapsedMilliseconds); sw.Reset();
sw.Reset();
sw.Start();
foreach (var item in yield)
{
}

sw.Stop();
Console.WriteLine(

"Yield compl: {0}", sw.ElapsedMilliseconds);

Console.ReadKey();
}
}

public class FakeEnumerable
{
public FakeEnumerator GetEnumerator()
{
return new FakeEnumerator();
}
}
public class FakeEnumerator
{
public FakeEnumerator()
{
this.Current = 1;
}
public int Current {get;set;}public bool MoveNext()
{
Current
++;
return Current < Program.Range;
}
}
public class RealEnumerable : IEnumerable<int>
{
public IEnumerator<int> GetEnumerator()
{
return new RealEnumerator();
}

System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{

throw new NotImplementedException();
}
}
public class RealEnumerator : IEnumerator<int>
{
private int current;public RealEnumerator()
{
this.current = 0;
}
public int Current
{
get { return this.current; }
}
public void Dispose()
{
}
object System.Collections.IEnumerator.Current
{
get { return this.current; }
}
public bool MoveNext()
{
current
++;
return current < Program.Range;
}
public void Reset()
{
throw new NotImplementedException();
}
}
public class YieldEnumerable : IEnumerable<int>
{
private static int index = 0;public IEnumerator<int> GetEnumerator()
{
while (index < Program.Range)
yield return index++;
}

System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{

throw new NotImplementedException();
}
}
}