title: Filling sparse matrix in Eigen is very slow
body: <p>I have this code which calls a c++ method in java in a for loop:</p>
  
  <pre><code>JNIEXPORT void JNICALL Java_com_jp_algi_CoreC_MMload(JNIEnv *env3, jobject clazz3, jdoubleArray inputv, jintArray inputi, jint poc, jint pozic)
  {
      jdouble* fltv2 ;
      jint* fltind2;
      jsize sizedat = env3-&gt;GetArrayLength(inputi);
      fltv2  = new jdouble[sizedat];
      fltind2  = new jint[sizedat];
      jint i;
      jint jm;
      env3-&gt;GetIntArrayRegion(inputi,0,sizedat,fltind2);
      env3-&gt;GetDoubleArrayRegion(inputv,0,sizedat,fltv2);
  
      // default is column major
      matA.reserve(VectorXi::Constant(1,sizedat));
  
      for ( jm = 0; jm &lt; sizedat; jm++) {
          //matA.insert(fltind2[jm],pozic) = fltv2[jm]; // alternative: mat.coeffRef(i,j) += v_ij;
          matA.insert(fltind2[jm],pozic)= fltv2[jm];
          //matA.insertBack(fltind2[jm],pozic)= fltv2[jm];
          //matA.ins
          //matA.insertBackUncompressed();
          //matA.coeffRef(fltind2[jm],pozic) += fltv2[jm];
          // optional
      }
  
      matA.makeCompressed();
  
      //k++; //blbe zayklenji!!!
      env3-&gt;SetIntArrayRegion(inputi,0,sizedat,fltind2);
      env3-&gt;SetDoubleArrayRegion(inputv,0,sizedat,fltv2);
      delete[] fltv2;
      delete[] fltind2;
  }
  </code></pre>
  
  <p>Where inputv are values of columns of matA.; and inputi are indexes of these values.</p>
  
  <p>I read in the docs for eigen that the insert function is the fastest, and it's ok when the number of non-zero coefficients are about 5000. But when I have 25000 it takes 5 sec per column!</p>
  
  <p>I tried insrtback, but the values are the same? What exactly does this command do? Is there some way to improve this code?</p>
  
  <p>Once advantage (maybe): the values and indexes in every column are sorted by the values from highest to lowest...</p>