Static keywords in asp.net C # (1/3)

Source: Internet
Author: User

01: // <summary>
02: // use the value in the byte array to initiate a new instance of zipinteger Structure
03: // Note: This constructor will destroy the value of the passed bits parameter.
04: // </summary>
05: // <param name = "bits"> an array of byte values in the order of 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 :}


The mask array only needs to be initialized once, instead of being initialized every time the constructor is called. That is to say, the mask variable should be declared as static, as shown below:

Static readonly byte [] mask = {0x7f, 0x3f, 0x1f, 0x0f, 0x07, 0x03, 0x01, 0x00 };
The desire is beautiful, and the reality is cruel. After the static keyword is added, the c # compiler will report the following errors during compilation:

Error cs0106: the modifier "static" is invalid for this item
That is to say, the c # language does not allow the use of static modifiers to declare internal variables of the method. However, this is allowed in c/c ++.

If you promote the mask variable inside the constructor to the outside of the constructor and become a field Member of the zipinteger structure, the variable can be declared as static. But in this case, it is not easy for the reader to understand that the mask field is only used within the constructor and becomes a bad taste of the Code. It is not elegant at all.

 

Well, let's write a short program to test the impact on the running efficiency after adding 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 tutorial 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 :}
The first row of the above program uses a fixed seed to initialize a new instance of the random class, so as to generate the same random number sequence, so that the same test case can be used for testing, so that the test results can be comparable. Note: In the above program, if the count value and the random number seed are not well selected, the endofstreamexception may be thrown when the zipinteger. read method of row 33rd is executed.

 

The test program runs in the. net framework 4 environment of windows vista as follows:

OS version: microsoft windows nt 6.0.6002 service pack 2
Clr version: 4.0.30319.1
Number: 500,990,730
Milliseconds: 181,886

 
Run the test program five times for the mask variable in non-static and static conditions. The obtained milliseconds values are shown in the following table:

No. Non-static
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
Average 180,856 166,933

After a simple calculation, it is found that the average running efficiency in the static environment is improved by 7.7%, which is considerable.

 

The running result of this test program in mono 2.6.7 of ubuntu 10.10 operating system is as follows:

Non-static
OS version: unix 2.6.35.24
Clr version: 2.0.50727.1433
Number: 500,992,030
Milliseconds: 660,254

OS version: unix 2.6.35.24
Clr version: 2.0.50727.1433
Number: 500,992,030
Milliseconds: 583,387

1 2 3

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.