简体   繁体   中英

How can improve my code on implementation of DBMS queries in c++?

As part of course learning activity I have been asked to implement DBMS query in (based on) c++. The sample c++ code is:-

         class Customer
         {
         public:
             string c_id,name,gender;
             int age,phno;
             friend void ticket_book(ifstream&ib1, ifstream &ib2);
         };

         class Ticket
         {
         public:
             string ticket_no,t_origin,t_dest,j_date;
             int berth,amount;
             string status,aadhar_id,bus_no;
             float t_dept,t_arrival;
             friend void ticket_book(ifstream&ib1, ifstream &ib2);
         };

         void ticket_book(ifstream&ib1, ifstream &ib2, Customer hh[], Ticket ee[])
        {
            int flag;
            for(int i=0; i<13;i++)
        {
        ib1 >> hh[i].c_id >> hh[i].name >> hh[i].gender >> hh[i].age >> hh[i].phno;
        }
        for(int i=0; i<14;i++)
        {
            ib2 >> ee[i].ticket_no >> ee[i].t_origin >> ee[i].t_dest >> ee[i].j_date >> ee[i].berth >> ee[i].amount >> ee[i].status >> ee[i].t_dept >> ee[i].t_arrival >> ee[i].aadhar_id >> ee[i].bus_no;
        }
        for(int i=0;i<13;i++)
        {
            flag=0;
            for (int j=0;j<14;j++)
            {
            if (hh[i].c_id==ee[j].aadhar_id)
            {
                flag=1;
            }
            }
            if (flag==0)
            {
            cout << hh[i].c_id <<"\t" << hh[i].name <<"\t" << hh[i].gender << "\t"<<hh[i].age<<"\t"<<hh[i].phno<< endl;
            }
        }

 main()
{
    Customer hh[13];
    Ticket ee[14];
    ifstream ib1("C://Users//xyz//Desktop//customer.txt");
    ifstream ib2("C://Users//xyz//Desktop//ticket.txt");
    cout<<"Retrieve the customer details who has not booked any ticket\n"<<endl;
            ticket_book(ib1,ib2,hh,ee);
            ib1.close();
            ib2.close();
}

The above code is for retrieving details of customer who has not booked any ticket. Similarly i have to implement 20+ queries based on my database using c++ concepts. I have used array of objects for storing each tuple of a table. Each table contents are stored in text file. Now i have implemented 10 queries and it has already crossed 500 lines of code Microsoft visual studio IDE. When i build the project it takes more than a minute and even execution is slow as number of queries increase when i implement in above manner.

So help me how can I improve my code to implement the above scenario so that it is efficient in all aspects like memory, time. Which is the better way to implement DBMS queries in c++? Help me out. Suggestions and edits are welcome. Thanks in advance.

You may be able to speed up your program by reading more data into memory with one request. For example, instead of reading one variable into memory, then the next, read all of the file into memory, then access from memory.

Another alternative is to use memory mapped files. This is OS dependent and not all OS support this feature.

Common bottlenecks (in order):

  • I/O (data transfers)
  • Data formatting (converting from text representation to internal representation and vice-versa)
  • Division & Modulo
  • Branching
  • Loading data cache.

In general, you will gain more performance by improving your I/O than by improving the data cache. However, this depends on the quantity of data and data accesses.

Another way of solving your problem would be through linklist .

Insert and delete operation can easily be performed,which reduces the size of code.

Here is the sample code which you can refer.

template<class T>
class LinkedList
{
 public:
  node<T> *head;
 public:
   LinkedList(){
    head=new node<T>;
    head->llink=head;head->rlink=head;
}
void insertRear(T str){
    node<T> *nxt,*temp;
    temp=new node<T>;
    nxt=this->head->rlink;
    temp->a=str;
    nxt->llink=temp;
    this->head->rlink=temp;
    temp->llink=this->head;
    temp->rlink=nxt;
}

 void deletePos(T str){
    node<T> *i,*nxt,*before;
    for(i=head->llink;i!=head;i=i->llink){
        if(str==i->a) goto del;
    }
    del:
    nxt=i->rlink;
    before=i->llink;

    nxt->llink=before;
    before->rlink=nxt;
    delete i;
  }
};

class Cust
{
 private:
string c_id;
string name;

public:
Cust(){}
Cust(string a,string b){
    c_id=a;
    name=b;
}
friend ostream& operator<<(ostream &out,Cust &c){} 
friend istream& operator>>(istream &in,Cust &c){}
};

int main() {
    LinkedList<Emp> ll;
    Emp c,c1;
    ifstream fin("Emp.txt");
    while(!fin.eof()){
        fin>>c;
        ll.insertRear(c);
    }
    ll.deleteRear();
    fin.close();
    ll.disp();
    return 0;
}

而不是使用数组,指针索引将使代码运行得更快。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM