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

推荐订阅源

AI
AI
WordPress大学
WordPress大学
博客园 - 聂微东
Hugging Face - Blog
Hugging Face - Blog
小众软件
小众软件
V
V2EX
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google DeepMind News
Google DeepMind News
V
Visual Studio Blog
The GitHub Blog
The GitHub Blog
IT之家
IT之家
D
Docker
M
MIT News - Artificial intelligence
D
DataBreaches.Net
博客园 - 三生石上(FineUI控件)
酷 壳 – CoolShell
酷 壳 – CoolShell
量子位
博客园_首页
Y
Y Combinator Blog
F
Full Disclosure
Microsoft Security Blog
Microsoft Security Blog
月光博客
月光博客
C
CXSECURITY Database RSS Feed - CXSecurity.com
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Spread Privacy
Spread Privacy
Know Your Adversary
Know Your Adversary
A
Arctic Wolf
Vercel News
Vercel News
T
Threat Research - Cisco Blogs
T
Threatpost
Apple Machine Learning Research
Apple Machine Learning Research
L
LINUX DO - 热门话题
T
The Exploit Database - CXSecurity.com
N
Netflix TechBlog - Medium
GbyAI
GbyAI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
T
Tailwind CSS Blog
J
Java Code Geeks
爱范儿
爱范儿
Cisco Talos Blog
Cisco Talos Blog
博客园 - 叶小钗
Latest news
Latest news
C
Check Point Blog
阮一峰的网络日志
阮一峰的网络日志
博客园 - Franky
P
Privacy International News Feed
MyScale Blog
MyScale Blog
Project Zero
Project Zero
Simon Willison's Weblog
Simon Willison's Weblog

博客园 - microsheen

[MDX学习笔记之五]优化Set操作——SUM中的CrossJoin [MDX学习笔记之四]Where vs. Subselect/Subcube [MDX学习笔记之三]MDX的上下文(Context) [MDX学习笔记之二]在MDX中处理边界情况 SQL Server 2005 SP2 CTP 在11月7号发布了 [MDX学习笔记之一]MDX中一些常见的计算 SSAS中Cube的结构 如何使用MDX编写同比和环比 SQL Server2005对XML的支持 2006 Tech.Ed 第二天见闻 2006 Tech.Ed 第一天见闻 C#简单命名指南 [.net泛型学习笔记之二]泛型的性能 [.net泛型学习笔记之一]泛型介绍 [设计模式学习笔记之二]设计模式和量体剪衣 Note Three: studying JavaScript: The Definitive Guide, 4th Edition Note Two : studying JavaScript: The Definitive Guide, 4th Edition Note One —— studying JavaScript: The Definitive Guide, 4th Edition [设计模式学习笔记之一]面向对象是什么?
[.net泛型学习笔记之三]再测泛型的性能
microsheen · 2006-05-26 · via 博客园 - microsheen

脚本的问题

前一篇文章的测试脚本中,发现一个问题,请见下面的粗体部分。

    public static double Test1()

    {   GC.Collect();

        ArrayList list1 = new ArrayList();

        sw.Reset();       //sw是一个System.Diagnostics.Stopwatch对象

        sw.Start();

        for (int i = 0; i < Program.MaxCount; i++)

        {   LargeDataStruct data1 = new LargeDataStruct();     

            list1.Add(data1);                                   //测试装箱

            LargeDataStruct data2 = (LargeDataStruct)list1[0];  //测试拆箱

        }

        sw.Stop();

        return sw.Elapsed.TotalSeconds;

}

由于在集合的Add方法里,要判断当前集合的数量是否达到了集合的最大容量(见下面的代码),如果达到了,需要做相关的操作,而这个操作可能花费较多的时间。

    //这是ArrayList的部分源代码

    public virtual int Add(object value)

    {

        if (this._size == this._items.Length)

        {

            this.EnsureCapacity(this._size + 1); //花费很多时间

        }

        this._items[this._size] = value;

        this._version++;

        return this._size++;

    }

    private void EnsureCapacity(int min)

    {

        if (this._items.Length < min)

        {

            int num1 = (this._items.Length == 0) ? 4 : (this._items.Length * 2);

            if (num1 < min)

            {

                num1 = min;

            }

            this.Capacity = num1;

        }

}

新的测试脚本 下载

为了保证测试的独立性,把测试代码改成(下面的粗体部分,保证了集合的容量始终够用):

    public static double Test1()

    {   GC.Collect();

        ArrayList list1 = new ArrayList(Program.MaxCount);

        sw.Reset();       //sw是一个System.Diagnostics.Stopwatch对象

        sw.Start();

        for (int i = 0; i < Program.MaxCount; i++)

        {   LargeDataStruct data1 = new LargeDataStruct();     

            list1.Add(data1);                                   //测试装箱

            LargeDataStruct data2 = (LargeDataStruct)list1[0];  //测试拆箱

        }

        sw.Stop();

        return sw.Elapsed.TotalSeconds;

}

测试结果 下载

P4 2.4GHz, 1G内存硬件,测试结果如下:

int in Arraylist

int in List<T>

large struct in ArrayList

large class in ArrayList

large struct in List<T>

large class in List<T>

string in ArrayList

string in List<T>

1000

0.0000949

0.0000387

0.001201

0.000836

0.000914

0.0009

0.00046

0.00042

10000

0.0006762

0.0002055

0.013376

0.008187

0.010115

0.0092

0.00427

0.00422

100000

0.007687

0.0017933

0.341669

0.30313

0.102465

0.3019

0.04894

0.04877

1000000

0.1269231

0.0186578

4.651395

4.524638

1.297351

4.5808

0.83491

0.83317

结果分析

l         避免装箱和拆箱测试

1.         int in Arraylist vs. int in List<T>
List<T>
ArrayList2-5倍。而且数据量越大,泛型的优势越大。

2.         Large struct in ArrayList vs. Large class in ArrayList
后者比前者快一些,提高幅度在10%-50%之间。但数据量越大,差距越小。

3.         Large struct in ArrayList vs. large struct in List<T>
List<T>
ArrayList30%-300%,数据量越大,泛型的优势越大。

l         类型检查是在编译时间进行,而不是在运行时间进行的

1.         custom class in ArrayList vs. custom class in List<T>  custom class large class
ArrayList
List<T>速度稍快,在10%范围以内,故总体相当。

2.         string in Arraylist vs. string in List<T>
List<T>
Arraylist速度稍快,在10%范围以内,故总体相当。

总结

对于泛型在的性能情况,可以得出以下结论:

1)  对于int这样的简单值类型,泛型能够提高2-5倍的速度。数据量越大,越明显。

2)  对于复杂的值类型,泛型能够提高30%-300%。数据量越大,越明显。

3)  对于引用(Reference)类型,泛型和传统的方式速度相当。

由上面的结论,可以看出,由于减少了装箱和拆箱,泛型对于值类型的对象性能提升明显。而在在编译时间进行类型检查的方式并没有带来性能的提升。另外由于值类型分配在堆栈之上,所以我们也能看出值类型的分配与创建过程比引用类型(分配在堆上)快得多。

今天的结论和前一篇文章的最大的不同在于,复杂值类型的测试结果迥异,但究竟原因是什么呢?一开始就初始化集合的容量真的对性能影响有这么大吗?泛型中的EnsureCapacity(int)方法为何比非泛型的EnsureCapacity(int)慢那么多,原因何在?