动态网站制作指南 [  QQ表情  ]
[ 投票调查 ]
[ 企业邮箱 ]
[ 网站空间 ]
网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
ASP源码 | .Net源码 | PHP源码 | JSP源码 | JAVA源码 | CGI源码 | VB源码 | C++源码 | Delphi源码 | PB源码 | VF源码 | 汇编 | 服务器
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > ASP.NET技巧
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
文章搜索服务
邮件订阅
输入你的邮件地址,
你将不会错过任何关于:
[ ASP.NET技巧 ]的信息

本月文章推荐
.怎样才能判断出用户上传的文件中.
.在VS2003中直接用DREAMWEAVER8打.
.ASP.NET服务器控件之视图状态.
.轻松加密ASP.NET 2.0 Web程序配置.
.通过System.Web.Mail程序发邮件.
.ASP.NET2.0服务器控件之捕获回传.
.如何在.NET中访问MySQL数据库.
.ASP.NET 2.0 中的 Theme 功能.
.过滤ASP.NET输出HTML中的无用空格.
.虚拟主机上asp.net运行权限不足问.
.数据绑定控件再ASP.NET1.X和ASP..
.结合JavaScript与ASP.NET Web窗体.
.asp.net 操作xml.
.ASP.NET 2.0高级数据处理之处理N.
.利用.net反射动态调用指定程序集.
.ASP.NET2.0的跨页回调.
.利用WebRequest来实现模拟浏览器.
.使用ASP.Net Forms模式实现WebSe.
.asp.net 2.0中gridview里嵌套dro.
.如何获取当前程序文件的路径 Cur.

值类型和引用类型在hashtable里面存取的性能比较

发表日期:2006-10-19 |


首先定义两个类:
 1    public interface ITest
 2    {
 3        void M();
 4    }
 5    public class Test1:ITest
 6    {
 7        public void M()
 8        {
 9        }
10    }
11   class Test
12        {
13            public Test()
14            {
15            }
16        } 首先,测试设置的速度hashtable.add()
 1static void Main(string[] args)
 2        {         
 3            Hashtable table = new Hashtable();
 4
 5            System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch();
 6            stopWatch.Start();
 7            for (int i = 0; i < CompareCount; i++)
 8            {
 9                table.Add(i,new Test());
10            }
11            stopWatch.Stop();
12           
13            for (int i = 0; i < CompareCount; i++)
14            {
15                Test o = table[i] as Test;
16            }
17          
18            string t1 = stopWatch.ElapsedTicks.ToString();
19         
20            Hashtable table1 = new Hashtable();
21            System.Diagnostics.Stopwatch stopWatch1 = new System.Diagnostics.Stopwatch();
22            stopWatch1.Start();
23            for (int i = 0; i < CompareCount; i++)
24            {
25                table1.Add(i, i);
26            }
27            stopWatch1.Stop();
28       
29            for (int i = 0; i < CompareCount; i++)
30            {
31                int o = (int)table1[i];
32            }
33          
34            string t2 = stopWatch1.ElapsedTicks.ToString();
35            Hashtable table2 = new Hashtable();
36            System.Diagnostics.Stopwatch stopWatch2 = new System.Diagnostics.Stopwatch();
37            stopWatch2.Start();
38            for (int i = 0; i < CompareCount; i++)
39            {
40                ITest test2 = new Test1();
41                table2.Add(i,test2);
42            }
43
44            stopWatch2.Stop();
45            for (int i = 0; i < CompareCount; i++)
46            {
47                ITest o = table2[i] as ITest;
48            }
49          
50            string t3 = stopWatch2.ElapsedTicks.ToString();
51            Console.WriteLine(t1);
52            Console.WriteLine(t2);
53            Console.WriteLine(t3);
54            Console.WriteLine(((double)Convert.ToInt64(t1)/Convert.ToInt64(t2)).ToString());
55            Console.WriteLine(((double)Convert.ToInt64(t3) / Convert.ToInt64(t2)).ToString());
56            Console.Read();
57           
58        }测试获取的代码
 1static void Main(string[] args)
 2        {         
 3            Hashtable table = new Hashtable();
 4        
 5            for (int i = 0; i < CompareCount; i++)
 6            {
 7                table.Add(i,new Test());
 8            }
 9           
10            System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch();
11            stopWatch.Start();
12            for (int i = 0; i < CompareCount; i++)
13            {
14                Test o = table[i] as Test;
15            }
16            stopWatch.Stop();
17            string t1 = stopWatch.ElapsedTicks.ToString();
18         
19            Hashtable table1 = new Hashtable();
20          
21            for (int i = 0; i < CompareCount; i++)
22            {
23                table1.Add(i, i);
24            }
25           
26            System.Diagnostics.Stopwatch stopWatch1 = new System.Diagnostics.Stopwatch();
27            stopWatch1.Start();
28            for (int i = 0; i < CompareCount; i++)
29            {
30                int o = (int)table1[i];
31            }
32            stopWatch1.Stop();
33            string t2 = stopWatch1.ElapsedTicks.ToString();
34            Hashtable table2 = new Hashtable();
35           
36            for (int i = 0; i < CompareCount; i++)
37            {
38                ITest test2 = new Test1();
39                table2.Add(i,test2);
40            }
41           
42            System.Diagnostics.Stopwatch stopWatch2 = new System.Diagnostics.Stopwatch();
43            stopWatch2.Start();
44            for (int i = 0; i < CompareCount; i++)
45            {
46                ITest o = table2[i] as ITest;
47            }
48            stopWatch2.Stop();
49            string t3 = stopWatch2.ElapsedTicks.ToString();
50            Console.WriteLine(t1);
51            Console.WriteLine(t2);
52            Console.WriteLine(t3);
53            Console.WriteLine(((double)Convert.ToInt64(t1)/Convert.ToInt64(t2)).ToString());
54            Console.WriteLine(((double)Convert.ToInt64(t3) / Convert.ToInt64(t2)).ToString());
55            Console.Read();
56           
57        }

测试结果 

Add

1)  调试(1)

数据类型

循环次数

执行时间

执行时间比例

int

100000

227960

1

class

100000

138122

0.6059

Interface

100000

103693

0.4549

 





 
调试(2

数据类型

循环次数

执行时间

执行时间比例

int

100000

282564

1

class

100000

156588

0.5542

Interface

100000

148623

0.5230






2) 

   运行(1)

数据类型

循环次数

执行时间

执行时间比例

int

100000

155927

1

class

100000

191537

1.2284

Interface

100000

127647

0.8186

    





 
运行(2)   

数据类型

循环次数

执行时间

执行时间比例

int

100000

151806

1

class

100000

222375

1.4649

Interface

100000

256467

1.6894

   





运行
(3)

数据类型

循环次数

执行时间

执行时间比例

int

100000

99465

1

class

100000

235016

2.3628

Interface

100000

201519

2.0260

 

 





从上面几个表可以得出,在向
Hashtable里面添加数据的时候,当value为值类型的时候最快,interface次之,class

 

2. 查询

  

1.       调试(1)

数据类型

循环次数

执行时间

执行时间比例

int

100000

52360

1

class

100000

71250

1.3608

Interface

100000

291566

5.5685

 

2.      



调试
(2)

数据类型

循环次数

执行时间

执行时间比例

int

100000

53645

1

class

100000

55679

1.0379

Interface

100000

310780

5.7932

 





  运行
(1)

数据类型

循环次数

执行时间

执行时间比例

int

100000

53013

1