简体   繁体   English

相同代码在不同的gcc编译器中具有巨大的性能差异

[英]Same code huge performance difference in different gcc compilers

Trying to understand the cause of the performance difference. 试图了解性能差异的原因。 I'm reading a ~70M file with the function below. 我正在阅读带有以下功能的~70M文件。 Running the code with: gcc 4.4.6 takes less than a second gcc 3.2.3 takes more than 6 minutes 运行代码:gcc 4.4.6需要不到一秒的gcc 3.2.3需要超过6分钟

Most of the time is spend in the assign part. 大部分时间都花在分配部分。 What was changed to make this speed difference between the 2 compilers ? 是什么改变了两个编译器之间的速度差异?

bool ReadFile(const string& path, string& file_data)
{
    ifstream ifs(path.c_str(), ifstream::in | ifstream::ate);
    if (!ifs) return false;
    int size = ifs.tellg();
    if (size==0) return false;
    ifs.seekg(0, ios::beg);
    file_data.assign((istreambuf_iterator<char>(ifs)),
       istreambuf_iterator<char>());

    return true;
}

Could you try tweak a bit this code (one extra line): 你可以尝试调整一下这段代码(一个额外的行):

bool ReadFile(const string& path, string& file_data)
{
    ifstream ifs(path.c_str(), ifstream::in | ifstream::ate);
    if (!ifs) return false;
    int size = ifs.tellg();
    if (size==0) return false;
    ifs.seekg(0, ios::beg);

    file_data.reserve(size);
    file_data.assign((istreambuf_iterator<char>(ifs)),
       istreambuf_iterator<char>());

    return true;
}

and do measurement again 并再次进行测量
In second attempt you can try this: 在第二次尝试中你可以试试这个:

bool ReadFile(const string& path, string& file_data)
{
    ifstream ifs(path.c_str(), ifstream::in | ifstream::ate);
    if (!ifs) return false;
    int size = ifs.tellg();
    if (size==0) return false;
    ifs.seekg(0, ios::beg);

    file_data.resize(size);
    return ifs.read(&file_data[0], size);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 GCC和Clang代码性能的巨大差异 - Drastic difference in GCC and Clang code performance 不同版本的编译器(例如 GCC)会产生不同的性能吗? - Do different versions of compilers (e.g GCC) generate different performance? 编译器的不同结果:(gcc 4.8.1)和gcc(4.3.2) - Different results for compilers: (gcc 4.8.1) and gcc( 4.3.2) snprintf:相同的代码-不同的g ++编译器具有不同的错误/警告 - snprintf: Same code - different errors/warnings on different g++ compilers 具有相同golang代码段的巨大性能差异查询mysql数据库 - Huge performance difference query mysql database with same golang snippet 只需一行代码,C ++的巨大性能差异 - c++ huge performance difference by just one line of code Mac和Linux下C ++程序(与GCC一起编译)的巨大性能差异 - Huge performance difference of a C++ program (compiled with GCC) under Mac and Linux MSVC和GCC之间的性能差异,用于高度优化的矩阵乘法代码 - Difference in performance between MSVC and GCC for highly optimized matrix multplication code 相同的代码执行两次:性能差异 - Same code executed twice : performance difference 当相同的源代码在两个不同的编译器下给出不同的答案时,它意味着什么? - What does it mean when the same source code gives different answers under two different compilers?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM