QT project performance optimization

QT project performance tuning notes

QT 5.5 was used in the recent project. During the operation of the project, the CPU occupancy rate continued for a period of 25%, and the network was frequently disconnected. Therefore, it was decided to optimize the performance of the project.

The optimization tool is also a performance analysis tool that comes with VS2010. For specific usage, see: http://www.cnblogs.com/smark/archive/2011/10/12/2208039.html

You can select "just my code" to filter out the code you wrote.

Through the performance analysis tool, you can find the function that takes more CPU time, and then optimize it according to the amount of time occupied -> re-analyze -> re-optimize. After multiple optimizations, the CPU occupancy rate is reduced to less than 2%. The following will record several optimization steps that have greatly improved performance:

1. Optimize the string formatting method.

The project has the function of formatting the binary numbers in QByteArray such as "123" into "31, 32, 33", the code used is as follows:

Copy code

 1 QByteArray msg = xxx
 2 
 3 QString  str;
 4 
 5 foreach (quint8 b, msg)
 6 
 7 {
 8 
 9     str.append(QString().sprintf(“%02X”, b));
10 
11 }

Copy code

 

When the msg contains 6,7w characters, this code needs to be executed for 4 to 5s on the lz Core 2 generation i5 machine, because the QString in it will call the new function 6, 7w, which has a great impact on performance. The optimized code is as follows:

Copy code

 1 QString  buildString(const QByteArray& ba)
 2 
 3 {
 4 
 5 static const char ascii[] = {‘0’, ‘1’, ‘2’,‘3’,‘4’,‘5’,‘6’,‘7’,‘8’,‘9’,‘A’,‘B’,‘C’,‘D’,‘E’,‘F’};
 6 
 7 QString buf;
 8 
 9 buf.resize(ba.length() * 3);
10 
11 int i = 0;
12 
13 foreach (quint8 b, ba)
14 
15 {
16 
17     buf[i] = ascii[b >> 4];
18 
19     buf[i + 1] =ascii[b & 0xF];
20 
21     buf[i + 2] = ‘, ’;
22 
23  
24 
25     i += 3;
26 
27 }
28 
29 if (i > 0)
30 
31     i –= 3;
32 
33 buf[i] = ‘\0’;
34 
35 return buf;
36 
37 }

Copy code

 

After re-running, the CPU took about 120ms, and the performance improved dozens of times.

2. Optimize interface refresh

There is a table display function in the interface function, which displays the received data. In the original code, when a piece of data is received and filled into the table, the table scrollTo method is called once. When 1s receives 2, 3 hundred When there are data, the scrollTo method 2, 3 is called, which directly leads to frequent updates of the interface.

The optimization method is to take into account the observation ability of the human eye, fix the refresh frequency, that is, the function call of scrollTo at 1s once, which reduces the interface redrawing by 2 to 300 times and reduces the CUP load.

3. Optimize the update time stamp function

There is a function to update the time stamp in the project, which records whether certain states have timed out. The original code uses QDateTime to record the time stamp. When the data arrives, QDateTime::currentDateTime is called to update the time stamp, and it will be updated frequently when a large amount of data arrives. Time stamp, through performance analysis, it is found that 7% of the total CPU usage is called here. Considering that the time stamp only needs to count the time interval, after optimization, the clock function in time.h is used to time stamp, and this function returns to the number of milliseconds when the program is started. Performing performance analysis again shows that the call here drops to 0.23% of the CPU usage, and the performance is improved significantly.

4. Optimize database operations

There is a database record update operation in the project to update QByteArray to the database. The program uses the stored procedure API in QT to bind the QByteArray variable to the QSqlQuery object. The reference code is as follows:

Copy code

1 QSqlQuery query(db);
2 
3 QByteArray data;
4 
5 query.prepare(“update table set data = ? where id = ?”);
6 
7 query.bindValue(0, data);
8 
9 query.bindValue(1, id);

Copy code

 

Among them, data contains 6,7w pieces of data. It is found that the program takes the most time on query.bindValue(data). After printing the log, it is found that the content in the data must be displayed ascii, so the code is changed to

1 query.bindValue(0, QString(data));

 

Reduce the time for QByteArray to QString conversion.

5. Other

Other optimizations include the adjustment of the data structure, including changing the array of 2,300 data to a map structure for storage.

 

to sum up

Of course, the optimization of the software should follow the corresponding principles, such as not premature optimization, the stability, readability, and scalability of the code are the main goals at the beginning of the project, and only when the performance of the code cannot meet the requirements. Proper optimization. Because the optimization of the code often sacrifices the above three special effects, it is often necessary to balance these characteristics in the software development process.

Guess you like

Origin blog.csdn.net/Mario_z/article/details/84585781