pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

混合 cout 和 printf 以獲得更快的輸出

mixing cout and printf for faster output(混合 cout 和 printf 以獲得更快的輸出)
本文介紹了混合 cout 和 printf 以獲得更快的輸出的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

在執行一些測試后,我注意到 printfcout 快得多.我知道它依賴于實現,但在我的 Linux 機器上 printf 快 8 倍.所以我的想法是混合兩種打印方法:我想使用 cout 進行簡單的打印,我計劃使用 printf 來產生巨大的輸出(通常在循環中).我認為只要在切換到其他方法之前不要忘記刷新就可以安全:

After performing some tests I noticed that printf is much faster than cout. I know that it's implementation dependent, but on my Linux box printf is 8x faster. So my idea is to mix the two printing methods: I want to use cout for simple prints, and I plan to use printf for producing huge outputs (typically in a loop). I think it's safe to do as long as I don't forget to flush before switching to the other method:

cout << "Hello" << endl;
cout.flush();

for (int i=0; i<1000000; ++i) {
    printf("World!
");
}
fflush(stdout);

cout << "last line" << endl;
cout << flush;

這樣可以嗎?

更新:感謝所有寶貴的反饋.答案摘要:如果您想避免棘手的解決方案,只需堅持使用 cout 但不要使用 endl 因為它會隱式刷新緩沖區(減慢進程).使用 " " 代替.如果你產生輸出,這會很有趣.

Update: Thanks for all the precious feedbacks. Summary of the answers: if you want to avoid tricky solutions, simply stick with cout but don't use endl since it flushes the buffer implicitly (slowing the process down). Use " " instead. It can be interesting if you produce large outputs.

推薦答案

直接的回答是,是的,沒關系.

The direct answer is that yes, that's okay.

很多人就如何提高速度提出了各種想法,但似乎對哪種方法最有效存在相當多的分歧.我決定編寫一個快速測試程序,以至少了解哪些技術做了什么.

A lot of people have thrown around various ideas of how to improve speed, but there seems to be quite a bit of disagreement over which is most effective. I decided to write a quick test program to get at least some idea of which techniques did what.

#include <iostream>
#include <string>
#include <sstream>
#include <time.h>
#include <iomanip>
#include <algorithm>
#include <iterator>
#include <stdio.h>

char fmt[] = "%s
";
static const int count = 3000000;
static char const *const string = "This is a string.";
static std::string s = std::string(string) + "
";

void show_time(void (*f)(), char const *caption) { 
    clock_t start = clock();
    f();
    clock_t ticks = clock()-start;
    std::cerr << std::setw(30) << caption 
        << ": " 
        << (double)ticks/CLOCKS_PER_SEC << "
";
}

void use_printf() {
    for (int i=0; i<count; i++)
        printf(fmt, string);
}

void use_puts() {
    for (int i=0; i<count; i++) 
        puts(string);        
}

void use_cout() { 
    for (int i=0; i<count; i++)
        std::cout << string << "
";
}

void use_cout_unsync() { 
    std::cout.sync_with_stdio(false);
    for (int i=0; i<count; i++)
        std::cout << string << "
";
    std::cout.sync_with_stdio(true);
}

void use_stringstream() { 
    std::stringstream temp;
    for (int i=0; i<count; i++)
        temp << string << "
";
    std::cout << temp.str();
}

void use_endl() { 
    for (int i=0; i<count; i++)
        std::cout << string << std::endl;
}

void use_fill_n() { 
    std::fill_n(std::ostream_iterator<char const *>(std::cout, "
"), count, string);
}

void use_write() {
    for (int i = 0; i < count; i++)
        std::cout.write(s.data(), s.size());
}

int main() { 
    show_time(use_printf, "Time using printf");
    show_time(use_puts, "Time using puts");
    show_time(use_cout, "Time using cout (synced)");
    show_time(use_cout_unsync, "Time using cout (un-synced)");
    show_time(use_stringstream, "Time using stringstream");
    show_time(use_endl, "Time using endl");
    show_time(use_fill_n, "Time using fill_n");
    show_time(use_write, "Time using write");
    return 0;
}

在使用 VC++ 2013(x86 和 x64 版本)編譯后,我在 Windows 上運行了它.一次運行的輸出(輸出重定向到磁盤文件)如下所示:

I ran this on Windows after compiling with VC++ 2013 (both x86 and x64 versions). Output from one run (with output redirected to a disk file) looked like this:

          Time using printf: 0.953
            Time using puts: 0.567
   Time using cout (synced): 0.736
Time using cout (un-synced): 0.714
    Time using stringstream: 0.725
            Time using endl: 20.097
          Time using fill_n: 0.749
           Time using write: 0.499

正如預期的那樣,結果各不相同,但我覺得有幾點很有趣:

As expected, results vary, but there are a few points I found interesting:

  1. 在寫入 NUL 設備時,printf/puts 比 cout 快得多
    • 但是 cout 在寫入真實文件時保持得很好
  • 在我的測試中,fill_n 與其他任何東西一樣快

我最近編輯了代碼以強制調用printf.Anders Kaseorg 很友好地指出——g++ 識別特定序列 printf("%s ", foo); 等價于 puts(foo);,并相應地生成代碼(即生成代碼來調用 puts 而不是 printf).將格式字符串移動到全局數組,并將其作為格式字符串傳遞會產生相同的輸出,但強制它通過 printf 而不是 puts 產生.當然,他們也有可能在某一天對此進行優化,但至少現在 (g++ 5.1) 使用 g++ -O3 -S 進行的測試確認它實際上正在調用 printf(前面的代碼編譯為對 puts 的調用).

I've recently edited the code to force a call to printf. Anders Kaseorg was kind enough to point out--that g++ recognizes the specific sequence printf("%s ", foo); is equivalent to puts(foo);, and generates code accordingly (i.e., generates code to call puts instead of printf). Moving the format string to a global array, and passing that as the format string produces identical output, but forces it to be produced via printf instead of puts. Of course, it's possible they might optimize around this some day as well, but at least for now (g++ 5.1) a test with g++ -O3 -S confirms that it's actually calling printf (where the previous code compiled to a call to puts).

這篇關于混合 cout 和 printf 以獲得更快的輸出的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數據?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環: for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環?)
Reusing thread in loop c++(在循環 C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環形?)
主站蜘蛛池模板: 杭州成人高考_浙江省成人高考网上报名| 培训中心-海南香蕉蛋糕加盟店技术翰香原中心官网总部 | 电池挤压试验机-自行车喷淋-车辆碾压试验装置-深圳德迈盛测控设备有限公司 | HDPE土工膜,复合土工膜,防渗膜价格,土工膜厂家-山东新路通工程材料有限公司 | 食安观察网| 成都竞价托管_抖音代运营_网站建设_成都SEM外包-成都智网创联网络科技有限公司 | 美国HASKEL增压泵-伊莱科elettrotec流量开关-上海方未机械设备有限公司 | 招商帮-一站式网络营销服务|互联网整合营销|网络推广代运营|信息流推广|招商帮企业招商好帮手|搜索营销推广|短视视频营销推广 | 铁素体测量仪/检测仪/铁素体含量测试仪-苏州圣光仪器有限公司 | 留学生辅导网-在线课程论文辅导-留学生挂科申诉机构 | 沈阳楼承板_彩钢板_压型钢板厂家-辽宁中盛绿建钢品股份有限公司 轴承振动测量仪电箱-轴承测振动仪器-测试仪厂家-杭州居易电气 | 淋巴细胞分离液_口腔医疗器材-精欣华医疗器械(无锡)有限公司 | 步进电机_agv电机_伺服马达-伺服轮毂电机-和利时电机 | 铝箔-铝板-花纹铝板-铝型材-铝棒管-上海百亚金属材料有限公司 | 无锡市珂妮日用化妆品有限公司|珂妮日化官网|洗手液厂家 | 透平油真空滤油机-变压器油板框滤油机-滤油车-华之源过滤设备 | 南京技嘉环保科技有限公司-杀菌除臭剂|污水|垃圾|厕所|橡胶厂|化工厂|铸造厂除臭剂 | 棉柔巾代加工_洗脸巾oem_一次性毛巾_浴巾生产厂家-杭州禾壹卫品科技有限公司 | 涿州网站建设_网站设计_网站制作_做网站_固安良言多米网络公司 | 电动液压篮球架_圆管地埋式篮球架_移动平箱篮球架-强森体育 | 减速机电机一体机_带电机减速器一套_德国BOSERL电动机与减速箱生产厂家 | 【化妆品备案】进口化妆品备案流程-深圳美尚美化妆品有限公司 | 刹车盘机床-刹车盘生产线-龙口亨嘉智能装备 | 商标转让-商标注册-商标查询-软著专利服务平台 - 赣江万网 | 球形钽粉_球形钨粉_纳米粉末_难熔金属粉末-广东银纳官网 | 上海电子秤厂家,电子秤厂家价格,上海吊秤厂家,吊秤供应价格-上海佳宜电子科技有限公司 | 绿叶|绿叶投资|健康产业_绿叶投资集团有限公司 | 【连江县榕彩涂料有限公司】官方网站 | 外贮压-柜式-悬挂式-七氟丙烷-灭火器-灭火系统-药剂-价格-厂家-IG541-混合气体-贮压-非贮压-超细干粉-自动-灭火装置-气体灭火设备-探火管灭火厂家-东莞汇建消防科技有限公司 | 手持式线材张力计-套帽式风量罩-深圳市欧亚精密仪器有限公司 | 混合生育酚_醋酸生育酚粉_琥珀酸生育酚-山东新元素生物科技 | 超声骨密度仪-动脉硬化检测仪器-人体成分分析仪厂家/品牌/价格_南京科力悦 | 电伴热系统施工_仪表电伴热保温箱厂家_沃安电伴热管缆工业技术(济南)有限公司 | 杭州公司变更法人-代理记账收费价格-公司注销代办_杭州福道财务管理咨询有限公司 | 全自动包装秤_全自动上袋机_全自动套袋机_高位码垛机_全自动包装码垛系统生产线-三维汉界机器(山东)股份有限公司 | 网架支座@球铰支座@钢结构支座@成品支座厂家@万向滑动支座_桥兴工程橡胶有限公司 | 衬塑管道_衬四氟管道厂家-淄博恒固化工设备有限公司 | 无负压供水设备,消防稳压供水设备-淄博创辉供水设备有限公司 | 郑州宣传片拍摄-TVC广告片拍摄-微电影短视频制作-河南优柿文化传媒有限公司 | 成都竞价托管_抖音代运营_网站建设_成都SEM外包-成都智网创联网络科技有限公司 | 股票入门基础知识_股票知识_股票投资大师_格雷厄姆网 |