Simple is beautifull

还需要副标题吗?

导航

<2024年3月>
252627282912
3456789
10111213141516
17181920212223
24252627282930
31123456

统计

常用链接

留言簿(2)

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜

2006年3月31日 #

为什么Python的性能比较好呢?

在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
.^_^。满心欢喜结束这个讨论旅程

posted @ 2006-03-31 15:28 音乐虫子 阅读(2620) | 评论 (4)编辑 收藏

2006年2月12日 #

类型选择

// test7.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

using namespace std;
using namespace boost;

namespace
{
 template<typename T> char checkSize(T*);
 template<typename T> long checkSize(...);
}

template<typename FIRST, typename SECOND>
struct SameType
{
 enum{value = sizeof(char) == sizeof(checkSize<FIRST>((SECOND*)0)) };
};


class IFoo
{
public:
 virtual void bar() = 0;
};

class Foo: public IFoo
{
public:
 virtual void bar(){ cout<<"bar()"<<endl; };
};

template<int>
class TypeSelect
{
public:
 template<typename T>
  TypeSelect(T& r){}
};

template<> class TypeSelect<true>
{
public:
 template<typename T>
  TypeSelect(T& r)
 {
  r.bar();
 } 
};

template<class T> void func( T& t )
{
 (TypeSelect<SameType<IFoo, T>::value>(t));          
}

int main()
{
 Foo x;
 int y;
 func(x);
 func(y);
}

posted @ 2006-02-12 11:27 音乐虫子 阅读(380) | 评论 (0)编辑 收藏

2006年2月9日 #

boost::thread 中使用类成员作为线程入口

just mark it:)

boost::thread trd1(boost::bind(&TheClass::theMember), &obj);

posted @ 2006-02-09 17:08 音乐虫子 阅读(825) | 评论 (5)编辑 收藏

仅列出标题