Fastest Way to Append Multiple Elements to an Array

2021-04-10 Thread Per Nordlöw via Digitalmars-d-learn
https://forum.dlang.org/post/rkxnfxsjvkffxufok...@forum.dlang.org On Thursday, 1 January 2015 at 19:18:34 UTC, Nordlöw wrote: Do we really need Append and Prepend (along with append and prepend) here? Doesn't [cC]hain already fulfill the needs of a lazy range in this case inplace of Append? I

Re: Fastest Way to Append Multiple Elements to an Array

2015-01-01 Thread Nordlöw
On Wednesday, 31 December 2014 at 00:36:36 UTC, Damian wrote: Append and Prepend would be very useful additions to Phobos it's quite surprising there not already there? In any case would love to see some pull requests :) Do we really need Append and Prepend (along with append and prepend) here

Re: Fastest Way to Append Multiple Elements to an Array

2014-12-30 Thread Damian via Digitalmars-d-learn
On Friday, 26 December 2014 at 16:31:48 UTC, Nordlöw wrote: On Friday, 26 December 2014 at 16:29:56 UTC, Nordlöw wrote: See my update at https://github.com/nordlow/justd/blob/master/algorithm_ex.d#L1602 which tries to merge all the ideas into one adapting algorithm :) Destroy! BTW: 1. Is

Re: Fastest Way to Append Multiple Elements to an Array

2014-12-30 Thread Nordlöw
On Saturday, 27 December 2014 at 07:34:43 UTC, Tobias Pankrath wrote: On Friday, 26 December 2014 at 16:31:48 UTC, Nordlöw wrote: On Friday, 26 December 2014 at 16:29:56 UTC, Nordlöw wrote: See my update at https://github.com/nordlow/justd/blob/master/algorithm_ex.d#L1602 which tries to merge

Re: Fastest Way to Append Multiple Elements to an Array

2014-12-26 Thread Tobias Pankrath via Digitalmars-d-learn
On Friday, 26 December 2014 at 16:31:48 UTC, Nordlöw wrote: On Friday, 26 December 2014 at 16:29:56 UTC, Nordlöw wrote: See my update at https://github.com/nordlow/justd/blob/master/algorithm_ex.d#L1602 which tries to merge all the ideas into one adapting algorithm :) Destroy! BTW: writ

Re: Fastest Way to Append Multiple Elements to an Array

2014-12-26 Thread Nordlöw
On Friday, 26 December 2014 at 16:29:56 UTC, Nordlöw wrote: See my update at https://github.com/nordlow/justd/blob/master/algorithm_ex.d#L1602 which tries to merge all the ideas into one adapting algorithm :) Destroy! BTW: 1. Is is relevant to extend `append` to data being a non-Random-A

Re: Fastest Way to Append Multiple Elements to an Array

2014-12-26 Thread Nordlöw
On Thursday, 18 December 2014 at 20:12:01 UTC, Tobias Pankrath wrote: You shouldn't use my code verbatim though. For example, you should only use x.length, if x is of element type of data. Otherwise if you have e.g. an array of array you'd get totally wrong numbers. What role does `x` play he

Re: Fastest Way to Append Multiple Elements to an Array

2014-12-19 Thread anonymous via Digitalmars-d-learn
On Saturday, 20 December 2014 at 00:15:21 UTC, MarcelDuchamp wrote: You've forget the array of ref version. (append the address of the first data) What "array of ref" version? Sounds like it would be a different data structure, which would be beyond the scope the topic. And adding S to an arra

Re: Fastest Way to Append Multiple Elements to an Array

