网管联盟 | 网管论坛 | 网管u家 | 网管博客 | 网管软件 | 网管求职 | 小游戏 | 网管搜索 | 网管原创 | 网管聚合 | 网管读摘 | 网管焦点 | 世界素材 | 会员投稿 | 会员中心 
中国网管联盟
Windows Linux Cisco 网络技术 数据库 黑客攻防 DotNet Java PHP 认证 新闻资讯 服务器 存储资讯 网络设备 网管学堂 技术专题 焦点 网吧频道
 当前位置: > bitsCN.com > DotNet > VB.NET > 明确对比四种整数数据类型的性能  

明确对比四种整数数据类型的性能

2005-09-15  作者:BitsCN整理  来源:中国网管联盟  点评 投稿 收藏


  在我们写VBA程序的时候,我们经常要面对数据类型定义的选择,有的情况下,业务本身对于数据类型有要求和限制,那么我们并不难以选择,有些时候却没有限制,我们可以任意选用四种整数类型(Byte,Integer,Long,Currency)中的一种,例如:
  
  For i=1 to 100
  
  在这行代码中,我们该把变量i定义为什么类型的变量呢?显然四种整数类型都可以正常运行,但是他们的效率是否相同呢?我们到底该如何选择?有的人说,当时是选最小的数据类型Byte,有的人说在32位系统上,32位的Long类型才是效率最高的。
  
  那么究竟谁说的是正确的,让我们来进行一下这四种整数类型的性能对比测试,我们使用如下代码:
  
  Const LoopTimes = 100000000
  
  Public Sub test()
  Dim bytTmp As Byte
  Dim intTmp As Integer
  Dim lngTmp As Long
  Dim curTmp As Currency
  Dim loopCount As Long
  
  Dim timeBegin As Single
  Dim timeEnd As Single
  Dim timeAddition As Single
  
  timeBegin = Timer
  For loopCount = 0 To LoopTimes
  Next loopCount
  timeEnd = Timer 网管联盟bitsCN_com
  timeAddition = timeEnd - timeBegin
  
  timeBegin = Timer
  For loopCount = 0 To LoopTimes
  bytTmp = 255
  Next loopCount
  timeEnd = Timer
  Debug.Print "Byte :"; timeEnd - timeBegin - timeAddition; "秒"
  
  timeBegin = Timer
  For loopCount = 0 To LoopTimes
  intTmp = 255
  Next loopCount
  timeEnd = Timer
  Debug.Print "Integer :"; timeEnd - timeBegin - timeAddition; "秒"
  
  timeBegin = Timer
  For loopCount = 0 To LoopTimes
  lngTmp = 255
  Next loopCount
  timeEnd = Timer
  Debug.Print "Long :"; timeEnd - timeBegin - timeAddition; "秒"
  
  timeBegin = Timer
  For loopCount = 0 To LoopTimes
  curTmp = 255
  Next loopCount
  timeEnd = Timer
  Debug.Print "Currency :"; timeEnd - timeBegin - timeAddition; "秒"
  Debug.Print "*********************"
  
  End Sub
  
  在这里,我们对每个整数类型进行了1亿次的赋值操作,同时减去了循环控制所消耗的空转时间,剩下的就是纯粹的赋值操作所需的时间。最后,让我们来看看运行的结果: 网管有家bitscn.net
  
  Byte : 7.234375 秒
  Integer : 2.421875 秒
  Long : 3.4375 秒
  Currency : 4.84375 秒
  *********************
  Byte : 7.234375 秒
  Integer : 2.421875 秒
  Long : 3.453125 秒
  Currency : 4.875 秒
  *********************
  Byte : 7.21875 秒
  Integer : 2.421875 秒
  Long : 3.421875 秒
  Currency : 4.875 秒
  *********************
  看到这里,我想大家都应该很清楚了,虽然Byte占用内存最少,但是他的性能却是最差的,如果对于单个变量,我们没有必要使用Byte,当然Byte在大块数据段进行指针操作的时候,还是有他的非凡之处。剩下三种整数数据类型里面,Integer性能最佳,Currency性能最差。我们尽可能选择能够满足我们业务需要的最小数据类型。
  
  上面是赋值操作的性能对比,下面我们来进行位操作的性能对比测试,我们使用如下代码:
  
  Const LoopTimes = 10000000
  
  Public Sub test()
  Dim bytTmp As Byte
  Dim intTmp As Integer
  Dim lngTmp As Long
  Dim curTmp As Currency
  Dim strTmp As String 中国网管论坛bbs.bitsCN.com
  Dim loopCount As Long
  
  Dim timeBegin As Single
  Dim timeEnd As Single
  Dim timeAddition As Single
  
  timeBegin = Timer
  For loopCount = 0 To LoopTimes
  strTmp = 255
  Next loopCount
  timeEnd = Timer
  timeAddition = timeEnd - timeBegin
  
  timeBegin = Timer
  For loopCount = 0 To LoopTimes
  strTmp = bytTmp Or 255
  Next loopCount
  timeEnd = Timer
  Debug.Print "Byte :"; timeEnd - timeBegin - timeAddition; "秒"
  
  timeBegin = Timer
  For loopCount = 0 To LoopTimes
  strTmp = intTmp Or 255
  Next loopCount
  timeEnd = Timer
  Debug.Print "Integer :"; timeEnd - timeBegin - timeAddition; "秒"
  
  timeBegin = Timer
  For loopCount = 0 To LoopTimes
  strTmp = lngTmp Or 255
  Next loopCount
  timeEnd = Timer
  Debug.Print "Long :"; timeEnd - timeBegin - timeAddition; "秒"
  
  timeBegin = Timer
  For loopCount = 0 To LoopTimes 网管u家u.bitscn@com
  strTmp = curTmp Or 255
  Next loopCount
  timeEnd = Timer
  Debug.Print "Currency :"; timeEnd - timeBegin - timeAddition; "秒"
  Debug.Print "*********************"
  
  End Sub
  
  这里,我们所比较的是逐位或操作,同样我们扣除了循环控制时间,赋值时间,下面让我们来看看结果:
  
  Byte : .625 秒
  Integer : .296875 秒
  Long : .296875 秒
  Currency : .890625 秒
  *********************
  Byte : .609375 秒
  Integer : .34375 秒
  Long : .328125 秒
  Currency : .90625 秒
  *********************
  Byte : .484375 秒
  Integer : .265625 秒
  Long : .203125 秒
  Currency : .8125 秒
  *********************
  Byte : .53125 秒
  Integer : .328125 秒
  Long : .28125 秒
  Currency : .875 秒
  *********************
  
  我们可以看到,在位操作项目上,Byte赶上了Currency成了第三名,而Integer和Long则咬得很紧,但是最终还是Long胜出了,看来在32位系统上,32位数据类型确实有位操作上的优势,不要以为1字节位操作就会比4字节位操作快,事实上正好相反,4字节>2字节>1字节。
