[
https://issues.apache.org/jira/browse/LANG-536?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14172305#comment-14172305
]
Duncan Jones commented on LANG-536:
-----------------------------------
Regarding the speed discussions, I performed a little benchmark (using JMH) to
test your original patch against a primitive implementation. I considered the
{{isSorted(double[])}} method and added an additional method:
{code:java}
public static boolean isSortedX(double[] a) {
if(a == null || a.length <= 1) {
return true;
}
double previous = a[0];
final int n = a.length;
for(int i = 1; i < n; i++) {
final double current = a[i];
if(Double.compare(previous, current) > 0) {
return false;
}
previous = current;
}
return true;
}
{code}
I considered the worst case, which is a large array (10,000 items) that's
already sorted. Note that this isn't an edge case - presumably arrays passed to
{{isSorted}} will often be already sorted - that's the test, after all. The
results were very clear:
{code}
# Run complete. Total time: 00:16:03
Benchmark Mode Samples Score Error Units
o.s.MyBenchmark.testObject thrpt 200 9771.778 ▒ 86.590 ops/s
o.s.MyBenchmark.testPrimitive thrpt 200 184494.374 ▒ 1087.017 ops/s
{code}
Almost twenty times faster using primitives. Given the relative simplicity of
these methods, I think we should code individual primitive implementations of
the sort I have described in this comment. I'll attach a ZIP file containing
the JMH code, in case anyone wants to play. It is a Maven project that will
build provided you apply the original patch, add my method above and run {{mvn
clean install}} on your Lang3 code base to install the snapshot in your local
repository. The performance test is then executed with {{java -jar
target\benchmarks.jar}}.
> Add isSorted() to ArrayUtils
> ----------------------------
>
> Key: LANG-536
> URL: https://issues.apache.org/jira/browse/LANG-536
> Project: Commons Lang
> Issue Type: New Feature
> Components: lang.*
> Reporter: Sergei Ivanov
> Priority: Minor
> Fix For: Review Patch
>
> Attachments: LANG-536 partial.patch, LANG-536.patch, perftest.zip
>
>
> In my unit tests I often need to verify that an array is correctly sorted.
> In order to achieve this, I've got two helper methods as follows.
> Is it possible to integrate these methods into ArrayUtils?
> {code}
> /**
> * Checks that the specified array of objects is in an ascending order
> * according to the specified comparator. All elements in the array must
> be
> * <i>mutually comparable</i> by the specified comparator (that is,
> * <tt>c.compare(e1, e2)</tt> must not throw a <tt>ClassCastException</tt>
> * for any elements <tt>e1</tt> and <tt>e2</tt> in the array).
> *
> * @param a the array to be checked.
> * @param c the comparator to determine the order of the array. A
> * <tt>null</tt> value indicates that the elements'
> * {@linkplain Comparable natural ordering} should be used.
> * @return {@code true}, if the array is sorted; {@code false}, otherwise.
> * @throws ClassCastException if the array contains elements that are
> * not <i>mutually comparable</i> using the specified comparator.
> */
> public static <T> boolean isSorted(final T[] a, final Comparator<? super
> T> c) {
> if (a.length <= 1) {
> // Empty or singleton arrays are always sorted
> return true;
> }
> // Otherwise, check that every element is not smaller than the
> previous
> T previous = a[0];
> for (int i = 1, n = a.length; i < n; i++) {
> final T current = a[i];
> if (c.compare(previous, current) > 0) {
> return false;
> }
> previous = current;
> }
> return true;
> }
> /**
> * Checks that the specified array of objects is in an ascending order,
> * according to the {@linkplain Comparable natural ordering} of its
> elements.
> * All elements in the array must implement the {@link Comparable}
> interface.
> * Furthermore, all elements in the array must be <i>mutually
> comparable</i>
> * (that is, <tt>e1.compareTo(e2)</tt> must not throw a
> <tt>ClassCastException</tt>
> * for any elements <tt>e1</tt> and <tt>e2</tt> in the array).
> *
> * @param a the array to be checked.
> * @return {@code true}, if the array is sorted; {@code false}, otherwise.
> * @throws ClassCastException if the array contains elements that are not
> * <i>mutually comparable</i> (for example, strings and integers).
> */
> @SuppressWarnings({"unchecked"})
> public static <T> boolean isSorted(final T[] a) {
> if (a.length <= 1) {
> // Empty or singleton arrays are always sorted
> return true;
> }
> // Otherwise, check that every element is not smaller than the
> previous
> T previous = a[0];
> for (int i = 1, n = a.length; i < n; i++) {
> final T current = a[i];
> if (((Comparable<? super T>) previous).compareTo(previous) > 0) {
> return false;
> }
> previous = current;
> }
> return true;
> }
> {code}
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)