Exemple #1
0
int main13123131(int argc, const char * argv[])
{
    srand((unsigned)time(NULL));
    int N;
    N = 100000;
    clock_t begin,end;
    ST st;
    for (int i=0;i<N;i++)
    {
        Item it;
        it.rand();
  //      cout<<it.key()<<" "<<it.INFO()<<endl;
        st.insert(it);
    }
    Key v = 0;
    double result = 0;
    for (int i = 0; i < 10000; i++) {
        v=1 + (int) (element * (::rand() / (RAND_MAX + 1.0)));
        begin = clock();
        st.search(v);
        end = clock();
        double t = double (end-begin)/CLOCKS_PER_SEC * 1000;
        result += t;
    }
    result=result/10000*1000;
    
    cout<<"ST contains: "<<st.count()<<" elements"<<endl;
  //  if (result.null())
  //  cout<<"Can not find the Key:"<<v;
  //  else
  //  cout<<"Key: "<<result.key()<<" Value:"<<result.INFO();
   // double timeConsumed_ = double (end-begin)/CLOCKS_PER_SEC * 1000;
    cout<<endl<<result<<" ms";
    
    return 0;
    
}
Exemple #2
0
int maintest()
{
    srand((unsigned)time(NULL));
 //   clock_t begin,end;
  /*  ST st;
    
    //INSERT ELEMENTS
    for (int i=0;i<N;i++)
    {
        Item it(i,i*0.1);
        
        //      cout<<it.key()<<" "<<it.INFO()<<endl;
        st.insert(it);
    }
    
    
    Key v = 0;
    double result = 0;
    
    for (int i = 0; i < repeat; i++) {
        v=1 + (int) (N * (::rand() / (RAND_MAX + 1.0)));
        begin = clock();
        st.search(v).INFO();
        end = clock();
        double t = double (end-begin)/CLOCKS_PER_SEC * 1000;
        result += t;
    }
    result=result/repeat;
    cout<<"ST contains: "<<st.count()<<" elements"<<endl;
    cout<<"The search cost "<<result<<" ms";
    */
    
    /*
     Binary search st
     */
    
    int key_[N];
    float val_[N];
    for (int i=0; i<N ; i++) {
        key_[i] = 0;
        val_[i] = 0;
    }
    ST bsst;
    for (int i=0; i<N; i++) {
        Item item(i,i*0.1);
        //insert items
        if (binarysearch2(item.key(),0, bsst.count(),key_) == -1) {
            int j=bsst.count();
            while(item.key()<key_[j-1]) {
                if(j==0)
                    break;
                key_[j]=key_[j-1];
                val_[j]=val_[j-1];
                j--;
            }
            key_[j]=item.key();
            val_[j]=item.INFO();
        }
        else {
            val_[binarysearch2(item.key(),0, bsst.count(),key_)] = item.INFO();
        }
        bsst.insert(item);
    }
   
    Key v = 0;
    double result = 0;
    clock_t begin,end;
    begin = clock();
    for (int i = 0; i < repeat; i++) {
        v=1 + (int) (N * (::rand() / (RAND_MAX + 1.0)));
        Item x = bsst.search(N/2);
    }
    end = clock();
    result=double (end-begin)/CLOCKS_PER_SEC * 1000;
    cout<<"ST contains: "<<bsst.count()<<" elements"<<endl;
    cout<<"The search cost "<<result<<" ms";

    

    
    result = 0;
    int max = bsst.count()-1;
    begin = clock();
    for (int i=0; i <repeat; i++) {
        v=1 + (int) (N * (::rand() / (RAND_MAX + 1.0)));
        binarysearch2(max,0, max,key_);
    }
    end = clock();
    result=double (end-begin)/CLOCKS_PER_SEC * 1000;
   // result=result/repeat;
     
    
    cout<<endl<<"BSST contains: "<<bsst.count()<<" elements"<<endl;
    cout<<"The search cost "<<result<<" ms";
    
    
    
    return 0;
    
}