Timeline for Adding Lists Together
Current License: CC BY-SA 3.0
17 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Jul 22, 2012 at 0:35 | comment | added | Mark Adler | I know what it does -- it's trivial to see by inspection. My point is that there is no conceivable reason to do a speed comparison between two things that produce different output or take different input. For this question, the comparison has to be with list1 + list2 + list3. | |
Jul 22, 2012 at 0:19 | comment | added | Artes |
@MarkAdler It doesn't yield wrong results, it serves different purpose. (Plus @@@ {list1,list2,...,list2})==( Total /@{list1,list2,...,list2})
|
|
Jul 22, 2012 at 0:07 | comment | added | Mark Adler | Why should I compare it with something that gives the wrong result? | |
Jul 21, 2012 at 6:30 | comment | added | Artes |
@MarkAdler It is in fact better if you test more extensively varoius types of lists, not restricting to this problem literally. You can use it in more general situations, then it apears to be better (not always). Look at the documentation center to read Total and try more examples to compare with Plus . Consider also comparing it with Plus@@@list[list1,list2,...,listn] .
|
|
Jul 21, 2012 at 6:18 | comment | added | Mark Adler | In summary, the obvious solution of list1 + list2 + list3 on packed arrays of reals is just as fast as the other fastest solutions (so far) of Total[{list1,list2,list3}] or Plus@@{list1,list2,list3}. So the "much better" claim in this answer is not borne out, at least in terms of speed. (The assertion in a comment above about Apply always unpacking is also apparently not true.) The MapThread and Tr/@Transpose solutions are two orders of magnitude slower. (I am using Mma 8.0.4.0 on an Intel Core 2 Duo, Mac OS X.) | |
Jul 21, 2012 at 4:27 | comment | added | Mark Adler | Wow, no. Tr/@Transpose was about the same speed as the MapThread above, which is two orders of magnitude slower than the other solutions. (Again, this was on packed arrays of integers and reals, one million in length.) | |
Jul 20, 2012 at 21:01 | comment | added | user1066 |
There is also Tr /@ Transpose[{list1, list2, list3}] , seemingly quite fast for packed arrays, see here (Ted Ersek) and here (Paul Abbott)
|
|
Jul 20, 2012 at 16:39 | vote | accept | canadian_scholar | ||
Jul 20, 2012 at 16:14 | comment | added | Mark Adler | I had done those tests using vectors generated using RandomInteger[]. I repeated the tests using RandomReal[]. In that case, there was no longer any difference between list1 + list2 + list3 and Total/Plus@@. All three had very close times. Still, MapThread came out almost two orders of magnitude slower. (Also using packed arrays.) | |
Jul 20, 2012 at 16:06 | comment | added | Mark Adler | I did the test on lists with one million numerical entries. Plus@@ and Total took almost exactly the same amount of time as each other. They were packed arrays. | |
Jul 20, 2012 at 15:29 | comment | added | Artes | @yulinlinyu Your answer is straightforward when you deal with small lists, however for long lists mine should be faster. | |
Jul 20, 2012 at 15:23 | comment | added | yulinlinyu | @Artes, I prefer your answer to mine. | |
Jul 20, 2012 at 15:14 | comment | added | Artes |
@MarkAdler It depends on the data you are deal with, in general Total is faster than other approaches, nevertheless if we deal with unpacked lists, Plus would be faster.
|
|
Jul 20, 2012 at 15:11 | comment | added | rm -rf♦ |
@MarkAdler Are you sure you aren't micro-optimizing and looking at negligible differences on tiny lists? I can't imagine Plus@@ being faster, as Apply unpacks the list...
|
|
Jul 20, 2012 at 15:08 | comment | added | Mark Adler | Interesting. Total and Plus @@ are about 50% faster than list1 + list2 + list3 (they are the same speed as each other). I wonder why. The truly odd thing is that MapThread is more than two orders of magnitude slower! | |
Jul 20, 2012 at 14:43 | history | edited | Artes | CC BY-SA 3.0 |
added 53 characters in body; added 9 characters in body; deleted 8 characters in body
|
Jul 20, 2012 at 14:37 | history | answered | Artes | CC BY-SA 3.0 |