asp.net C# 中的 static 關鍵字(1/3)

來源:互聯網
上載者:User

01:  /// <summary>
02:  /// 使用位元組數組中的值初始 zipinteger 結構的新執行個體
03:  /// 注意:本建構函式會破壞傳入的 bits 參數的值。
04:  /// </summary>
05:  /// <param name="bits">順序為 big-endian 的位元組值的數組</param>
06:  public zipinteger(byte[] bits)
07:  {
08:    if (bits == null) throw new argumentnullexception("bits");
09:    if (bits.length < 1 || bits.length > 9) throw new argumentexception("invalid length", "bits");
10:    byte[] mask = { 0x7f, 0x3f, 0x1f, 0x0f, 0x07, 0x03, 0x01, 0x00 };
11:    if (bits.length > 1 && bits.length < 9) bits[0] &= mask[bits.length - 1];
12:    array.reverse(bits);
13:    array.resize(ref bits, 8);
14:    if (!bitconverter.islittleendian) array.reverse(bits);
15:    data = decode(bitconverter.toint64(bits, 0));
16:  }


mask 數組其實只需要初始化一次就行了,而不需要每次調用該建構函式時都進行初始化。也就是說,應該將 mask 變數聲明為 static 的,如下所示:

static readonly byte[] mask = { 0x7f, 0x3f, 0x1f, 0x0f, 0x07, 0x03, 0x01, 0x00 };
願望是美好的,現實是殘酷的,加上 static 關鍵字後,再編譯時間 c# 編譯器會報告以下錯誤:

error cs0106: 修飾符“static”對該項無效 
也就是說,c# 語言不允許使用 static 修飾符來聲明方法內部的變數。但是在 c/c++ 語言中是允許這麼做的。

如果把該建構函式內部的 mask 變數提升到建構函式外部,成為 zipinteger 結構的欄位成員,就可以聲明為 static 的。但是這樣一樣,讀這段代碼的人就不容易弄清楚 mask 欄位只在這個建構函式內部使用,成為代碼的“壞味道”,一點也不優雅了。

 

好了,讓我們寫一小段程式來測試一下加上 static 後對運行效率的影響:

01:  using system;
02:  using system.io;
03:  using system.diagnostics;
04:  using skyiv.numerics;
05: 
06:  namespace skyiv.tester
07:  {
08:    sealed class statictester
09:    {
10:      static void main()
11:      {
12:        try
13:        {
14:          new statictester().run(100000000);
15:        }
16:        catch (exception ex)
17:        {
18:          console.writeline(ex);
19:        }
20:      }
21: 
22:      void run(int count)
23:      {
24:        console.writeline("  os version: " + environment.osversion);
25:        console.writeline(" clr version: " + environment.version);
26:        using (var reader = new memorystream(getbytes(count)))
27:        {
28:          var watch = stopwatch.startnew();
29:          var n = 0;
30:          for (var i = 0; i < 10; i++)
31:          {
32:            reader.seek(0, seekorigin.begin);
33:            while (zipinteger.read(reader).hasvalue) n++;
34:          }
35:          watch.stop();
36:          console.writeline("       count: " + n.tostring("n0"));
37:          console.writeline("milliseconds: " + watch.elaps教程edmilliseconds.tostring("n0"));
38:        }
39:      }
40: 
41:      byte[] getbytes(int count)
42:      {
43:        var bits = new byte[count];
44:        var rand = new random(123456);
45:        rand.nextbytes(bits);
46:        return bits;
47:      }
48:    }
49:  }
上述程式中第 44 行使用固定的種子初始化 random 類的新執行個體,從而產生相同的隨機數序列,以便用相同的測試案例來進行測試,使測試結果具有可比性。注意,在上述程式中如果 count 值和隨機數的種子選取得不好,執行到第 33 行的 zipinteger.read 方法時是有可能拋出 endofstreamexception 異常的。

 

這個測試程式在 windows vista 作業系統的 .net framework 4 環境下的運行結果如下所示:

  os version: microsoft windows nt 6.0.6002 service pack 2
 clr version: 4.0.30319.1
       count: 500,990,730
milliseconds: 181,886

 
將這個測試程式對 mask 變數是非靜態和靜態兩種情況分別運行五次,得到的 milliseconds 值如下表所示:

序號 非靜態 靜態
1 181,886 167,528
2 179,074 166,847
3 180,309 166,526
4 183,542 166,399
5 179,469 167,365
平均 180,856 166,933

經過簡單的計算得知,靜態情況下比非靜態情況下的平均運行效率提高了 7.7%,還是很可觀的。

 

這個測試程式在 ubuntu 10.10 作業系統的 mono 2.6.7 環境的運行結果如下所示:

非靜態 靜態
  os version: unix 2.6.35.24
 clr version: 2.0.50727.1433
       count: 500,992,030
milliseconds: 660,254

   os version: unix 2.6.35.24
 clr version: 2.0.50727.1433
       count: 500,992,030
milliseconds: 583,387

 

首頁 1 2 3 末頁
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.