为什么该C code比这个C ++ code更快?获得文件最大的行 [英] Why is this C code faster than this C++ code ? getting biggest line in file
问题描述
我有一个程序,它基本上是同样的事情,得到一个线的最大长度在一个文件的两个版本,我有大约8万行的文件,在C我的code是多一点点原始的(当然!),比code我在C ++。的C PROGRAMM大约需要2秒运行,而在C ++程序需要10秒来运行(相同的文件,我与测试两种情况下)。但为什么?我期待其采取的时间相同或一点点但不8秒慢!
我的code在C:
的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&string.h中GT;#如果_DEBUG
#定义DEBUG_PATH../Debug/
#其他
#定义DEBUG_PATH
#万一为const char FILE_NAME [] = DEBUG_PATHdata.noun诠释的main()
{
INT SPOS = 0;
INT MAXCOUNT = 0;
INT CPOS = 0;
INT CH;
FILE * in_file中; in_file中FOPEN =(FILE_NAME,R);
如果(in_file中== NULL)
{
的printf(无法打开%s \\ n,FILE_NAME);
出口(8);
} 而(1)
{
CH =龟etc(in_file中);
如果(CH == ||的0x0A CH == EOF)// \\ n或\\ r或\\ r \\ n或最终文件
{
如果((CPOS - SPOS)GT; MAXCOUNT)
MAXCOUNT =(CPOS - SPOS); 如果(CH == EOF)
打破; SPOS = CPOS;
}
其他
CPOS ++;
} FCLOSE(in_file中); 的printf(最大线路长度:%I \\ N,MAXCOUNT); 残培();
返回(0);
}
我的code在C ++:
的#include<&iostream的GT;
#包括LT&;&的fstream GT;
#包括LT&;&stdio.h中GT;
#包括LT&;串GT;使用命名空间std;#IFDEF _DEBUG
#定义FILE_PATH../Debug/data.noun
#其他
#定义FILE_PATHdata.noun
#万一诠释的main()
{
字符串文件名= FILE_PATH;
字符串s =;
ifstream的文件;
INT大小= 0; file.open(fileName.c_str());
如果(!文件)
{
的printf(无法打开文件!);
返回0;
} 而(函数getline(文件,多个))
大小=(s.length()>大小)? s.length():大小;
file.close(); 的printf(文件最大的行:%I,大小); 的getchar();
返回0;
}
C ++版本不断分配并释放的std ::字符串的实例。内存分配是一个代价高昂的操作。此外,该构造函数/析构函数中执行。
C版采用然而常量内存,而只是没有必要:在单个字符阅读,设置行长度计数器为新值如果较高,对于每一个新行,这就是它
。I have two versions of a program that does basically the same thing, getting the biggest length of a line in a file, I have a file with about 8 thousand lines, my code in C is a little bit more primitive (of course!) than the code I have in C++. The C programm takes about 2 seconds to run, while the program in C++ takes 10 seconds to run (same file I am testing with for both cases). But why? I was expecting it to take the same amount of time or a little bit more but not 8 seconds slower!
my code in C:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if _DEBUG
#define DEBUG_PATH "../Debug/"
#else
#define DEBUG_PATH ""
#endif
const char FILE_NAME[] = DEBUG_PATH "data.noun";
int main()
{
int sPos = 0;
int maxCount = 0;
int cPos = 0;
int ch;
FILE *in_file;
in_file = fopen(FILE_NAME, "r");
if (in_file == NULL)
{
printf("Cannot open %s\n", FILE_NAME);
exit(8);
}
while (1)
{
ch = fgetc(in_file);
if(ch == 0x0A || ch == EOF) // \n or \r or \r\n or end of file
{
if ((cPos - sPos) > maxCount)
maxCount = (cPos - sPos);
if(ch == EOF)
break;
sPos = cPos;
}
else
cPos++;
}
fclose(in_file);
printf("Max line length: %i\n", maxCount);
getch();
return (0);
}
my code in C++:
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <string>
using namespace std;
#ifdef _DEBUG
#define FILE_PATH "../Debug/data.noun"
#else
#define FILE_PATH "data.noun"
#endif
int main()
{
string fileName = FILE_PATH;
string s = "";
ifstream file;
int size = 0;
file.open(fileName.c_str());
if(!file)
{
printf("could not open file!");
return 0;
}
while(getline(file, s) )
size = (s.length() > size) ? s.length() : size;
file.close();
printf("biggest line in file: %i", size);
getchar();
return 0;
}
The C++ version constantly allocates and deallocates instances of std::string. Memory allocation is a costly operation. In addition to that the constructors/destructors are executed.
The C version however uses constant memory, and just does was necessary: Reading in single characters, setting the line-length counter to the new value if higher, for each newline and that's it.
这篇关于为什么该C code比这个C ++ code更快?获得文件最大的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!