gpt4 book ai didi

c++ - 排序字符串 vector : plain C vs idiomatic C++11

转载 作者:IT老高 更新时间:2023-10-28 21:38:09 26 4
gpt4 key购买 nike

我目前正在尝试学习 C++11 及其花哨的功能。具体来说,我正在寻找高效的通用性。所以我很高兴地用 C++11 编写了一个程序来对输入文件的行进行排序,以测试我的新技能。由于 C++ 编译器的内联和很好的特性,我期望这个小例子有高性能。为了提示我的程序有多快,我使用 qsort 函数在 C 中破解了完全相同的程序,因为它是原始 C 没有对此函数执行内联,并且我的比较函数被调用间接访问,需要做两次间接访问 char * 表示字符串的指针。

事实

然而,我对结果感到非常惊讶,C 似乎比 C++ 快 4 倍。在 8Mb 文件上,我得到以下结果:

$ g++ -O3 -std=c++11 -o sort sort.C
$ time ./sort < huge > /dev/null

real 0m0.415s
user 0m0.397s
sys 0m0.013s

$ cc -O3 -Wall -o sortc sort.c
$ time ./sortc < huge > /dev/null

real 0m0.104s
user 0m0.097s
sys 0m0.010s

$ wc -l huge
140190 huge

请注意,我尽量做到公平,编译选项是相同的,我的 C 程序(稍后转储)的行为方式与 C++ 程序相同:输入行的大小没有限制,也没有限制输入行数。

我还注意到,虽然我的 C 程序几乎为每个输入行调用一次 malloc,但 C++ 程序每个输入行的分配比例为 10!

代码

这是我用来比较的两个程序。

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <memory>

int main () {
typedef std::vector<std::string> svec;
svec a;
std::string s;

for (;;) {
getline(std::cin, s);
if (std::cin.eof()) {
if (s != "")
a.push_back(std::move(s));
break;
}
a.push_back(std::move(s));
}
std::sort(a.begin(), a.end());
for (std::string &s : a) {
std::cout << s << "\n";
}
}

还有我更详细的 C 版本。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define BUFSZ 100
size_t getl(char **line, size_t len) {
char buf[BUFSZ];
size_t i, n;

for (i=0; i<BUFSZ; i++) {
int c = getchar();

if (c == EOF || c == '\n') {
*line = malloc(len+i+1);
memcpy(&(*line)[len], buf, i);
(*line)[len+i] = 0;
return i;
}
buf[i] = c;
}

n = getl(line, len+i);
memcpy(&(*line)[len], buf, i);
return i+n;
}

#define ARRAYSZ 30
struct Array {
char **lv;
size_t li, lc;
};

void addline(struct Array *a, char *line) {
if (a->li == a->lc) {
a->lc *= 2;
a->lv = realloc(a->lv, a->lc * sizeof *a->lv);
}
a->lv[a->li++] = line;
}

int cmp(const void *a, const void *b) {
return strcmp(*(const char **)a, *(const char **)b);
}

int main(void) {
char *line;
struct Array a;
size_t i;

a.li = 0;
a.lc = ARRAYSZ;
a.lv = malloc(a.lc * sizeof *a.lv);

for (;;) {
getl(&line, 0);
if (feof(stdin)) {
if (line[0] != 0)
addline(&a, line);
else
free(line);
break;
}
addline(&a, line);
}
qsort(a.lv, a.li, sizeof *a.lv, cmp);
for (i=0; i<a.li; i++) {
printf("%s\n", a.lv[i]);
free(a.lv[i]);
}
free(a.lv);
return 0;
}

问题

有人能告诉我我的 C++ 程序必须在哪里更改(而不是变成纯 C)才能更快吗?我试图保持非常地道,这是破解 C++ 的好方法,还是当我想要高性能时应该倾向于编写类似 C 的代码?为什么 C++ 程序在堆上分配这么多,我该如何减少呢?

编辑

根据大众的需求,我展示了我的 C++ 程序的分析结果。这是我的 C++ 程序的分析器的有趣输出(前两行):

Each sample counts as 0.01 seconds.
% cumulative self self total
time seconds seconds calls ms/call ms/call name
40.03 0.02 0.02 1198484 0.00 0.00 __gnu_cxx::__normal_iterator<std::string*, std::vector<std::string, std::allocator<std::string> > >::operator--()
30.02 0.04 0.02 2206802 0.00 0.00 bool std::operator< <char, std::char_traits<char>, std::allocator<char> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)

当我读到它时,似乎分配不是唯一的原因。

最佳答案

原因在于 c++ std io 同步。以下代码:

int main () {
typedef std::vector<std::string> svec;
svec a;
std::string s;

// note
std::ios_base::sync_with_stdio(false);

for (;;) {
getline(std::cin, s);
if (std::cin.eof()) {
if (s != "")
a.push_back(std::move(s));
break;
}
a.push_back(std::move(s));
}
std::sort(a.begin(), a.end());
for (std::string &s : a) {
std::cout << s << "\n";
}
}

给予

 real   0m0.106s
user 0m0.104s
sys 0m0.004s

C 版本给出了这个:

 real   0m0.167s
user 0m0.164s
sys 0m0.000s

编辑:正如 RiaD 正确提到的,sync_with_stdio 当然是静态函数,因此对所有 std io 流调用一次函数就足够了。

关于c++ - 排序字符串 vector : plain C vs idiomatic C++11,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11985859/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com