C # version Memset features

Source: Internet
Author: User

public static Class Cmemory
{
Static Action<intptr, Byte, int> memsetdelegate;
Static Cmemory ()
{
DynamicMethod DynamicMethod = new DynamicMethod (
"Memset",
Methodattributes.public | Methodattributes.static, Callingconventions.standard,
Null
New[] {typeof (IntPtr), typeof (Byte), typeof (int)}, typeof (Cmemory), True
);
ILGenerator generator = Dynamicmethod.getilgenerator ();
Generator. Emit (OPCODES.LDARG_0);
Generator. Emit (opcodes.ldarg_1);
Generator. Emit (opcodes.ldarg_2);
Generator. Emit (OPCODES.INITBLK);
Generator. Emit (Opcodes.ret);
Memsetdelegate = (action<intptr, byte, int>) Dynamicmethod.createdelegate (typeof (Action<intptr, Byte, int >));
}
public static void memset (byte[] array, byte-what, int length)
{
GCHandle GCHandle = gchandle.alloc (array, gchandletype.pinned);
Memsetdelegate (Gchandle.addrofpinnedobject (), what, length);
Gchandle.free ();
}

}


MSIL has support for the INITBLK directive, which is equivalent to memset, but the. NET class has no corresponding encapsulation. Use emit to encapsulate the directive as a function. (Note: Array.clear can only clear 0, can not specify other values)

PC,WP available, Xamarin is not measured.

Performance: PC on INITBLK and array.clear performance, WP is 3 times times the speed. About 12 to 15 times times faster than the For loop byte assignment.

C # version Memset features

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.