2014-12-19 Thread anonymous via Digitalmars-d-learn
On Friday, 19 December 2014 at 14:41:07 UTC, Anonymous wrote: What a big surprise. If you make an array of struct, each item of your array has the length of the struct. structs a values. If you want to append a struct to an array just append a pointer to the struct: -- struct arg{uint

Re: Fastest Way to Append Multiple Elements to an Array

2014-12-19 Thread MarcelDuchamp via Digitalmars-d-learn
On Saturday, 20 December 2014 at 00:44:54 UTC, zeljkog wrote: If you wish run this just add: struct S{ int n1, n2, n3, n4, n5 ...; } I wish. And I also wish you to get that to append in an array or a in a list some ptr its not the same as appending structs, particularly when the structs a

Re: Fastest Way to Append Multiple Elements to an Array

2014-12-19 Thread zeljkog via Digitalmars-d-learn
If you wish run this just add: struct S{ int n1, n2, n3, n4, n5 ...; }

Re: Fastest Way to Append Multiple Elements to an Array

2014-12-19 Thread MarcelDuchamp via Digitalmars-d-learn
On Saturday, 20 December 2014 at 00:15:21 UTC, MarcelDuchamp wrote: On Friday, 19 December 2014 at 23:24:13 UTC, zeljkog wrote: On 19.12.14 23:56, Ali Çehreli wrote: Can we see the test code please. Ali import std.stdio, std.datetime, std.random; int N = 10_000; int len = 1000; int k = 4;

Re: Fastest Way to Append Multiple Elements to an Array

2014-12-19 Thread MarcelDuchamp via Digitalmars-d-learn
On Friday, 19 December 2014 at 23:24:13 UTC, zeljkog wrote: On 19.12.14 23:56, Ali Çehreli wrote: Can we see the test code please. Ali import std.stdio, std.datetime, std.random; int N = 10_000; int len = 1000; int k = 4; struct S{ int n1, n2; //~ this(this){ //~ writeln(

Re: Fastest Way to Append Multiple Elements to an Array

2014-12-19 Thread zeljkog via Digitalmars-d-learn
On 19.12.14 23:56, Ali Çehreli wrote: > Can we see the test code please. > > Ali import std.stdio, std.datetime, std.random; int N = 10_000; int len = 1000; int k = 4; struct S{ int n1, n2; //~ this(this){ //~ writeln("this(this)"); //~ } } ref T[] append(T, Args...)(ref

Re: Fastest Way to Append Multiple Elements to an Array

2014-12-19 Thread Ali Çehreli via Digitalmars-d-learn
On 12/19/2014 07:42 AM, zeljkog wrote: On 19.12.14 16:25, Steven Schveighoffer wrote: Did you compile both tests with the same command line parameters? Yes. Can we see the test code please. Ali

Re: Fastest Way to Append Multiple Elements to an Array

2014-12-19 Thread Steven Schveighoffer via Digitalmars-d-learn
On 12/19/14 5:28 PM, Anonymous wrote: What a big surprise. If you make an array of struct, each item of your array has the length of the struct. structs a values. If you want to append a struct to an array just append a pointer to the struct: -- struct arg{uint a,r,g,h;}; arg * [] argh

Re: Fastest Way to Append Multiple Elements to an Array

2014-12-19 Thread Anonymous via Digitalmars-d-learn
On Friday, 19 December 2014 at 18:38:32 UTC, Steven Schveighoffer wrote: On 12/19/14 12:15 PM, Anonymous wrote: On Friday, 19 December 2014 at 15:25:46 UTC, Steven Schveighoffer wrote: This is surprising to me. Ho ho ho, this year he has brought your surprise sooner than expected... It

Re: Fastest Way to Append Multiple Elements to an Array

2014-12-19 Thread Steven Schveighoffer via Digitalmars-d-learn
On 12/19/14 12:15 PM, Anonymous wrote: On Friday, 19 December 2014 at 15:25:46 UTC, Steven Schveighoffer wrote: This is surprising to me. Ho ho ho, this year he has brought your surprise sooner than expected... It would be nice if this was the only time this year I was surprised because

Re: Fastest Way to Append Multiple Elements to an Array

2014-12-19 Thread Anonymous via Digitalmars-d-learn
On Friday, 19 December 2014 at 15:25:46 UTC, Steven Schveighoffer wrote: On 12/18/14 5:27 PM, zeljkog wrote: On 18.12.14 14:50, Steven Schveighoffer wrote: I wonder how your code compares to this: void append(T)(ref T[] arr, T[] args...) { arr ~= args; } This is ~20% slower for ints, but

Re: Fastest Way to Append Multiple Elements to an Array

2014-12-19 Thread Steven Schveighoffer via Digitalmars-d-learn
On 12/19/14 10:42 AM, zeljkog wrote: On 19.12.14 16:25, Steven Schveighoffer wrote: This is surprising to me. I would expect especially ints to be faster. In reality, there is no difference between arr ~= args and arr.length += args.length, and then copying, it's just how you do the copying. Per

Re: Fastest Way to Append Multiple Elements to an Array

2014-12-19 Thread zeljkog via Digitalmars-d-learn
On 19.12.14 16:25, Steven Schveighoffer wrote: This is surprising to me. I would expect especially ints to be faster. In reality, there is no difference between arr ~= args and arr.length += args.length, and then copying, it's just how you do the copying. Perhaps it's the call to memcpy in the ru

Re: Fastest Way to Append Multiple Elements to an Array

2014-12-19 Thread Steven Schveighoffer via Digitalmars-d-learn
On 12/18/14 5:27 PM, zeljkog wrote: On 18.12.14 14:50, Steven Schveighoffer wrote: I wonder how your code compares to this: void append(T)(ref T[] arr, T[] args...) { arr ~= args; } This is ~20% slower for ints, but it difference increases for bigger structs. This is surprising to me

Re: Fastest Way to Append Multiple Elements to an Array

2014-12-19 Thread Anonymous via Digitalmars-d-learn
On Thursday, 18 December 2014 at 22:27:06 UTC, zeljkog wrote: On 18.12.14 14:50, Steven Schveighoffer wrote: I wonder how your code compares to this: void append(T)(ref T[] arr, T[] args...) { arr ~= args; } This is ~20% slower for ints, but it difference increases for bigger structs.

Re: Fastest Way to Append Multiple Elements to an Array

2014-12-18 Thread zeljkog via Digitalmars-d-learn
On 18.12.14 14:50, Steven Schveighoffer wrote: > I wonder how your code compares to this: > > void append(T)(ref T[] arr, T[] args...) > { > arr ~= args; > } This is ~20% slower for ints, but it difference increases for bigger structs.

Re: Fastest Way to Append Multiple Elements to an Array

2014-12-18 Thread Tobias Pankrath via Digitalmars-d-learn
On Thursday, 18 December 2014 at 15:12:39 UTC, Nordlöw wrote: On Wednesday, 17 December 2014 at 12:30:37 UTC, Tobias Pankrath wrote: 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;

Re: Fastest Way to Append Multiple Elements to an Array

2014-12-18 Thread Nordlöw
On Wednesday, 17 December 2014 at 12:30:37 UTC, Tobias Pankrath wrote: 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] =

Re: Fastest Way to Append Multiple Elements to an Array

2014-12-18 Thread Steven Schveighoffer via Digitalmars-d-learn
On 12/17/14 6:15 AM, zeljkog wrote: 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 += ar

Re: Fastest Way to Append Multiple Elements to an Array

2014-12-17 Thread zeljkog via Digitalmars-d-learn
On 17.12.14 13:30, Tobias Pankrath wrote: void append(T, Args...)(ref T[] data, Args args) { static size_t estimateLength(Args args) { size_t result; foreach(e; args) static if(hasLength!(typeof(e))) result += e.length; else

Re: Fastest Way to Append Multiple Elements to an Array

2014-12-17 Thread Tobias Pankrath via Digitalmars-d-learn
On Wednesday, 17 December 2014 at 11:15:30 UTC, zeljkog wrote: 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{

Re: Fastest Way to Append Multiple Elements to an Array

2014-12-17 Thread zeljkog via Digitalmars-d-learn
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)

Re: Fastest Way to Append Multiple Elements to an Array

2014-12-15 Thread Nordlöw
On Monday, 15 December 2014 at 15:55:22 UTC, Steven Schveighoffer wrote: What's the fastest way to append multiple elements to an array?: Before appending, call reserve to avoid the possibility of reallocating multiple times: arr.reserve(arr.length + n); // about to append n ele

Re: Fastest Way to Append Multiple Elements to an Array

2014-12-15 Thread Steven Schveighoffer via Digitalmars-d-learn
On 12/14/14 6:16 PM, "Nordlöw" wrote: What's the fastest way to append multiple elements to an array?: Before appending, call reserve to avoid the possibility of reallocating multiple times: arr.reserve(arr.length + n); // about to append n elements. Either arr ~= e1;

Re: Fastest Way to Append Multiple Elements to an Array

2014-12-15 Thread John Colvin via Digitalmars-d-learn
On Sunday, 14 December 2014 at 23:16:12 UTC, Nordlöw wrote: What's the fastest way to append multiple elements to an array?: Either arr ~= e1; arr ~= e2; arr ~= e3; or arr ~= [e1,e2,e3]; or some other trick? It does somewhat depend on context but std.array.append

Re: Fastest Way to Append Multiple Elements to an Array

2014-12-14 Thread anonymous via Digitalmars-d-learn
On Sunday, 14 December 2014 at 23:52:15 UTC, zeljkog wrote: On 15.12.14 00:16, "Nordlöw" wrote: or some other trick? void append(T, Args...)(ref T[] arr, Args args){ arr.length += args.length; foreach(i, e; args) arr[$ - args.length + i] = args[i]; } void main() { int[] arr = [1, 2

Re: Fastest Way to Append Multiple Elements to an Array

2014-12-14 Thread zeljkog via Digitalmars-d-learn
On 15.12.14 01:00, "Nordlöw" wrote: Isn't this algorithm already encoded somewhere in Phobos? I don't know.

Re: Fastest Way to Append Multiple Elements to an Array

2014-12-14 Thread Nordlöw
On Sunday, 14 December 2014 at 23:52:15 UTC, zeljkog wrote: void append(T, Args...)(ref T[] arr, Args args){ arr.length += args.length; foreach(i, e; args) arr[$ - args.length + i] = args[i]; } Isn't this algorithm already encoded somewhere in Phobos?

Re: Fastest Way to Append Multiple Elements to an Array

2014-12-14 Thread zeljkog via Digitalmars-d-learn
On 15.12.14 00:16, "Nordlöw" wrote: > or some other trick? void append(T, Args...)(ref T[] arr, Args args){ arr.length += args.length; foreach(i, e; args) arr[$ - args.length + i] = args[i]; } void main() { int[] arr = [1, 2, 3, 4, 5]; arr.append(3, 10, 14); writeln(arr); } output

Fastest Way to Append Multiple Elements to an Array

2014-12-14 Thread Nordlöw
What's the fastest way to append multiple elements to an array?: Either arr ~= e1; arr ~= e2; arr ~= e3; or arr ~= [e1,e2,e3]; or some other trick?