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