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;
}
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:
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.
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.