网管联盟bitsCN@com

  
  综合以上表现,我们的结论是,Byte和Currency的表现是最差的,但是这两个数据类型有他们的特殊用途,Byte适用于内存块的批量操作,Currency适用数据类型不确定的时候,剩下的Integer和Long,Integer在赋值操作上更快,Long在位操作上更快。
  
  现在你知道该如何选择整数数据类型了吗?
TAGs性能   类型   对比   明确   Timer   操作   NextloopCount    
 上一篇:解决VB.net中ReadProcessMemory的问题   下一篇:在Form中增加listbox同checkbox(图)
相关文章列表
明确对比四种整数数据类型的性能 评论:
loading.. 评论加载中…
评论:请自觉遵守互联网相关政策法规,评论不得超过250字。

验证码: 注册用户
本类热门排行:
1.VS.Net水晶报表实现方法
2.visual basic 6.0遍历文件夹下所有文件
3.vb.net 通过app.config来改变编译路径的
4.通过app.config来改变编译路径的问题
5.VB.NET中常量与枚举基础知识了解
6.VB.NET的字节校验例子
7.如何用Visual Basic编写病毒
8.vb.net中使用GetPrivateProfileString访
9.VB.net 学习设计模式(中介者模式)
最新推荐文章:
1.VB.NET实现窗体图标最小化到状态栏
2.如何应用VB.NET MonthCalendar控件
3.如何将一个VB.NET类分解成多个文件
4.VB.NET 拖动无边框窗体编程实例
5.VB.NET中如何扩充字符串进行固定宽度显示
6.VB.NET读取INI文件设置信息函数sdGetIniI
7.Visual Basic 10开启应用程序的新时代
8.VB.NET入门--Imports 语句
9.在Form中增加listbox同checkbox(图)
10.一步一步在VB.NET中使用抽象类
网管论坛交流:
·大家来开心一下吧---看了会很开心的东西-
·中国人不可不知道的知识
·@@小鹏◎◎小鹏同志与某位女明星亲密接触
·◎◎小鹏◎◎发现不明生物,居然正在交配
·[图文]^^^版主是什么?????
·泡论坛的女人是好女人
·做个“水性杨花”的女人
·献给mm俱乐部的所有mm
·深圳一集团企业电脑基础应用培训教程
·■■■■十一遊玩照■■■■■