gpt4 book ai didi

c++ - 为什么锁定会减慢顺序文件解析器的速度?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:41:21 25 4
gpt4 key购买 nike

我为图形文件格式编写了一个简单的阅读器和解析器。问题是它非常慢。以下是相关方法:

Graph METISGraphReader::read(std::string path) {
METISParser parser(path);
std::pair<int64_t, int64_t> header = parser.getHeader();
int64_t n = header.first;
int64_t m = header.second;

Graph G(n);

node u = 0;
while (parser.hasNext()) {
u += 1;
std::vector<node> adjacencies = parser.getNext();
for (node v : adjacencies) {
if (! G.hasEdge(u, v)) {
G.insertEdge(u, v);
}
}
}
return G;
}

std::vector<node> METISParser::getNext() {
std::string line;
bool comment = false;
do {
comment = false;
std::getline(this->graphFile, line);
// check for comment line starting with '%'
if (line[0] == '%') {
comment = true;
TRACE("comment line found");
} else {
return parseLine(line);
}

} while (comment);
}

static std::vector<node> parseLine(std::string line) {
std::stringstream stream(line);
std::string token;
char delim = ' ';
std::vector<node> adjacencies;

// split string and push adjacent nodes
while (std::getline(stream, token, delim)) {
node v = atoi(token.c_str());
adjacencies.push_back(v);
}
return adjacencies;
}

为了诊断它为什么这么慢,我在分析器 (Apple Instruments) 中运行了它。结果令人惊讶:由于锁定开销,速度很慢。该程序 90% 以上的时间都花在 pthread_mutex_lock_pthread_cond_wait 上。

Instruments

我不知道锁定开销从何而来,但我需要摆脱它。你能建议下一步吗?

编辑:查看为 _pthread_con_wait 展开的调用堆栈。通过查看以下内容,我无法找出锁定开销的来源:

enter image description here

最佳答案

展开 _pthread_cond_wait 和 pthread_mutex_lock 调用的调用堆栈,找出调用锁定调用的位置。

作为一种猜测,我会说它存在于您正在执行的所有不必要的堆分配中。堆是线程安全的资源,在此平台上,可以通过互斥体提供线程安全。

关于c++ - 为什么锁定会减慢顺序文件解析器的速度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14483452/

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