简体   繁体   English

如何改进我在 C++ 中实现 DBMS 查询的代码?

[英]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++.作为课程学习活动的一部分,我被要求在(基于)c++ 中实现 DBMS 查询。 The sample c++ code is:-示例 C++ 代码是:-

         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.同样,我必须使用 C++ 概念基于我的数据库实现 20 多个查询。 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.现在我已经实现了 10 个查询,它已经超过了 500 行代码 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++?在 C++ 中实现 DBMS 查询的更好方法是什么? 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) I/O(数据传输)
  • 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.通常,通过改进 I/O 比通过改进数据缓存可以获得更高的性能。 However, this depends on the quantity of data and data accesses.但是,这取决于数据量和数据访问量。

Another way of solving your problem would be through linklist .解决问题的另一种方法是通过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;
}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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