c# - Can I copy this array any faster? -
is absolute fastest possibly copy bitmap
byte[]
in c#
?
if there speedier way dying know!
const int width = /* width */; const int height = /* height */; bitmap bitmap = new bitmap(width, height, pixelformat.format32bpprgb); byte[] bytes = new byte[width * height * 4]; bitmaptobytearray(bitmap, bytes); private unsafe void bitmaptobytearray(bitmap bitmap, byte[] bytes) { bitmapdata bitmapdata = bitmap.lockbits(new rectangle(0, 0, width, height), imagelockmode.readonly, pixelformat.format32bpprgb); fixed(byte* pbytes = &bytes[0]) { movememory(pbytes, bitmapdata.scan0.topointer(), width * height * 4); } bitmap.unlockbits(bitmapdata); } [dllimport("kernel32.dll", entrypoint = "rtlmovememory", setlasterror = false)] private static unsafe extern void movememory(void* dest, void* src, int size);
well, using marshal.copy()
wiser here, @ least cuts out on (one time) cost of looking dll export. that's it, both use c runtime memcpy()
function. speed entirely throttled ram bus bandwidth, buying more expensive machine can speed up.
beware profiling tricky, accessing bitmap data first time causes page faults pixel data memory. how long takes critically dependent on hard disk doing , state of file system cache.
Comments
Post a Comment