c++17vector中string的性能问题std::vectorstd::string。
。。
1,C风格字符串⼀般以char* 或 char str[]表⽰,字符串以'\0'结尾
2,sizeof()获得给定数据类型或变量的内存⼤⼩,strlen()获得字符串长度
3,字符串字⾯量
char* arr = "hello";
arr[1] = 'b'; // 未定义⾏为,⾮法
char arr[] = "hello";
arr[1] = 'b'; // 可以修改
4,原始字符串字⾯量
const char* str = R"(hello "world"!)" // 以R"(开头,以)"结尾
5,C++ std::string类
a, 重载了+、[]、==、!=、<、<= 等运算符
b, string类的c_str()⽅法可以获得⼀个C风格的const char*
c, ⾼级数值转换与低级数值转换
6. string_view (c++ 17):
a, ⽆法直接连接⼀个string和string_view,需要这样:str+sv.data();
b, string_view可以作为参数按值传递给函数,因为其本质是指针和长度,⾮常⾼效
c, ⽆法从string_view隐式构建⼀个string,要么使⽤⼀个显⽰的string构造函数,要么使⽤string_view::data();
d, auto sv = "my string_view"sv; // string_view字⾯量
不错的string介绍:
vector 转换成 string
edit
play_arrow
brightness_4
// C++ program transform a vector into
// a string.
#include <vector>
#include <string>
#include <algorithm>
#include <sstream>
#include <iterator>
#include <iostream>
int main()
{
std::vector<int> vec;
vec.push_back(1);
vec.push_back(2);
vec.push_back(3);
vec.push_back(4);
vec.push_back(5);
vec.push_back(6);
std::ostringstream vts;
if (!pty())
{
// Convert all but the last element to avoid a trailing ","
std::copy(vec.begin(), d()-1,
std::ostream_iterator<int>(vts, ", "));
// Now add the last element with no delimiter
vts << vec.back();
}
std::cout << vts.str() << std::endl;
}
c++ string 分配内存重载。可以查看创建string时内存分配情况
// sso.cpp
#include <iostream>
#include <string>
void* operator new(std::size_t count){
std::cout << "" << count << " bytes" << std::endl;
return malloc(count);
}
void getString(const std::string& str){}
int main() {
std::cout << std::endl;
std::cout << "std::string" << std::endl;
std::string small = "0123456789";
std::string substr = small.substr(5);
std::cout << "" << substr << std::endl;
std::cout << std::endl;
std::cout << "getString" << std::endl;
getString(small);
getString("0123456789");
const char message []= "0123456789";
getString(message);
std::cout << std::endl;
}
对⼤⽂件substr性能测试:
/
/ substr.cpp
#include <chrono>
#include <fstream>
#include <iostream>
#include <random>
#include <sstream>
#include <string>
#include <vector>
#include <string_view>
static const int count = 30;
static const int access = 10000000;
int main(){
std::cout << std::endl;
std::ifstream inFile("");
std::stringstream strStream;
strStream << inFile.rdbuf();
std::string grimmsTales = strStream.str();
size_t size = grimmsTales.size();
std::cout << "Grimms' Fairy Tales size: " << size << std::endl;
std::cout << std::endl;
// random values
std::random_device seed;
std::mt19937 engine(seed());
std::uniform_int_distribution<> uniformDist(0, size - count - 2);
std::vector<int> randValues;
for (auto i = 0; i < access; ++i) randValues.push_back(uniformDist(engine));
auto start = std::chrono::steady_clock::now();
for (auto i = 0; i < access; ++i ) {
grimmsTales.substr(randValues[i], count);//放到vector中时参考下⾯的性能测试,也许不会性能那么差。 }
std::chrono::duration<double> durString= std::chrono::steady_clock::now() - start;
std::cout << "std::string::substr: " << unt() << " seconds" << std::endl;
std::string_view grimmsTalesView{grimmsTales.c_str(), size};
start = std::chrono::steady_clock::now();
for (auto i = 0; i < access; ++i ) {
grimmsTalesView.substr(randValues[i], count);
}
std::chrono::duration<double> durStringView= std::chrono::steady_clock::now() - start;
std::cout << "std::string_view::substr: " << unt() << " seconds" << std::endl;
std::cout << std::endl;
std::cout << "unt()/unt(): " << unt()/unt() << std::endl; std::cout << std::endl;
}
性能测试
⽤c++ 17的string_view替换实现做测试。
string 在没有优化时慢。单在 o3 优化后,反⽽更快。
#include <string_view>
#include <algorithm>
std::vector<std::string>
c++中string的用法split(const std::string& str, const std::string& delims = "")
{
std::vector<std::string> output;
auto first = std::cbegin(str);
while (first != std::cend(str))
{
const auto second = std::find_first_of(first, std::cend(str),
std::cbegin(delims), std::cend(delims));
if (first != second)
break;
first = std::next(second);
}
return output;
}
std::vector<std::string_view>
splitSV(std::string_view strv, std::string_view delims = "")
{
std::vector<std::string_view> output;
size_t first = 0;
while (first < strv.size())
{
const auto second = strv.find_first_of(delims, first);
if (first != second)
if (second == std::string_view::npos)
break;
first = second + 1;
}
return output;
}
const std::string_view LoremIpsumStrv{ "Lorem ipsum dolor sit amet, consectetur adipiscing elit,"
"sed do eiusmod tempor incididuntsuperlongwordsuper ut labore et dolore magna aliqua. Ut enim ad minim veniam, " "quis nostrud exercitation ullamco laboris nisi ut aliquipsuperlongword ex ea commodo consequat. Duis aute" "irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur."
"Excepteur sint occaecat cupidatatsuperlongword non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." }; static void StringSplit(benchmark::State& state) {
std::string str { LoremIpsumStrv };
// Code inside this loop is measured repeatedly
for (auto _ : state) {
auto v = split(str);
benchmark::DoNotOptimize(v);
}
}
// Register the function as a benchmark
BENCHMARK(StringSplit);
static void StringViewSplit(benchmark::State& state) {
for (auto _ : state) {
auto v = splitSV(LoremIpsumStrv);
benchmark::DoNotOptimize(v);
}
}
BENCHMARK(StringViewSplit);
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论