目录
测试使用 vector.size 与 使用 标志位
使用 标志位判断vector 是否为null,将分别测试千、万、十万、百万级耗时对比。
判null
经测试,发现使用visual studio 比 clion 配置的 visual studio 更省时,
头文件 :RelevantSizeFuncTest.h
//
// Created by ly on 2022/7/12. size 性能测试
//
#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
源文件: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 << "标志位耗时: " << 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耗时: " << 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耗时: " << interval * 1000 << "ms" << endl;
}
void RelevantSizeFuncTest::getSizeTestNewT() {
//thread t1(&RelevantSizeFuncTest::getSizeTest,this);
}
使用 visual studio 测试
发现千万级用时相差 不是不大。
百万级
使用Clion 配置的 vs 2019
使用次配置, 发现运行缓慢(不建议使用)
经多次测试:千级 耗时 相差不大
五千级别:相差大概几时毫秒
万级测试:结果不太稳定,多数时候,时间相差不大 。标志问耗时比较稳定。多数时候,相差几十毫秒。
十万级,标志位大概节省2千毫秒
百万级:标志位耗时最少
经测试,使用标志位判null ,从十万级数量级起,标志位耗时更少,测试不精确,大概数量级越大,标志位耗时越少。仅使用clion 测试,结果不正确,弃用
集中方法耗时差别不大
注意:我曾使用多线程测试,但测试结果多线程更耗时。
循环
但是在c++中,一般情况下,类中定义了函数体的函数会自动被编译器视为内联函数,而内联函数的特点是它会在程序运行时讲代码段插入,即在调用内联函数的地方把函数的内容展开,从而省去了函数调用的开销。
循环时,不建议使用 for(int i = 0; i < list.size():i++)
//
// Created by ly on 2022/7/12. size 性能测试
//
#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 << "标志位耗时: " << 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耗时: " << 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耗时: " << 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耗时: " << 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耗时: " << interval * 1000 << "ms" << endl;
}