Timeline for Quickest way to test whether array is constant
Current License: CC BY-SA 4.0
11 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Sep 30, 2020 at 18:35 | comment | added | Michael E2 | @Alan The Q&A I linked is probably the best discussion available. The closest to official documentation is this Wolfram Library notebook and article by one of the lead kernel developers, library.wolfram.com/infocenter/Demos/391, which is also linked in a comment to that Q&A. | |
Sep 30, 2020 at 18:16 | comment | added | Alan | @MichaelE2 OK, that makes sense. Clearly I've paid too little attention to when arrays are packed and unpacked. Can you recommend useful documentation? | |
Sep 30, 2020 at 2:44 | comment | added | Michael E2 |
@Alan SameQ @@ array is a little faster than DeleteDuplicates[array] on array = Sqrt@ConstantArray[2, {100000}]; . The reason DeleteDuplicates is faster on array = ConstantArray[2, {100000}]; is that in effect it computes DeleteDuplicates[Developer`ToPackedArray@array] and SameQ @@ array cannot take advantage of packed arrays. Numbers have certain optimizations available that general expressions do not. Data functions such as DeleteDuplicates are programmed to take advantage of them.
|
|
Sep 29, 2020 at 18:21 | comment | added | Alan |
@kglr This still seems odd to me. DeleteDuplicates should require comparison and deletion, while SameQ should only require comparison. Perhaps SameQ is doing all pairwise comparisons in order not to assume transitivity of the comparisons?
|
|
Sep 29, 2020 at 17:34 | comment | added | kglr |
@Alan, the difference may also have to do with the fact that CountDistinct uses Length[DeleteDuplicates @ const] == 1 under the hood.
|
|
Sep 29, 2020 at 17:31 | history | edited | kglr | CC BY-SA 4.0 |
added 644 characters in body
|
Sep 29, 2020 at 17:11 | comment | added | Michael E2 |
@Alan It's because the array is "unpacked". Start with const = Developer`FromPackedArray@ConstantArray[1, 100000]; and rerun the timings; then Equal @@ const is only about twice as slow. See What is a packed array?
|
|
Sep 29, 2020 at 17:07 | comment | added | Sjoerd Smit | Oh, nice find there | |
Sep 29, 2020 at 16:40 | comment | added | Alan |
Why is CountDistinct@const so much faster than Equal@@const or SameQ@@const ? That seems counterintutive.
|
|
Sep 29, 2020 at 15:18 | comment | added | Michael E2 |
I don't know who wrote Statistics`Library but it's full of good stuff. (+1)
|
|
Sep 29, 2020 at 15:08 | history | answered | kglr | CC BY-SA 4.0 |