C List 與Array效能比較

2022-02-24 14:02:18 字數 3643 閱讀 8450

在.net framework 2.0之前,陣列表示可以用array 和集合類 arraylist 表示,2.0後,.net 引進了泛型的概念list<>,那麼我們的選擇就多了一種。

2.0引進泛型其本意是減少型別的裝箱拆箱所帶來的效能消耗。

比如;private void compareto(listlist)

這裡的list就是泛型,呼叫時我不知道要傳入什麼型別,有可能是int,string或class.

今天主要討論的是在型別確定的情況下,哪種效能高一些,arraylist就不用考慮了,它肯定是最慢的,它只能add object type的.這裡主要討論list<>和array的效能:

先看string的情況:

static void main(string args)

",count);

console.write("the value of total for array:");

stopwatch stopwatch = new stopwatch();

stopwatch.start();

for (int i = 0; i < count; i++)

int thearraytotal = 0;

for (int i = 0; i < count; i++)

stopwatch.stop();

console.write(thearraytotal);

console.writeline();

console.write("array init time:");

console.write(stopwatch.elapsedmilliseconds.tostring());

stopwatch.reset();

stopwatch.start();

console.writeline();

for (int i = 0; i < count; i++)

int thelisttotal = 0;

foreach (string v in list)

stopwatch.stop();

console.writeline("the value of total for list:", thelisttotal);

console.write("list init time:");

console.write(stopwatch.elapsedmilliseconds.tostring());

stopwatch.reset();

console.read();

當count=1000時,兩者看不出差別,用的time都是0

當count=10000時,也沒什麼區別

當count=100000時,

the count is:100000

the value of total for array:0

array init time:16

the value of total for list:0

list init time:21

aarry 比list快了5

當count=1000000時

the count is:1000000

the value of total for array:0

array init time:296

the value of total for list:0

list init time:320

arry比list快了24

當string時,arry是要比list快

當用int類時:

static void main(string args)

",count);

console.write("the value of total for array:");

stopwatch stopwatch = new stopwatch();

stopwatch.start();

for (int i = 0; i < count; i++)

int thearraytotal = 0;

for (int i = 0; i < count; i++)

stopwatch.stop();

console.write(thearraytotal);

console.writeline();

console.write("array init time:");

console.write(stopwatch.elapsedmilliseconds.tostring());

stopwatch.reset();

stopwatch.start();

console.writeline();

for (int i = 0; i < count; i++)

int thelisttotal = 0;

foreach (int v in list)

stopwatch.stop();

console.writeline("the value of total for list:", thelisttotal);

console.write("list init time:");

console.write(stopwatch.elapsedmilliseconds.tostring());

stopwatch.reset();

console.read();

}修改count=1000,10000,100000,1000000依次輸入為:

the count is:1000

the value of total for array:0

array init time:0

the value of total for list:0

list init time:0

the count is:10000

the value of total for array:0

array init time:0

the value of total for list:0

list init time:0

the count is:100000

the value of total for array:0

array init time:0

the value of total for list:0

list init time:1

the count is:1000000

the value of total for array:0

array init time:7

the value of total for list:0

list init time:17

the count is:10000000

the value of total for array:0

array init time:77

the value of total for list:0

list init time:218

(不同的硬體配置會有不同的結果)

從上面的結果來看array的效率要比list的要高一些,當陣列長度不是很大時,兩者沒什麼區別,建議用list<>,畢竟是可變長度,可以add;特殊應用還是建議用array,

不提倡用arraylist.

srand 與 rand效能比較

include include include int main gettimeofday tv,null long us1 tv.tv usec std cout 測試1結束,時間戳 us1 時間差 us1 beginus std endl for int i 0 i 1000 i gettime...

redis memcache 效能比較

from redis和memcache非常像的,都是key,value的方式,將資料存放記憶體中。最近在學習redis,在網上看了一些這方面的資料,有三種觀點 1,redis讀寫記憶體比memcache快 2,memcache讀寫記憶體比redis快 3,memcache讀寫記憶體比redis快,但...

apache resin ngnix 效能比較

靜態頁面測試,靜態頁面包括css,js,img ab c 20 n 30 併發測試 高數值的也測過,結論一致 resin2.17 平均響應時間為0.521ms nginx 0.7.65 平均響應時間為1.042ms 少許幾次小於1.042 apache2.0 平均響應時間為最低1.042ms 在re...