Beispiel #1
0
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
	// Verify input
	char* inputError = "Expected at least 3 parameters\n N - number of nodes\n from - 1xM vector of indices\n to - 1xM vector of indices\n s - (optional) source node in range (1:N)\n Example: [visit] = lemon_bfs(n, from, to, s);";
	
	if (nrhs < 3 ||  !mxIsNumeric(prhs[0]) || !mxIsNumeric(prhs[1]) || !mxIsNumeric(prhs[2]))
		mexErrMsgTxt(inputError);
	
	mwSize m = mxGetN(prhs[1]);
	double* x = mxGetPr(prhs[1]);
	double* y = mxGetPr(prhs[2]);
	mwSize n = (mwSize)mxGetScalar(prhs[0]);
	double s = 0;
	
	if (mxGetM(prhs[1]) != 1 || mxGetM(prhs[2]) != 1 || mxGetN(prhs[2]) != m)
		mexErrMsgTxt(inputError);
	
	if (nrhs > 3)
	{
		if (!mxIsNumeric(prhs[3]))
			mexErrMsgTxt(inputError);
		
		s = mxGetScalar(prhs[3]);
		if (s < 1 || s > n)
			mexErrMsgTxt(inputError);
		
	}
	
	// Read input
	SmartDigraph g;
	g.reserveNode(n);
	g.reserveArc(m);
	
	for (mwIndex i = 0; i < n; i++)
		g.addNode();
	
	for (mwIndex i = 0; i < m; i++)
		g.addArc(g.nodeFromId(x[i] - 1), g.nodeFromId(y[i] - 1));
	
	// Do stuff
	Visitor<SmartDigraph> v;
	BfsVisit<SmartDigraph, Visitor<SmartDigraph>> bfs(g, v);
	if (s != 0)
		bfs.run(g.nodeFromId(s - 1));
	else
		bfs.run();
	
	// Create output
	if (nlhs > 0)
	{
		plhs[0] = mxCreateDoubleMatrix(1, v.list.size(), mxREAL);
		double *visit = mxGetPr(plhs[0]);
		for (mwIndex i = 0; i < v.list.size(); i++)
			visit[i] = g.id(v.list[i]) + 1;
	}
	
	return;
}
int main() {

    int runsNr  = 100000;
    int nodesNr = 100000;
    int trash;
    
   Graph  graph   ( nodesNr );
   double a = .0123;
    
    Timer T(true);

    T.restart();
    /*
   for ( int n = 0 ; n < runsNr ; ++n ) {

      VertexIterator   v , v_end;
      boost::tie( v , v_end ) = vertices(graph);

//      for ( int m = 0 ; m < 100000 ; ++m )   graph[*v] = (a *= 4.*(1.-a));

      while ( v != v_end ) {
//         graph[*v] = (a *= 4.*(1.-a));
//         get ( boost::vertex_bundle , graph )[*v] = (a *= 4.*(1.-a));
//         ((Graph::stored_vertex*)*v)->m_property = (a *= 4.*(1.-a));  // worx w/ list only!!
         ++v;
        trash++;
      }

   }
    
    cout << "Boost Zeit: " << T.realTime() << endl;
    T.restart();
     

//   for ( int n = 0 ; n < 100 ; ++n ) {
//      
//      auto
//         v      =  graph.vertex_set().begin(),
//         v_end  =  graph.vertex_set().end();
//      
//      while ( v != v_end ) {
////         graph[*v] = (a *= 4.*(1.-a));
//         ++v;
//      }
//   }



   std::list<double> liste ( nodesNr );


   for ( int n = 0 ; n < runsNr ; ++n ) {
      std::list<double>::iterator
         l      = liste.begin(),
         l_end  = liste.end();

//      for ( int m = 0 ; m < 100000 ; ++m )   *l = (a *= 4.*(1.-a));

      while ( l != l_end ) {
//         *l = (a *= 4.*(1.-a));
         ++l;
          trash++;
      }

   }
    cout << "List Zeit: " << T.realTime() << endl;


    
    T.restart();
    ListGraph myGraph;
    for (int i = 0; i < nodesNr; i++) myGraph.addNode();
    cout << "ListDigraph Generation Zeit: " << T.realTime() << endl;
    
    T.restart();
    for (int i = 0; i < runsNr; i++){
        ListGraph::NodeIt nd( myGraph );
        while ( nd != INVALID ) {
//            trash = myGraph.id( nd );
            trash++;
            ++nd;
        }
    }
        
    cout << "ListDigraph Zeit: " << T.realTime() << endl;    

    */
    
    T.restart();
    SmartDigraph mySmart;
    for (int i = 0; i < nodesNr; i++) mySmart.addNode();
    int trashSmart = 0;
    cout << "SmartDigraph Generation Zeit: " << T.realTime() << endl;
    
    T.restart();
    for (int i = 0; i < runsNr; i++) {
        SmartDigraph::NodeIt ns( mySmart );
        while (ns != INVALID) {
            trash = mySmart.id( ns ) * 4;
            trashSmart ++;
            ++ns;
        }
    }
    cout << "SmartDigraph Zeit: " << T.realTime() << endl;
    cout << "trash: " << trashSmart << " | SmartNodes: " << countNodes( mySmart ) << endl;
    
    
}