我想读一个大文件,而一行的第一个字符不是&#t; t" &#34 ;. 但是我写的代码非常慢。我怎样才能加快日常工作? 是否有更好的解决方案而不是getline?
void readString(const char *fn)
{
FILE *fp;
char *vString;
struct stat fdstat;
int stat_res;
stat_res = stat(fn, &fdstat);
fp = fopen(fn, "r+b");
if (fp && !stat_res)
{
vString = (char *)calloc(fdstat.st_size + 1, sizeof(char));
int dataEnd = 1;
size_t len = 0;
int emptyLine = 1;
char **linePtr = malloc(sizeof(char*));
*linePtr = NULL;
while(dataEnd)
{
// Check every line
getline(linePtr, &len, fp);
// When data ends, the line begins with space (" ")
if(*linePtr[0] == 0x20)
emptyLine = 0;
// If line begins with space, stop writing
if(emptyLine)
strcat(vString, *linePtr);
else
dataEnd = 0;
}
strcat(vString, "\0");
free(linePtr);
linePtr = NULL;
}
}
int main(int argc, char **argv){
readString(argv[1]);
return EXIT_SUCCESS;
}
答案 0 :(得分:3)
如何加快日常工作?
您的计划表现最可疑的方面是strcat()
。在每次调用时,它需要从头开始扫描整个目标字符串,以找到附加源字符串的位置。因此,如果文件的行的长度由常量(即使是大的)限定,那么您的方法的性能会随着文件长度的平方而缩放。
渐近复杂性分析并不一定能说明整个故事。代码的I / O部分与文件长度呈线性关系,并且由于I / O比内存数据操作昂贵得多,因此对于足够小的文件来说,这将主导您的性能。如果你在那个政权,那么你可能不会比你已经做得更好。但是,在这种情况下,您可以通过fread()
一次阅读整个文件,然后通过strstr()
扫描数据结尾来做得更好:
size_t nread = fread(vString, 1, fdstat.st_size, fp);
// Handle nread != fdstat.st_size ...
// terminate the buffer as a string
vString[nread] = '\0';
// truncate the string after the end-of-data:
char *eod = strstr(vString, "\n ");
if (eod) {
// terminator found - truncate the string after the newline
eod[1] = '\0';
} // else no terminator found
它可以线性扩展,因此它也可以解决您的渐近复杂性问题,但如果感兴趣的数据通常比文件短得多,那么在这种情况下,它会比您更昂贵的I / O需要做。在那种情况下,正如@laissez_faire建议的那样,一种替代方案是阅读大块。另一种方法是调整原始算法以跟踪vString
的结尾,以便使用strcpy()
代替strcat()
来附加每个新行。该版本的关键部分如下所示:
char *linePtr = NULL;
size_t nread = 0;
size_t len = 0;
*vString = '\0'; // In case the first line is end-of-data
for (char *end = vString; ; end += nread) {
// Check every line
nread = getline(&linePtr, &len, fp);
if (nread < 0) {
// handle eof or error ...
}
// When data ends, the line begins with space (" ")
if (*linePtr == ' ') {
break;
}
strcpy(end, *linePtr);
}
free(linePtr);
另外,请注意
你不需要最初为*vString
分配的内存零填充,因为你只是用真正感兴趣的数据覆盖那些零(然后忽略其余的缓冲区) )。
您不应转换malloc()
- 家庭功能的返回值,包括calloc()
。
答案 1 :(得分:0)
您是否尝试使用fread读取文件并在每个步骤中读取更大的数据块,然后在读取后解析数据?类似的东西:
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
char *readString(const char *fn)
{
FILE *fp;
char *vString;
struct stat fdstat;
int stat_res;
stat_res = stat(fn, &fdstat);
fp = fopen(fn, "r+b");
if (fp && !stat_res) {
vString = (char *) calloc(fdstat.st_size + 1, sizeof(char));
int newline = 1;
int index = 0;
while (index < fdstat.st_size) {
int len =
fdstat.st_size - index >
4096 ? 4096 : fdstat.st_size - index;
char *buffer = (char *) malloc(len);
int read_len = fread(buffer, 1, len, fp);
int i;
if (newline) {
if (read_len > 0 && buffer[0] == ' ') {
return vString;
}
newline = 0;
}
for (i = 0; i < read_len; ++i) {
if (buffer[i] == '\n') {
if (i + 1 < read_len && buffer[i + 1] == ' ') {
memcpy(vString + index, buffer, i + 1);
return vString;
}
newline = 1;
}
}
memcpy(vString + index, buffer, read_len);
index += read_len;
}
}
return vString;
}
int main(int argc, char **argv)
{
char *str = readString(argv[1]);
printf("%s", str);
free(str);
return EXIT_SUCCESS;
}