在vckbase上看到有討論這樣一個問題:
http://blog.vckbase.com/jzhang/archive/2006/03/28/18807.html
CSDN的朋友參考了Python的實現源碼給出有如下的解答:
http://blog.csdn.net/imjj/archive/2006/03/31/645163.aspx?Pending=true
性能上已經比Python好了,但是該解答畢竟是針對了具體的應用,比如定死了hash桶的大小之類的。
我也湊熱鬧給了一個實現,只使用標準C++的一些算法解決此問題,性能上還是沒有Python好,但是已經非常接近了:
D:\test\pytest>python test.py
2006-03-31 14:59:19.348000
2006-03-31 14:59:22.963000
D:\test\pytest>cpptest
經過了4025.7888毫秒
實現:
#include <windows.h>??????//? just for time counting
#include <list>
#include <string>
#include <fstream>
#include <algorithm>
using namespace std;
int main( void )
{
?__int64 t1, t2;
?GetSystemTimeAsFileTime( (LPFILETIME)&t1 );
?list<string> emails;
?ifstream infile("email2.txt");
?ofstream oufile("email_cpp.txt");
?copy( istream_iterator<string>(infile), istream_iterator<string>(), back_inserter(emails) );
?emails.unique();
?ofstream outfile( "email_cpp.txt" );
?copy( emails.begin(), emails.end(), ostream_iterator<string>(outfile,"\n") );
?GetSystemTimeAsFileTime( (LPFILETIME)&t2 );
?printf( "經過了%I64d.%04I64d毫秒\n", (t2-t1)/10000, (t2-t1)%10000 );
}
對比的其他兩個實現:
1、vector + sort + unique
2、set
最后還是我的這個實現好一點:)
PS:編譯器用的是VC2005
再PS,寫了上面那個PS之后突然想看看VC2003怎么樣,于是測試一下,驚人的發現:
D:\test\pytest>cpptest2
經過了3234.6512毫秒
速度已經超越了Python
.^_^。滿心歡喜結束這個討論旅程