如何改善流性能?

编程入门 行业动态 更新时间:2024-10-26 18:19:36
本文介绍了如何改善流性能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我一直在运行一些基准来比较流和stdio 的性能。我一直怀疑stdio速度更快,但是发现速度要快得多。我将以下时间运行 到/ dev / null,以及使用printf()的相同循环。 int main() { for(int i = 0; i< 1000 * 1000; ++ i){ cout<< i = << i<< ''\\ n''; } } 我做了两个版本的流版本(带'' \ n'和endl), 以及printf的两个变体,不同之处在于每次调用printf()后调用一个调用 到fflush() 。这是在Sun E-250上使用 Sun Workshop编译器的结果: printf.noflush 2.80用户0.01系统(总共2.81) printf.flush 6.02用户3.28系统(共计9.30) stream.newline 33.02用户18.29系统(共51.31) stream.endl 36.33用户19.81系统(总共56.14) Stdio看起来比流快5-20倍。我在运行Linux的800 MHz Pentium上使用gcc获得了类似的 比率。在运行OS-X的 400 MHz Macintosh G4上使用gcc,stdio更接近100倍 更快! Are典型的这些比例?我能做些什么才能让溪流更快??我喜欢类型的安全性和可扩展性,但5-20(或者可能是甚至100)倍的速度是支付应用程序的一个重要价格 任何大量的i / o。

I''ve been running some benchmarks to compare streams and stdio performance. I''ve always suspected stdio was faster, but was astonished to discover how much faster. I timed the following running into /dev/null, as well as the same loop using printf(). int main () { for (int i = 0; i < 1000 * 1000; ++i) { cout << "i = " << i << ''\n''; } } I did two variations of the streams version (with ''\n'' and with endl), and also two variations with printf, the difference being ading a call to fflush() after each call to printf(). Here''s the results using the Sun Workshop compiler on a Sun E-250: printf.noflush 2.80 user 0.01 system (2.81 total) printf.flush 6.02 user 3.28 system (9.30 total) stream.newline 33.02 user 18.29 system (51.31 total) stream.endl 36.33 user 19.81 system (56.14 total) Stdio looks like it''s 5-20 times faster than streams. I got similar ratios using gcc on a 800 MHz Pentium running Linux. Using gcc on a 400 MHz Macintosh G4 running OS-X, stdio was closer to 100 times faster! Are these ratios typical? Is there something I can do to make streams faster? I like the type safety and extensibility, but 5-20 (or maybe even 100) times slower is a big price to pay for applications which do any significant amount of i/o.

推荐答案

" Roy Smith" < ro*@panix>在消息新闻中写道:bf ********** @ panix2.panix ... "Roy Smith" <ro*@panix> wrote in message news:bf**********@panix2.panix... Stdio看起来比它快5到20倍流。我在运行Linux的800 MHz Pentium上使用gcc获得了类似的比率。在运行OS-X的400 MHz Macintosh G4上使用gcc,stdio更接近100倍更快! Stdio looks like it''s 5-20 times faster than streams. I got similar ratios using gcc on a 800 MHz Pentium running Linux. Using gcc on a 400 MHz Macintosh G4 running OS-X, stdio was closer to 100 times faster!

你确定你转过身了吗?在优化器上?在大多数实现中,标准库在内联函数上依赖很多 。我在g ++上只获得3倍的差异 (printf仍然更快)。当然,尝试将一些类型类型输入printf ...

Are you sure you turned on the optimizer? The standard library relies heavily on inline functions in most implementations. I only get a 3x difference on g++ here (printf is still faster). Of course, try feeding some class type to printf...

" Ron Natalie" < ro*@sensor>写道: "Ron Natalie" <ro*@sensor> writes: " Roy Smith" < ro*@panix>在消息中写道新闻:bf ********** @ panix2.panix ... "Roy Smith" <ro*@panix> wrote in message news:bf**********@panix2.panix... Stdio看起来像是5-比溪流快20倍。我在运行Linux的800 MHz Pentium上使用gcc获得了相似的比率。在运行OS-X的400 MHz Macintosh G4上使用 gcc,stdio的速度提高了近100倍! Stdio looks like it''s 5-20 times faster than streams. I got similar ratios using gcc on a 800 MHz Pentium running Linux. Using gcc on a 400 MHz Macintosh G4 running OS-X, stdio was closer to 100 times faster!

您确定打开了优化器吗?在大多数实现中,标准库在很大程度上依赖于内联函数。我只是在g ++上获得3倍的差异(printf仍然更快)。当然,尝试将一些类型输入printf ...

Are you sure you turned on the optimizer? The standard library relies heavily on inline functions in most implementations. I only get a 3x difference on g++ here (printf is still faster). Of course, try feeding some class type to printf...

另一个重点是取消sync_with_stdio。 Dietmar Kuehl 在类似的 主题的clc ++ m线程中指出了一段时间。 ISTR它使iostream的速度是printf的两倍(无论如何,使用STLport 和Windows上的gcc)。理论上,iostream格式化应该更快,因为它不必首先解析格式字符串。 // ... int main(){ std :: sync_with_stdio(false); // ... } - Raoul Gough 让整个王国都有一种葡萄酒衡器,一个 衡量啤酒,玉米衡量一个衡量标准 - Magna Carta

Another important point is to cancel sync_with_stdio. Dietmar Kuehl pointed this out a while back in a clc++m thread on a similar topic. ISTR it making iostreams twice as fast as printf (using STLport with gcc on Windows, anyway). In theory, iostreams formatting should be faster, since it doesn''t have to parse a format string first. // ... int main () { std::sync_with_stdio (false); // ... } -- Raoul Gough "Let there be one measure for wine throughout our kingdom, and one measure for ale, and one measure for corn" - Magna Carta

Raoul Gough< Ra ******** @ yahoo.co.uk>写道: Raoul Gough <Ra********@yahoo.co.uk> writes: // ... int main(){ std :: sync_with_stdio(false); // ... int main () { std::sync_with_stdio (false);

更正 - 应该是: std :: cout.sync_with_stdio(false); 我提到的主题to(printf(...)与cout的表现<< ...) 在 groups.google/groups?th=d75093a01bedc2f0 - Raoul Gough 让整个王国都有一种葡萄酒的衡量标准,一种是啤酒的衡量标准,另一种是衡量玉米的衡量标准。 - Magna Carta

Correction - that should be: std::cout.sync_with_stdio(false); The thread I referred to (performance of printf(...) vs. cout << ...) is at groups.google/groups?th=d75093a01bedc2f0 -- Raoul Gough "Let there be one measure for wine throughout our kingdom, and one measure for ale, and one measure for corn" - Magna Carta

更多推荐

如何改善流性能?

本文发布于:2023-10-26 10:05:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1529822.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:性能

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!