C
C#2y ago
Thinker

Is Array.Copy decent for copying sections of memory?

I'm making a thing which relies pretty heavily on copying (not very large) sections of memory inside an array to other sections of the array. Is Array.Copy decent for this or is there some other lower-level alternative that is more performant/efficient?
10 Replies
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Thinker
Thinker2y ago
Well I have to rearrange stuff inside the array, but I guess I could use spans as auxiliary storage. To clarify, all the copy operations are from/to the same array.
SWR
SWR2y ago
Array.Copy will be your best bet for copying beyond multithreading (which is not even guaranteed to be faster)
Thinker
Thinker2y ago
Fair Last time I checked I think it does as many low-level optimizations as possible
SWR
SWR2y ago
I'd say, follow the old saying : don't optimize until it's absolutely necessary
Thinker
Thinker2y ago
True I'm writing a runtime for my own language, so some semblance of performance would be nice, although it's really not important that it's very efficient.
SWR
SWR2y ago
Finally, it really depends on what problem you're trying to solve. An array may not even be the best data structure.
Thinker
Thinker2y ago
hmm, I've considered using linked lists for what I'm doing, but I think I've settled on copying elements inside an array likely being the most efficient.
SWR
SWR2y ago
If you're trying to have copied data exist in multiple parts of an array, and copies and can do to and from, you may get better performance with a graph data structure Rather than copying an array, you just connect nodes with a new edge. Hard to say for sure without context though
Thinker
Thinker2y ago
true I'll go with this for, can always optimize it later