Even on your initial attempt, I got these results:
Mem::replace based approach took 2657210
Clone based approach took 2983794
I’m pretty sure that 2983794 > 2657210… Also, this makes sense, the clone approach has to copy the buffer each time.
On a second attempt I got:
Mem::replace based approach took 3652959
Clone based approach took 3020418
Hmm, mixed results. Time for a bit of number crunching!
Here is the test I ran. I ran it on my pc, as it would’ve taken ages on the playground, and even then took alot of my cpu:
I guess I should’ve optimized beforehand! Anyway, the results to that are:
On average clone took: 9968
The median clone took: 9910
On average mmcpy took: 7280
The median mmcpy took: 7276
they are the same clone is slower.
(Note that the above results are in milliseconds)
Edit: Whoops I misread my results, they are actually not the same.