Why is this C code faster than this C++ code ? getting biggest line in file(为什么这个 C 代码比这个 C++ 代码快?获取文件中的最大行)
问题描述
我有一个程序的两个版本,它们基本上做同样的事情,获取文件中一行的最大长度,我有一个大约有 8000 行的文件,我的 C 代码更原始一点(当然!)比我在 C++ 中的代码.C 程序大约需要 2 秒才能运行,而 C++ 程序需要 10 秒才能运行(我在这两种情况下测试的文件相同).但为什么?我原以为它会花费相同的时间或多一点但不会慢 8 秒!
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!
我的 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
", FILE_NAME);
        exit(8);
    }       
    while (1) 
    {
        ch = fgetc(in_file);
        if(ch == 0x0A || ch == EOF) // 
 or 
 or 
 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
",  maxCount); 
    getch();
    return (0);
}
我的 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;
}
推荐答案
C++ 版本不断地分配和释放 std::string 的实例.内存分配是一项代价高昂的操作.除此之外,还执行构造函数/析构函数.
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.
然而,C 版本使用常量内存,并且这样做是必要的:读取单个字符,如果更高,则将行长度计数器设置为新值,对于每个换行符,就是这样.
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 代码比这个 C++ 代码快?获取文件中的最大行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么这个 C 代码比这个 C++ 代码快?获取文件中的最大行
				
        
 
            
        - 静态初始化顺序失败 2022-01-01
 - STL 中有 dereference_iterator 吗? 2022-01-01
 - 近似搜索的工作原理 2021-01-01
 - 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
 - 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
 - 如何对自定义类的向量使用std::find()? 2022-11-07
 - C++ 协变模板 2021-01-01
 - Stroustrup 的 Simple_window.h 2022-01-01
 - 使用/clr 时出现 LNK2022 错误 2022-01-01
 - 从python回调到c++的选项 2022-11-16
 
						
						
						
						
						
				
				
				
				