commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Arne Plöse (JIRA) <j...@apache.org>
Subject [jira] [Commented] (MATH-628) use only SparseIterator, on RealVectors, that implement SpareRealVectors
Date Sun, 24 Jul 2011 18:03:09 GMT

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

Arne Plöse commented on MATH-628:
---------------------------------

Im off for two weeks, but here some quick results:

I modified my testcase to use the profiler from NetBeans...

{code}
package org.apache.commons.math.linear;

import java.util.Iterator;
import org.junit.BeforeClass;
import org.junit.Test;

/**
 *
 * @author aploese
 */
public class PerformanceTest {

    final static int ITER = 1000;
    final static int VECTOR_SIZE = 2048;
    final static double SPARSE_FILL_STATE = 0.8;
    final static double NANO_TO_MILLI = 1000000;
    static double[] data;
    
    @BeforeClass
    public static void beforeClass() {
        data = new double[VECTOR_SIZE];
        for (int i = 0; i < VECTOR_SIZE; i++) {
            data[i] = i;
        }
    }
    
    @Test
    public void performanceOpenMapRealVector() {
        System.out.println("performanceOpenMapRealVector [ms]");
        RealVector v = new OpenMapRealVector(VECTOR_SIZE);
        for (int i = 0; i < v.getDimension() * SPARSE_FILL_STATE; i++) {
            v.setEntry(i, i);
        }
        for (int j = 0; j < ITER; j++) {
            iterateOver(v);
            randomAccess(v);
        }
    }

    private double[] randomAccess(RealVector v) {
        final int dim = v.getDimension(); 
        double[] result = new double[dim];
        for (int i = 0; i < dim; i++) {
            result[i] = data[i] + v.getEntry(i);
        }
        return result;
    }

    private double[] iterateOver(RealVector v) {
        double[] result = data.clone();
        Iterator<RealVector.Entry> it = v.sparseIterator();
        RealVector.Entry e;
        while (it.hasNext() && (e = it.next()) != null) {
            result[e.getIndex()] += e.getValue();
        }
        return result;
    }

    @Test
    public void performanceArrayRealvecor() {
        System.out.println("performanceArrayRealvecor [ms]");
        RealVector v = new ArrayRealVector(VECTOR_SIZE);
        for (int i = 0; i < v.getDimension() * SPARSE_FILL_STATE; i++) {
            v.setEntry(i, i);
        }
        for (int j = 0; j < ITER; j++) {
            double a = 0;
            iterateOver(v);
            randomAccess(v);
        }
    }
}
{code}

here the results (all times im ms):

performanceOpenMapRealVector = 287
 iterateOver =  61.3
 random Access = 49.2

performanceOpenMapRealVector = 45.1
 iterateOver = 27.0
 random Access = 8.51

the initialization of OpenMapRealVector took 147 ms.

so these are values I had expected. ArrayRealvector is best, Iteration is with much overhead.
 

> use only SparseIterator, on RealVectors, that implement SpareRealVectors 
> -------------------------------------------------------------------------
>
>                 Key: MATH-628
>                 URL: https://issues.apache.org/jira/browse/MATH-628
>             Project: Commons Math
>          Issue Type: Improvement
>    Affects Versions: 3.0
>            Reporter: Arne Plöse
>            Priority: Minor
>         Attachments: ArrayRealVector.diff
>
>
> the performance suffers badly if one tries to use SparseIterator when there is no sparse
backing.
> Currently there is only a check for ArrayRealvector, all other is supposed to be a SparseRealvector.
> If one creates a new implementation of RealVector ... bang.
> here is a performance test case:
> {code}
> package org.apache.commons.math.linear;
> import java.util.Iterator;
> import org.junit.Test;
> /**
>  *
>  * @author aploese
>  */
> public class PerformanceTest {
>     final static int ITER = 100;
>     final static int VECTOR_SIZE = 2048;
>     final static double SPARSE_FILL_STATE = 0.8;
>     @Test
>     public void performanceSparseVectorOpenMap() {
>         System.out.println("performanceSparseVectorOpenMap");
>         RealVector v = new OpenMapRealVector(VECTOR_SIZE);
>         for (int i = 0; i < v.getDimension() * SPARSE_FILL_STATE; i++) {
>             v.setEntry(i, i);
>         }
>         for (int j = 0; j < ITER; j++) {
>             long t1 = System.nanoTime();
>             double a = 0;
>             Iterator<RealVector.Entry> it = v.sparseIterator();
>             RealVector.Entry e;
>             while (it.hasNext() && (e = it.next()) != null) {
>                 a += e.getValue();
>             }
>             long t2 = System.nanoTime();
>             for (int i = 0; i < v.getDimension(); i++) {
>                 a += v.getEntry(i);
>             }
>             long t3 = System.nanoTime();
>             System.out.println(String.format("OpenMap: %d\t%s\t| %s", j, Long.toString(t2
- t1), Long.toString(t3 - t2)));
>         }
>     }
>     @Test
>     public void performanceSparseVectorArray() {
>         System.out.println("performanceSparseVectorArray");
>         RealVector v = new ArrayRealVector(VECTOR_SIZE);
>         for (int i = 0; i < v.getDimension() * SPARSE_FILL_STATE; i++) {
>             v.setEntry(i, i);
>         }
>         for (int j = 0; j < ITER; j++) {
>             long t1 = System.nanoTime();
>             double a = 0;
>             Iterator<RealVector.Entry> it = v.sparseIterator();
>             RealVector.Entry e;
>             while (it.hasNext() && (e = it.next()) != null) {
>                 a += e.getValue();
>             }
>             long t2 = System.nanoTime();
>             for (int i = 0; i < v.getDimension(); i++) {
>                 a += v.getEntry(i);
>             }
>             long t3 = System.nanoTime();
>             System.out.println(String.format("Array: %d\t%s\t| %s", j, Long.toString(t2
- t1), Long.toString(t3 - t2)));
>         }
>     }
> {code}
> Patch will follow.

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

       

Mime
View raw message