如何从文本文件创建快速图表? [英] How to create a fast graph from a text file ?
问题描述
我想从包含36692个节点的文本文件中创建一个图形,文本的每一行都包含图形边缘的源节点和目标节点。我使用igraph库来创建图形。我写了下面的代码,但它太慢了。如何将整个文本文件读入内存缓冲区以使算法更快?
I want to make a graph from a text file containing 36692 nodes and each line of the text contains the source and the target node of an edge of the graph. I used the igraph library to create the graph. I wrote the following code but it is too slow. how can I read the whole text file into memory buffer to make the algorithm faster?
igraph_empty(&graph, vertexNum, 0);
ifstream inputFile("Email-Enron.txt");
int v1, v2;
while( ( inputFile >> v1 >> v2 ))
{
igraph_add_edge(&graph, v1, v2);
}
我的尝试:
此代码适用于小数据集,但大数据集需要很长时间。
What I have tried:
this code works for small data set, but it takes a long time for big dataset.
推荐答案
读取一个数据集,然后将其添加到图表一遍又一遍地使代码变慢,因为每次都是磁盘访问而不是图形添加。
Reading one data set and than adding it to the graph over and over again makes the code slow, because everytime is a disk access and than the graph addition.
int countLines = vertextNum;// compute size
int *v = new int[2*countLines];//double the amount of data
int i = 0;
//read all data
for( int i = 0; i < countLines; i += 2)
{
inputFile >> v[i] >> v[i+1];
}
//make graph
for( int i = 0; i < countLines; i += 2)
{
igraph_add_edge(&graph, v[i], v[i+1] );
}
如果可以在加载过程中停用重新计算或绘制图形。
如果发布代码是comimg some编译器中的优化标志和与展开相关的链接器。
提示:真正的快速解决方案是int字节在二进制文件中,你可以读取它们直接。这将使这一单行读取:
If it is possible deactivate recalculating or drawing of the graph in the load process.
For release code are comimg some optimization flags in the compiler and linkers in relevance like unrolling.
Tip: the real fast solution would be that the int bytes are in a binary file and you may read them direct. This would make this single one reading line:
inputFile.read( (char*)v, sizeof(unsigned int) * 2 * countLines);
数据以这种方式编写
Data was written in that manner
outputFile.write( (char*)v, sizeof(unsigned int) * 2 * countLines);
您可能最好使用 igraph_add_edges
函数,一次添加多个边,如 igraph参考手册 [ ^ ]。
You probably better use the igraph_add_edges
function, for adding multiple edges at once, as shown in the igraph Reference Manual[^].
这篇关于如何从文本文件创建快速图表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!