About the efficiency test of C # array initialization

Source: Internet
Author: User
Tags arrays datetime int size tostring
Arrays I have recently written in data compression programs, often used in arrays. All elements of the array need to be zeroed at every point. Because C # does not provide a memset () method. So the following tests were performed.

Main program part:

static void Main (string[] args)
{
int i,k;
Double p = 0;
DateTime S, E;
byte[] test = new byte[65536];
byte[] test2 = new byte[65536];
for (int i = 0; i < i++)
{
s = DateTime.Now;
For (j=0 j<50000; j + +)
{
//***************//
}
e = DateTime.Now;
TimeSpan C = e-s;
Console.WriteLine (C.totalmilliseconds.tostring ());
p + + c.totalmilliseconds;
}

P/= 10;
Console.WriteLine ("* * *" + p.tostring () + "* * *");
Console.read ();
}

Replace the red part with the following methods:

1.test = new byte[65536];

This method is obvious and easy to understand, each time a new array, the value of which is certainly 0.

2.memset (test,0,65536);

Imitate C + + Write a memset function:
public static void Memset (byte[] buf, byte val, int size)
{
int i;
for (i=0 i < size; i++)
Buf[i] = val;
}

3.for (k = 0; k < 65536; test[k++] = 0);
Write the function memset directly into the red part. The idea is simple, saving the overhead of calling a function.

4.array.clear (test,0,65536);

Direct use. NET Self-band method.

5.test2. CopyTo (test, 0);

Copies content to the destination array with an empty array.

Then the test is done and the results are as follows:

Method the longest and shortest average

1 1937.5 1812.5 1842.1875

2 4593.75 4625 4584.375

3 6046.875 5984.375 6001.5625

4 562.5 640.5 581.25

5 812.5 750 770.3125

Computer: p43.0e (Hyper-threading on)/ddr400 512M (dual channel) Software environment: Win xp-sp2/vs2003 Unit: MS


The result is clearly that the Array.clear () method is the ultimate winner. But it has a disadvantage that only the array can be emptied (0). If you want to set the array to a specific format CopyTo () The best way to change time in space.
There is only one point of view, the 2nd function call to the 3rd embedded algorithm, the efficiency is reduced. This can only be explained as a compiler optimization. It seems that using C # to write a program does not care too much about the overhead of function calls.




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.