Uploaded image for project: 'Commons Math'
  1. Commons Math
  2. MATH-628

use only SparseIterator, on RealVectors, that implement SpareRealVectors

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Minor
    • Resolution: Won't Fix
    • None
    • 3.2
    • None
    • None

    Description

      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:

      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)));
              }
          }
      

      Patch will follow.

      Attachments

        1. ArrayRealVector.diff
          3 kB
          Arne Plöse

        Issue Links

          Activity

            People

              Unassigned Unassigned
              aploese Arne Plöse
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: