Python和STL效率 [英] Python and STL efficiency
问题描述
我正在学习STL,我写了一些简单的代码来比较python和STL的
效率。
// C ++
#include< iostream>
#include< string>
#include< vector>
# include< set>
#include< algorithm>
using namespace std;
int main(){
vector< stringa;
for(long int i = 0; i< 10000; ++ i){
a.push_back(" What你知道吗?");
a.push_back(" so long ...");
a.push_back(" chicken crossses road&); ;
a.push_back(傻瓜);
}
set< stringb(a.begin(),a.end( ));
unique_copy(b.begin(),b.end(),ostream_iterator< string>(cout," \ n"));
}
#python
def f():
a = []
for i in range (10000):
a.append(''你知道什么'')
a.appe nd(''这么长......'')
a.append(''鸡肉过马路')
a.append(''傻瓜'')
b =设置(a)
for s in b:
print s
我是分别使用VC ++ .net和IDLE。我原本以为C ++要快得多b $ b。然而,虽然python代码立即给出了结果几乎是
,但C ++代码需要几秒钟才能运行!有人可以向我解释这个吗?或者我的代码有问题吗?
Hi, I''m learning STL and I wrote some simple code to compare the
efficiency of python and STL.
//C++
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
int main(){
vector<stringa;
for (long int i=0; i<10000 ; ++i){
a.push_back("What do you know?");
a.push_back("so long...");
a.push_back("chicken crosses road");
a.push_back("fool");
}
set<stringb(a.begin(), a.end());
unique_copy(b.begin(), b.end(), ostream_iterator<string>(cout, "\n"));
}
#python
def f():
a = []
for i in range(10000):
a.append(''What do you know'')
a.append(''so long...'')
a.append(''chicken crosses road'')
a.append(''fool'')
b = set(a)
for s in b:
print s
I was using VC++.net and IDLE, respectively. I had expected C++ to be
way faster. However, while the python code gave the result almost
instantly, the C++ code took several seconds to run! Can somebody
explain this to me? Or is there something wrong with my code?
推荐答案
In< 11 ************ **********@i42g2000cwa.googlegroups .com> ;,李承芳
写道:
In <11**********************@i42g2000cwa.googlegroups .com>, Licheng Fang
wrote:
我我正在学习STL并编写了一些简单的代码来比较python和STL的
效率。
// C ++
#include< iostream>
#include< string>
#include< vector>
#include< set>
#include< algorithm>
使用命名空间std;
int main(){
vector< ; stringa;
for(long int i = 0; i< 10000; ++ i){
a.push_back(你知道什么?) ;
a.push_back(" so long ...");
a.push_back(" chicken crossses road");
a.push_back(傻瓜);
}
set< stringb(a.begin(),a.end());
unique_c opy(b.begin(),b.end(),ostream_iterator< string>(cout," \ n"));
}
Hi, I''m learning STL and I wrote some simple code to compare the
efficiency of python and STL.
//C++
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
int main(){
vector<stringa;
for (long int i=0; i<10000 ; ++i){
a.push_back("What do you know?");
a.push_back("so long...");
a.push_back("chicken crosses road");
a.push_back("fool");
}
set<stringb(a.begin(), a.end());
unique_copy(b.begin(), b.end(), ostream_iterator<string>(cout, "\n"));
}
为什么你在这里使用`unique_copy`?
Why are you using `unique_copy` here?
#python
def f():
a = []
我的范围(10000):
a.append(''你知道什么'')
a.append(''这么长......'')
a.append(''鸡肉过马路')
a.append('''傻瓜'')
b =设定(a)
for s in b:
print s
我分别使用VC ++ .net和IDLE。我原本以为C ++要快得多b $ b。然而,虽然python代码立即给出了结果几乎是
,但C ++代码需要几秒钟才能运行!有人可以向我解释这个吗?或者我的代码有问题吗?
#python
def f():
a = []
for i in range(10000):
a.append(''What do you know'')
a.append(''so long...'')
a.append(''chicken crosses road'')
a.append(''fool'')
b = set(a)
for s in b:
print s
I was using VC++.net and IDLE, respectively. I had expected C++ to be
way faster. However, while the python code gave the result almost
instantly, the C++ code took several seconds to run! Can somebody
explain this to me? Or is there something wrong with my code?
至少数据结构存在差异。 Python`set`类型
是用哈希算法实现的,所以等效的STL类型将是
`hash_set`。 Python中的`set`不会对其内容进行排序。
Ciao,
Marc''BlackJack''Rintsch
There''s a difference in data structures at least. The Python `set` type
is implemented with a hash algorithm, so the equivalent STL type would be
`hash_set`. `set` in Python does not store its contents sorted.
Ciao,
Marc ''BlackJack'' Rintsch
Marc''BlackJack''Rintsch写道:
Marc ''BlackJack'' Rintsch wrote:
In< 11 ******* ***************@i42g2000cwa.googlegroups .com> ;,李承芳
写道:
In <11**********************@i42g2000cwa.googlegroups .com>, Licheng Fang
wrote:
$ b $我正在学习STL,我写了一些简单的代码来比较python和STL的
效率。
// C ++ >
#include< iostream>
#include< string>
#include< vector>
#include< ;设置>
#include< algorithm>
使用命名空间std;
int main(){
vector< stringa;
for(long int i = 0; i< 10000; ++ i){
a.push_back(" What are you知道吗?");
a.push_back(" so long ...");
a。 push_back(鸡穿越道路);
a.push_back(傻瓜);
}
set< stringb(a .begin(),a.end());
unique_copy(b.begin(),b.end(),ostream_iterator< string>(cout," \ n"));
}
Hi, I''m learning STL and I wrote some simple code to compare the
efficiency of python and STL.
//C++
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
int main(){
vector<stringa;
for (long int i=0; i<10000 ; ++i){
a.push_back("What do you know?");
a.push_back("so long...");
a.push_back("chicken crosses road");
a.push_back("fool");
}
set<stringb(a.begin(), a.end());
unique_copy(b.begin(), b.end(), ostream_iterator<string>(cout, "\n"));
}
为什么你在这里使用`unique_copy`?
Why are you using `unique_copy` here?
对不起,这是一个错字。其实我用''复制''。
Sorry, that''s a typo. Actually I used ''copy''.
>
>
#python
def f():
a = []
我在范围内(10000):
a.append(''你知道什么'')
a.append(''这么久......'' )
a.append(''鸡肉过马路')
a.append('''傻瓜'')
b = set( a)
for s in b:
print s
我分别使用VC ++ .net和IDLE。我原本以为C ++要快得多b $ b。然而,虽然python代码立即给出了结果几乎是
,但C ++代码需要几秒钟才能运行!有人可以向我解释这个吗?或者我的代码有问题吗?
#python
def f():
a = []
for i in range(10000):
a.append(''What do you know'')
a.append(''so long...'')
a.append(''chicken crosses road'')
a.append(''fool'')
b = set(a)
for s in b:
print s
I was using VC++.net and IDLE, respectively. I had expected C++ to be
way faster. However, while the python code gave the result almost
instantly, the C++ code took several seconds to run! Can somebody
explain this to me? Or is there something wrong with my code?
至少数据结构存在差异。 Python`set`类型
是用哈希算法实现的,所以等效的STL类型将是
`hash_set`。 Python中的`set`不会对其内容进行排序。
Ciao,
Marc''BlackJack''Rintsch
There''s a difference in data structures at least. The Python `set` type
is implemented with a hash algorithm, so the equivalent STL type would be
`hash_set`. `set` in Python does not store its contents sorted.
Ciao,
Marc ''BlackJack'' Rintsch
感谢您的评论。我测试了hash_set,但我没有看到
性能提升很多。当我将循环增加到100万次b $ b次时,python代码仍然运行得相当快,并且C ++代码在那里停留了
。这让我感到很惊讶,因为根据这个页面
http://norvig.com / python-lisp.html ,python的速度远不及C ++的速度。
Thank you for your comments. I tested with hash_set, but I didn''t see
much performance improvement. When I increased the loop to 1 million
times, the python code still ran reasonably fast and the C++ code got
stuck there. This totally surprised me, because according this page
http://norvig.com/python-lisp.html, the speed of python is nowhere near
that of C++.
Licheng Fang写道:
Licheng Fang wrote:
我正在学习STL,我写了一些简单的代码来比较
$ python和STL的效率。
我分别使用VC ++ .net和IDLE。我原本以为C ++要快得多b $ b。然而,虽然python代码立即给出了结果几乎是
,但C ++代码需要几秒钟才能运行!有人可以向我解释这个吗?或者我的代码有问题吗?
Hi, I''m learning STL and I wrote some simple code to compare the
efficiency of python and STL.
I was using VC++.net and IDLE, respectively. I had expected C++ to be
way faster. However, while the python code gave the result almost
instantly, the C++ code took several seconds to run! Can somebody
explain this to me? Or is there something wrong with my code?
我不是C ++大师所以不能对C ++代码本身发表评论,但是我做了
想知道你是否用其他STL实现测试了你的C ++代码,例如gcc(gcc在windows上也可用,各种版本)。
>
可能是在C ++中扩展列表是以非常小的增量完成的,这导致了许多重新分配。是否有可能
预分配向量<有足够的条目?
另外,你引用的Python代码,实际上并没有调用你的函数
F()。如果你说你立刻得到了结果,我认为你的意思是
所有4个字符串实际上都印在了控制台上?
(我很惊讶控制台快速打印东西。
顺便说一下,在Python中使用range()并不是很有效,我认为......最好是
使用xrange()。
要求我的C ++同事发表评论,他强烈怀疑
你实际上是在.Net运行时运行它(你的C ++)代码包含
一些C#主题,例如在include<语句中省略''。h''。
运气,
--Tim
Hi,
I''m no C++ guru so cannot comment on the C++ code itself, however I do
wonder if you tested your C++ code with other STL implementation such
as gcc (gcc is available on windows as well, in various versions).
What could be is that expanding the list in C++ is done in very small
increments, leading to many re-allocations. Is it possible to
pre-allocate the vector<with sufficient entries?
Also, your Python code as quoted, doesn''t actually call your function
f(). If you say that you get results instantly, I assume that you mean
all 4 strings are actually printed to console?
(I''m surprised that the console prints things that fast).
btw, using range() in Python isn''t very efficient, I think... Better to
use xrange().
Asked a C++ collegue of mine to comment, and he strongly suspects that
you''re actually running it in the .Net runtime (your C++ code contains
some C#-isms, such as omitting the ''.h'' in the include <statements).
Luck,
--Tim
这篇关于Python和STL效率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!