当前位置:网站首页>Vector size performance problems
Vector size performance problems
2022-07-27 00:44:00 【ly_ zszcyx】
Catalog
Test use vector.size And Use Sign a
Use Flag bit judgment vector Is it null, Thousand will be tested separately 、 ten thousand 、 One hundred thousand 、 Millions of time-consuming comparison .
sentence null
After testing , Discover the use of visual studio Than clion Configured visual studio More time saving ,
The header file :RelevantSizeFuncTest.h
//
// Created by ly on 2022/7/12. size Performance testing
//
#ifndef CPLUSPLUSTESTPRJ_RELEVANTSIZEFUNCTEST_H
#define CPLUSPLUSTESTPRJ_RELEVANTSIZEFUNCTEST_H
#include <vector>
#include <cstring>
#include <string>
using namespace std;
class RelevantSizeFuncTest {
public:
RelevantSizeFuncTest();
~RelevantSizeFuncTest();
void getFlagSizeTest();
void getSizeTest();
void getEmptyTest();
void getSizeTestNewT();
private:
std::vector<string> strListFlag;
std::vector<string> strList;
std::vector<string> strEmList;
};
#endif //CPLUSPLUSTESTPRJ_RELEVANTSIZEFUNCTEST_H
Source file :RelevantSizeFuncTest.cpp
//
// Created by caeri on 2022/7/12.
//
#include<iostream>
#include <thread>
#include "PerformanceTest/RelevantSizeFuncTest.h"
RelevantSizeFuncTest::RelevantSizeFuncTest() {
}
RelevantSizeFuncTest::~RelevantSizeFuncTest() {
}
const int cunt = 1000;
void RelevantSizeFuncTest::getFlagSizeTest() {
auto start = clock();
bool isEmpty = false;
for(int i = 0; i < cunt; i++){
this->strListFlag.push_back(to_string(i));
if(!isEmpty) isEmpty = true;
}
if(isEmpty){
string s = "124";
}
auto end = clock();
double interval = ((double)end - start) / CLOCKS_PER_SEC;
cout << " Flag bit takes time : " << interval * 1000 << "ms" << endl;
}
void RelevantSizeFuncTest::getSizeTest() {
auto start = clock();
for(int i = 0; i < cunt; i++){
this->strList.push_back(to_string(i));
}
if(this->strList.size() > 0){
string s = "124";
}
auto end = clock();
double interval = ((double)end - start) / CLOCKS_PER_SEC;
cout << "size Time consuming : " << interval * 1000 << "ms" << endl;
}
void RelevantSizeFuncTest::getEmptyTest() {
auto start = clock();
for(int i = 0; i < cunt; i++){
this->strEmList.push_back(to_string(i));
}
if(!this->strEmList.empty()){
string s = "124";
}
auto end = clock();
double interval = ((double)end - start) / CLOCKS_PER_SEC;
cout << "empty Time consuming : " << interval * 1000 << "ms" << endl;
}
void RelevantSizeFuncTest::getSizeTestNewT() {
//thread t1(&RelevantSizeFuncTest::getSizeTest,this);
}Use visual studio test
It is found that there is a time difference of ten million levels Not big .

Million level

Use Clion Configured vs 2019
Use secondary configuration , Found running slowly ( Not recommended )

After many tests : Thousand level Time consuming Little difference

5000 level : The difference is about how many milliseconds

10000 level test : The result is not very stable , Most of the time , There is little difference in time . The time consumption of the logo is relatively stable . Most of the time , The difference is tens of milliseconds .


One hundred thousand , Flag bits are probably saved 2 Thousands of milliseconds



Million level : The flag bit takes the least time

