Qt工作笔记-QVector与QMap查找效率实战

因为项目使用QVector,太慢了,听说QMap比QVector查找时快,所以写一个小程序试试;

从30000个数据中找5000个

程序运行截图如下:

QVector

Qt工作笔记-QVector与QMap查找效率实战_第1张图片

QMap

Qt工作笔记-QVector与QMap查找效率实战_第2张图片


一样的数据,找一样的数


代码如下图:

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include 
#include 
#include 

namespace Ui {
class Widget;
}

typedef struct MyData{
    MyData(QString num1,QString num2,QString num3){
        m_num1=num1;
        m_num2=num2;
        m_num3=num3;
    }

    QString m_num1;
    QString m_num2;
    QString m_num3;
}MyData;

typedef struct VecData{
    VecData(const qint64 num,const QString name){
        m_num=num;
        m_name=name;
    }
    qint64 m_num;
    QString m_name;

    QList m_list;
}VecData;

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

public slots:
    void vectorBtnClicked();
    void mapBtnClicked();

private:
    Ui::Widget *ui;
    QVector m_myVec;
    QMap m_myMap;
    qint64 m_num[5000];

};

#endif // WIDGET_H


widget.cpp

#include "widget.h"
#include "ui_widget.h"
#include 
#include 
#include 
#include 
#include 

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    this->setWindowTitle("CSDN IT1995");

    qint64 data=100000000;
    for(qint64 i=0;i<30000;i++){
        m_myVec.append(new VecData(data,"这个是name"));
        m_myMap.insert(data,new VecData(data,"这个是name"));
        for(int j=0;j<100;j++){
            m_myVec[i]->m_list.append(new MyData("10000","20000","30000"));
            (*m_myMap.find(data))->m_list.append(new MyData("10000","20000","30000"));
        }
        data++;
    }

    qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime()));
    for(int i=0;i<5000;i++){
        m_num[i]=qrand()%100000+100000000;
    }

    connect(ui->vectorPushButton,&QPushButton::clicked,this,&Widget::vectorBtnClicked);
    connect(ui->mapPushButton,&QPushButton::clicked,this,&Widget::mapBtnClicked);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::vectorBtnClicked(){

    clock_t start,finish;
    double totaltime;
    start=clock();
    for(int i=0;i<5000;i++){
        for(int j=0;jm_num){
                qDebug()<<"Value:"<m_num;
                break;
            }
        }
    }
    finish=clock();
    totaltime=(double)(finish-start)/CLOCKS_PER_SEC;
    qDebug()<<"此程序的运行时间为"<m_num;
    }
    finish=clock();
    totaltime=(double)(finish-start)/CLOCKS_PER_SEC;
    qDebug()<<"\n此程序的运行时间为"<


main.cpp

#include "widget.h"
#include 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();

    return a.exec();
}

你可能感兴趣的:(C/C++,Qt,工作笔记)