STL速度 [英] STL speed

查看:154
本文介绍了STL速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



任务确实很简单。阅读2.000.000字(平均长度= 9),排序

将它们写入新文件。

我已经用STL制作了它,它差不多了比我之前的版本慢了17倍

" clear-C-code"。

我使用了< vector>,< algorithm>,< iostream>和<算法>。 STL真的如此

慢吗?


thx in adv。

Przemo


ps我知道STL使用的是IntrospectiveSort,这似乎是不错的选择,我认为INPUT(cin)非常慢,

和< vector>作为一个动态结构也不是快速...任何想法?

Hi,
The task was indeed simple. Read 2.000.000 words (average length = 9), sort
them and write it to new file.
I''ve made this in STL, and it was almost 17 times slower than my previous
"clear-C-code".
I used <vector>, <algorithm>, <iostream> and <algorithm>. Is STL really so
slow?

Thx in adv.
Przemo

p.s. i know that STL uses IntrospectiveSort which seems to be good choice, i
suppose that INPUT (cin) is extremaly slow,
and <vector> as a dynamic structure also isn''t to fast... any ideas?

推荐答案

" Przemo Drochomirecki" < PE ****** @ gazeta.pl>在留言中写道

news:bt ********** @ nemesis.news.tpi.pl ...
"Przemo Drochomirecki" <pe******@gazeta.pl> wrote in message
news:bt**********@nemesis.news.tpi.pl...

任务确实很简单。读取2.000.000个单词(平均长度= 9),
对它们进行排序并将其写入新文件。
我已经在STL中创建了它,它比我之前的那个慢了近17倍/>clear-C-code。
我使用< vector>,< algorithm>,< iostream>和<算法>。 STL真的如此缓慢吗?

请问赞助。
Przemo

p.s.我知道STL使用IntrospectiveSort似乎是一个不错的选择,
我认为INPUT(cin)非常慢,
和< vector>作为一个动态的结构也不是要快......任何想法?
Hi,
The task was indeed simple. Read 2.000.000 words (average length = 9), sort them and write it to new file.
I''ve made this in STL, and it was almost 17 times slower than my previous
"clear-C-code".
I used <vector>, <algorithm>, <iostream> and <algorithm>. Is STL really so
slow?

Thx in adv.
Przemo

p.s. i know that STL uses IntrospectiveSort which seems to be good choice, i suppose that INPUT (cin) is extremaly slow,
and <vector> as a dynamic structure also isn''t to fast... any ideas?




我怀疑它本身就很慢。这取决于实施。我记得跟踪MS编译器上的流输出并发现

它编写了一个格式字符串并称为sprintf!银行里有五十亿美元,但是他们选择了最便宜,最糟糕的实施方案。


DW



I doubt that it''s inherently slow. It depends on the implementation. I
remember tracing through the stream output on an MS compiler and discovered
that it fabricated a format string and called sprintf! Fifty billion dollars
in the bank, but they chose the cheapest, nastiest implementation possible.

DW


" Przemo Drochomirecki" < PE ****** @ gazeta.pl>写在

新闻:bt ********** @ nemesis.news.tpi.pl:
"Przemo Drochomirecki" <pe******@gazeta.pl> wrote in
news:bt**********@nemesis.news.tpi.pl:

任务确实很简单。读取2.000.000个单词(平均长度= 9),对它们进行排序并将其写入新文件。
我在STL中做了这个,它比我的
previousclear-C-code。
我使用了< vector>,< algorithm>,< iostream>和<算法>。 STL
真的这么慢吗?

请问赞助。
Przemo

p.s.我知道STL使用IntrospectiveSort似乎是很好的选择,我认为INPUT(cin)非常慢,
和< vector>作为一个动态结构也不是禁止......任何想法?
Hi,
The task was indeed simple. Read 2.000.000 words (average length = 9),
sort them and write it to new file.
I''ve made this in STL, and it was almost 17 times slower than my
previous "clear-C-code".
I used <vector>, <algorithm>, <iostream> and <algorithm>. Is STL
really so slow?

Thx in adv.
Przemo

p.s. i know that STL uses IntrospectiveSort which seems to be good
choice, i suppose that INPUT (cin) is extremaly slow,
and <vector> as a dynamic structure also isn''t to fast... any ideas?




你没有发布你的clear-C-code,也没有你的标准C ++代码,因此

我们甚至无法猜测你做得不够好。


凝视我的水晶球说你没有用C和C ++表达相同的

程序。



You didn''t post your "clear-C-code", nor your Standard C++ code, thus there
is no way for us to even guess at what you''ve done inefficiently.

Gazing into my crystal ball says that you haven''t expressed the same
program in C and C++.


> STL真的这么慢吗?


没有源代码就很难说,这取决于很多其他的东西,比如STL实现编译器。

我会编写如下程序:


#include< algorithm>

#include < iterator>

#include< iostream>

#include< fstream>

#include< string>

#include< vector>


使用命名空间std;


int main()

{

ifstream file(" input.txt");

vector< string>单词(istream_iterator< string>(file),

(istream_iterator< string>()));

sort(words.begin(),words.end()) ;

ofstream outfile(" output.txt");

copy(words.begin(),words.end(),ostream_iterator< string>(outfile," ; \ n"));

}


问候

Ignaz
> Is STL really so slow?

That''s hard to say without the source and it depends on quite a few other
things, like the STL implementation and the Compiler.
I''d write the programm as following:

#include <algorithm>
#include <iterator>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

int main()
{
ifstream file("input.txt");
vector<string> words(istream_iterator<string>(file),
(istream_iterator<string>()));
sort(words.begin(), words.end());
ofstream outfile("output.txt");
copy(words.begin(), words.end(), ostream_iterator<string>(outfile, "\n"));
}

Regards
Ignaz

这篇关于STL速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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