我为图形文件格式编写了一个简单的阅读器和解析器。问题是它非常慢。以下是相关方法:
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
.
我不知道锁定开销来自哪里,但我需要摆脱它。你能建议接下来的步骤吗?
编辑:查看为_pthread_con_wait
. 通过查看以下内容,我无法找出锁定开销的来源: