zoukankan      html  css  js  c++  java
  • C++加速

    在网上查看别人的ACM代码时,发现别人输入输出语句用的总是scanf与printf,有点不解,还以为他们用的都是C语言,而非C++,但今天做的一道题(Sort):发现与网上的其他高手使用完全相同的方法,使用scanf及printf的代码提交后Accepted,而使用cin及cout的却Time Limit Exceede;

    加上下面代码后使用cin及cout也可以Accepted:

    std::ios::sync_with_stdio(false);

    百度了一下,原来而cincout之所以效率低,是因为先把要输出的东西存入缓冲区,再输出,导致效率降低,而这段语句可以来打消iostream的输入 输出缓存,可以节省许多时间,使效率与scanf与printf相差无几,还有应注意的是scanf与printf使用的头文件应是stdio.h而不是 iostream。

    以前碰到cin TLE的时候总是傻乎乎地改成scanf,甚至还相信过C++在IO方面效率低下的鬼话,殊不知这只是C++为了兼容C而采取的保守措施。

    tie

    tie是将两个stream绑定的函数,空参数的话返回当前的输出流指针。

    #include <iostream>
    #include <fstream>
    ///SubMain//
    int main(int argc, char *argv[])
    {
     std::ostream *prevstr;
     std::ofstream ofs;
     ofs.open("test.txt");
     
     std::cout << "tie example:
    "; // 直接输出到屏幕
     
     *std::cin.tie() << "This is inserted into cout
    "; // 空参数调用返回默认的output stream,也就是cout
     prevstr = std::cin.tie(&ofs); // cin绑定ofs,返回原来的output stream
     *std::cin.tie() << "This is inserted into the file
    "; // ofs,输出到文件
     std::cin.tie(prevstr); // 恢复
     
     ofs.close();
     system("pause");
     return 0;
    }
    ///End Sub//

    输出:

    tie example:
    This is inserted into cout
    请按任意键继续. . .
    同时当前目录下的test.txt输出:
    This is inserted into the file
    

    sync_with_stdio

    这个函数是一个“是否兼容stdio”的开关,C++为了兼容C,保证程序在使用了std::printf和std::cout的时候不发生混乱,将输出流绑到了一起。

    应用

    在ACM里,经常出现数据集超大造成 cin TLE的情况。这时候大部分人(包括原来我也是)认为这是cin的效率不及scanf的错,甚至还上升到C语言和C++语言的执行效率层面的无聊争论。其实像上文所说,这只是C++为了兼容而采取的保守措施。我们可以在IO之前将stdio解除绑定,这样做了之后要注意不要同时混用cout和printf之类。
    在默认的情况下cin绑定的是cout,每次执行 << 操作符的时候都要调用flush,这样会增加IO负担。可以通过tie(0)(0表示NULL)来解除cin与cout的绑定,进一步加快执行效率。
    如下所示:

    #include <iostream>
    int main() 
    {
        std::ios::sync_with_stdio(false);
        std::cin.tie(0);
        // IO
    }
  • 相关阅读:
    POJ2993——Emag eht htiw Em Pleh(字符串处理+排序)
    POJ2109——Power of Cryptography
    POJ2993——Help Me with the Game(字符串处理+排序)
    POJ1573——Robot Motion
    POJ2632——Crashing Robots
    POJ1068——Parencodings
    POJ3295——Tautology
    POJ2506——Tiling
    POJ2524——Ubiquitous Religions
    性能问题“三板斧”
  • 原文地址:https://www.cnblogs.com/zdw20191029/p/14553388.html
Copyright © 2011-2022 走看看