After testing , Use the flag bit to judge null , From the order of 100000 , Flag bits take less time , The test is not accurate , The larger the order of magnitude , The less time the flag bit takes . Use only clion test , The result is not correct , Abandoning
Centralized methods take little time
Be careful : I used to use multithreaded testing , But multithreading of test results is more time-consuming .
loop
But in c++ in , In general , Functions in the class that define the function body will automatically be regarded as inline functions by the compiler , The feature of inline function is that it will insert code segments when the program is running , That is, expand the contents of the function where the inline function is called , This saves the cost of function calls .
loop , Not recommended for(int i = 0; i < list.size():i++)
//
// Created by ly on 2022/7/12. size Performance testing
//
#ifndef CPLUSPLUSTESTPRJ_RELEVANTSIZEFUNCTEST_H
#define CPLUSPLUSTESTPRJ_RELEVANTSIZEFUNCTEST_H
#include <vector>
#include <cstring>
#include <string>
using namespace std;
const int cunt = 10000000;
class RelevantSizeFuncTest {
public:
RelevantSizeFuncTest();
~RelevantSizeFuncTest();
void getFlagSizeTest();
void getSizeTest();
void getEmptyTest();
void getSizeTestNewT();
void useSizeTest();
void useTempTest();
private:
std::vector<string> strListFlag;
std::vector<string> strList;
std::vector<string> strEmList;
std::vector<string> comStrList;
};
#endif //CPLUSPLUSTESTPRJ_RELEVANTSIZEFUNCTEST_H
//
// Created by caeri on 2022/7/12.
//
#include<iostream>
#include <thread>
#include "PerformanceTest/RelevantSizeFuncTest.h"
RelevantSizeFuncTest::RelevantSizeFuncTest() {
for (int i = 0; i < cunt; i++) {
comStrList.push_back(to_string(i));
}
}
RelevantSizeFuncTest::~RelevantSizeFuncTest() {
}
void RelevantSizeFuncTest::getFlagSizeTest() {
auto start = clock();
bool isEmpty = false;
for(int i = 0; i < cunt; i++){
this->strListFlag.push_back(to_string(i));
if(!isEmpty) isEmpty = true;
}
if(isEmpty){
string s = "124";
}
auto end = clock();
double interval = ((double)end - start) / CLOCKS_PER_SEC;
cout << " Flag bit takes time : " << interval * 1000 << "ms" << endl;
}
void RelevantSizeFuncTest::getSizeTest() {
auto start = clock();
for(int i = 0; i < cunt; i++){
this->strList.push_back(to_string(i));
}
if(this->strList.size() > 0){
string s = "124";
}
auto end = clock();
double interval = ((double)end - start) / CLOCKS_PER_SEC;
cout << "size Time consuming : " << interval * 1000 << "ms" << endl;
}
void RelevantSizeFuncTest::getEmptyTest() {
auto start = clock();
for(int i = 0; i < cunt; i++){
this->strEmList.push_back(to_string(i));
}
if(!this->strEmList.empty()){
string s = "124";
}
auto end = clock();
double interval = ((double)end - start) / CLOCKS_PER_SEC;
cout << "empty Time consuming : " << interval * 1000 << "ms" << endl;
}
void RelevantSizeFuncTest::getSizeTestNewT() {
//thread t1(&RelevantSizeFuncTest::getSizeTest,this);
}
void RelevantSizeFuncTest::useSizeTest() {
auto start = clock();
for (int i = 0; i < this->comStrList.size(); i++) {
//string s = "124";
}
auto end = clock();
double interval = ((double)end - start) / CLOCKS_PER_SEC;
cout << "size Time consuming : " << interval * 1000 << "ms" << endl;
}
void RelevantSizeFuncTest::useTempTest() {
auto start = clock();
int tempCnt = this->comStrList.size();
for (int i = 0; i < tempCnt; i++) {
//string s = "124";
}
auto end = clock();
double interval = ((double)end - start) / CLOCKS_PER_SEC;
cout << "tempCnt Time consuming : " << interval * 1000 << "ms" << endl;
}


边栏推荐
- Class and object notes I
- Blue Bridge Cup 1004 [recursive] cow story
- Reduced dimension mean dot product matrix multiplicative norm probability normal distribution square loss
- 八皇后 N皇后
- 12_ Decision tree
- Complete review of parsing web pages
- 输入一串字母 将里面的元音输出希望各位大佬能给指导
- Drawing warehouse Tsai
- 【4.7 高斯消元详解】
- Eight queens n Queens
猜你喜欢
随机推荐
C language shutdown applet
Oracle data guard service, process and protection mode
[acwing game 61]
Drawing warehouse-3 (functional image)
Lt9611ux Mipi to HDMI 2.0 dual port with audio
[4.10 detailed explanation of game theory]
[3. VIM operation]
[leetcode] no duplicate longest string
[4.4 detailed explanation of fast power and inverse element of fast power]
并行MPI程序传递发送消息
[Qt]属性
[4.2 approximations]
[qt] meta object system
C language is more than a variety of descriptions of size. Don't stick to one way of writing
运算符重载
[4.6 detailed explanation of Chinese remainder theorem]
JSCORE day_03(7.4)
Today's 20220719 toss deeplobcut
Operator overloading
裁剪tif影像
![[Qt]属性](/img/ca/5f9d8f33e38b0ac5cbb0768a7b3ffd.png)
![[acwing game 61]](/img/83/c9a43536705451a60252720fc41d14.jpg)






