如何改善流性能? [英] How can I improve streams performance?

查看:80
本文介绍了如何改善流性能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在运行一些基准来比较流和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.com>在消息新闻中写道:bf ********** @ panix2.panix.com ...

"Roy Smith" <ro*@panix.com> wrote in message news:bf**********@panix2.panix.com...
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.com>写道:
"Ron Natalie" <ro*@sensor.com> writes:
" Roy Smith" < ro*@panix.com>在消息中写道
新闻:bf ********** @ panix2.panix.com ...
"Roy Smith" <ro*@panix.com> wrote in message
news:bf**********@panix2.panix.com...
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的表现<< ...)

http://groups.google.com/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 http://groups.google.com/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


这篇关于如何改善流性能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