Fastest Way to Append Multiple Elements to an Array

zeljkog via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Dec 17 03:15:28 PST 2014


On 15.12.14 01:00, "Nordlöw" wrote:
> Isn't this algorithm already encoded somewhere in Phobos?

void append(T, Args...)(ref T[] arr, auto ref Args args){
{
   static if (args.length == 1)
      arr ~= args[0];     // inlined
   else{
      arr.length += args.length;
      foreach(i, e; args)
         arr[$ - args.length + i] = e;
   }
}

I've just tested, this looks usable.
Equal for 1 item, considerably (~40%) faster for 2, and much (100+%) faster for more.
Also more convenient.

Maybe samthing like that should go to Fobos.


More information about the Digitalmars-d-learn mailing list