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
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億次的賦值操作,同時減去了循環控制所消耗的空轉時間,剩下的就是純粹的賦值操作所需的時間。最后,讓我們來看看運行的結果:
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
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
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字節。
綜合以上表現,我們的結論是,byte和currency的表現是最差的,但是這兩個數據類型有他們的特殊用途,byte適用于內存塊的批量操作,currency適用數據類型不確定的時候,剩下的integer和long,integer在賦值操作上更快,long在位操作上更快。
現在你知道該如何選擇整數數據類型了嗎?
新聞熱點
疑難解答