commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Gilles (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (MATH-1130) A new set of functions for copyof, remove and replace a given value on a slice of array
Date Wed, 25 Jun 2014 23:27:24 GMT

    [ https://issues.apache.org/jira/browse/MATH-1130?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14044169#comment-14044169
] 

Gilles commented on MATH-1130:
------------------------------

bq. What is the test code used or the micro benchmark?

It's in {{src/test/java/org/apache/commons/math3/PerfTestUtils.java}}.

The code I used is
{code}
package org.apache.commons.math3.util;

import org.apache.commons.math3.PerfTestUtils;
import org.apache.commons.math3.distribution.RealDistribution;
import org.apache.commons.math3.distribution.UniformRealDistribution;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.Assert;

/**
 * Performance tests for "Precision.equals".
 * Not enabled by default, as the class does not end in Test.
 * 
 * Invoke by running<br/>
 * {@code mvn test -Dtest=EqualsTestPerf}<br/>
 * or by running<br/>
 * {@code mvn test -Dtest=EqualsTestPerf -DargLine="-DtestRuns=1234 -server"}<br/>
 */
public class EqualsTestPerf {
    private static final int RUNS = Integer.parseInt(System.getProperty("testRuns","10000000"));

    @Test
    public void testSimpleBenchmark() {
        final String D = "CM";
        final String DM = "V";

        final int numStat = 100;
        final int numCall = RUNS / numStat;

        final RealDistribution d = new UniformRealDistribution(0, Double.MAX_VALUE);
        final double v = d.sample();
        final double w = d.sample();

        PerfTestUtils.timeAndReport("equalsIncludingNaN",
                                    numCall,
                                    numStat,
                                    false,
                                    new PerfTestUtils.RunTest(D) {
                                        @Override
                                        public Double call() throws Exception {
                                            return Precision.equalsIncludingNaN(v, w) ? 1d
: 0d;
                                        }
                                    },
                                    new PerfTestUtils.RunTest(DM) {
                                        @Override
                                        public Double call() throws Exception {
                                            return EqualsTestPerf.equalsIncludingNaN(v, w)
? 1d : 0d;
                                        }
                                    });
    }

    public static boolean equalsIncludingNaN(double x, double y) {
        return (x != x || y != y) ?
            !(x != x ^ y != y) :
            Precision.equals(x, y, 1);
    }
}
{code}

bq. What does N correspond to?

The value of the "testRuns" property.

bq. Basically If only one of them is NaN it does not make sense to get to a detailed compare
which is what i have eliminated.

OK. I got it. In that case (only one argument is NaN), your code should be faster indeed.
In the general case (none of the arguments is NaN), it's not that obvious, and the benchmarking
code does not seem to help figuring it out. You are most welcome to try it (and report flaws...).


> A new set of functions for copyof, remove and replace a given value on a slice of array
> ---------------------------------------------------------------------------------------
>
>                 Key: MATH-1130
>                 URL: https://issues.apache.org/jira/browse/MATH-1130
>             Project: Commons Math
>          Issue Type: New Feature
>    Affects Versions: 3.4
>            Reporter: Venkatesha Murthy TS
>         Attachments: equalsIncludingNaN.dat, math-1130-checknotnan.patch, math-1130-precision-equals.patch,
math-1130-remove.patch, math-1130-replace.patch, math-1130.patch
>
>
> These are utility functions mostly required as part of MathArrays.
> MathArrays:
> =============
> The requirement is as follows:
> a) double[] copyOf(double[] values, int begin, int length) ;
> Similar to most other functions that support slice defined  by  the array part from [begin,
begin+length) ;its a requirement to copy a slice which is not available (the closest is copyOf(array,
int len) which misses out the begin index)
> b) double[] removeAll(double[] values, int begin, int length, double removable);
> Need a function to remove a value from array slice defined by [begin,begin+length) and
return the filtered version.
> c) double[] replaceAll(double[] values, int begin, int length, double oldValue, double
newValue);
> Need a function to replace inplace an oldValue substituted with newValue in the array
slice defined by [begin,begin+length) and return the original  complete array with just replaced
values only in the segment [begin,begin+length)
> MathUtils
> =========
> boolean canEqual(double d1, double d2) ;
> provide a canEqual function that is slightly better than exisitng MathUtils.equals. We
could also improve existing equals method however.
> So the change here is that the new enhanced canEqual can do a quick check on Nans  and
then move to a detailed Double.compare(..) method. This avoids the Double.compare call when
any one of them is NaN.



--
This message was sent by Atlassian JIRA
(v6.2#6252)

Mime
View raw message