• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            Simple is beautifull

            還需要副標(biāo)題嗎?

            導(dǎo)航

            <2006年4月>
            2627282930311
            2345678
            9101112131415
            16171819202122
            23242526272829
            30123456

            統(tǒng)計(jì)

            常用鏈接

            留言簿(2)

            隨筆檔案

            搜索

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            為什么Python的性能比較好呢?

            在vckbase上看到有討論這樣一個(gè)問(wèn)題:
            http://blog.vckbase.com/jzhang/archive/2006/03/28/18807.html
            CSDN的朋友參考了Python的實(shí)現(xiàn)源碼給出有如下的解答:
            http://blog.csdn.net/imjj/archive/2006/03/31/645163.aspx?Pending=true
            性能上已經(jīng)比Python好了,但是該解答畢竟是針對(duì)了具體的應(yīng)用,比如定死了hash桶的大小之類的。

            我也湊熱鬧給了一個(gè)實(shí)現(xiàn),只使用標(biāo)準(zhǔn)C++的一些算法解決此問(wèn)題,性能上還是沒(méi)有Python好,但是已經(jīng)非常接近了:
            D:\test\pytest>python test.py
            2006-03-31 14:59:19.348000
            2006-03-31 14:59:22.963000

            D:\test\pytest>cpptest
            經(jīng)過(guò)了4025.7888毫秒

            實(shí)現(xiàn):
            #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( "經(jīng)過(guò)了%I64d.%04I64d毫秒\n", (t2-t1)/10000, (t2-t1)%10000 );
            }
            對(duì)比的其他兩個(gè)實(shí)現(xiàn):
            1、vector + sort + unique
            2、set
            最后還是我的這個(gè)實(shí)現(xiàn)好一點(diǎn):)
            PS:編譯器用的是VC2005

            再PS,寫了上面那個(gè)PS之后突然想看看VC2003怎么樣,于是測(cè)試一下,驚人的發(fā)現(xiàn):
            D:\test\pytest>cpptest2
            經(jīng)過(guò)了3234.6512毫秒
            速度已經(jīng)超越了Python
            .^_^。滿心歡喜結(jié)束這個(gè)討論旅程

            posted on 2006-03-31 15:28 音樂(lè)蟲子 閱讀(2671) 評(píng)論(4)  編輯 收藏 引用

            評(píng)論

            # re: 為什么Python的性能比較好呢? 2006-03-31 18:23 蟲子

            為了方便日后查看(怕那些鏈接無(wú)效了),特意把一些其他實(shí)現(xiàn)的代碼摘錄下來(lái):
            ====================================================1.Python的原始實(shí)現(xiàn):
            #remove duplicated email address from file
            import datetime
            if __name__ == "__main__":
            t = datetime.datetime(2000,1,1)
            print str(t.today())
            hashtable = {}
            f = file("email.txt","r")
            f2 = file("email_new.txt","w")
            line = f.readline();
            while len(line)>0:
            if not hashtable.has_key(line):
            hashtable[line] = 1
            f2.write(line)
            line = f.readline();
            f.close()
            f2.close()
            t2 = datetime.datetime(2000,1,1)
            print str(t2.today())

            from link:
            http://blog.vckbase.com/jzhang/archive/2006/03/28/18807.html
            ====================================================
              回復(fù)  更多評(píng)論   

            # re: 為什么Python的性能比較好呢? 2006-03-31 18:24 铏瓙

            2. 參看Python代碼實(shí)現(xiàn)的實(shí)現(xiàn)
            #include <cstdio>

            // code by 李嘉
            // 禁止任何商業(yè)目的的轉(zhuǎn)載
            // 不對(duì)因使用代碼產(chǎn)生任何后果負(fù)任何責(zé)任
            // 轉(zhuǎn)載請(qǐng)保留所有聲明

            #include <windows.h>
            using namespace std;


            #define c_mul(a, b) (a * b & 0xFFFFFFFF)

            size_t python_hash(const char * str)
            {
            size_t value = str[0] << 7;
            size_t len = 0;
            while(*str != 0)
            {
            value = c_mul(1000003, value) ^ *str++;
            len++;
            }

            value = value ^ len;
            if (value == (size_t)-1)
            value = (size_t)-2;
            return value;
            }

            size_t hash(const char * str, size_t seed = 1)
            {
            size_t h = 0, g;
            size_t len = 0;
            while (*str)
            {
            h = (h << 4) + *str++;
            if ((g = (h & 0xF0000000))) {
            h = h ^ (g >> 24);
            h = h ^ g;
            h = h ^ seed;
            }
            len++;
            }
            return h;
            }


            #define MAX_TABLE_SIZE (780000)
            #define MAX_CONFI 9

            struct hash_item
            {
            size_t items[MAX_CONFI];
            size_t item_count;
            hash_item()
            {
            item_count = 0;
            }
            bool check_has(const char * str)
            {
            size_t key = hash(str);
            for(size_t i = 0; i < item_count; i++)
            {
            if (items[i] == key)
            return true;
            }
            items[item_count++] = key;
            return false;
            }

            };


            int main( void )
            {
            __int64 t1, t2;
            GetSystemTimeAsFileTime( (LPFILETIME)&t1 );
            FILE * fin = fopen("email.txt", "r");
            FILE * fout = fopen("email_new_my.txt", "w+");

            size_t hash_key_a = 0;
            size_t hash_key_b = 0;
            size_t pos_x = 0;
            size_t pos_y = 0;
            const char * buffer = NULL;
            char line[255];
            fgets(line, 255, fin);
            hash_item * table = new hash_item[MAX_TABLE_SIZE];
            while(!feof(fin))
            {
            buffer = line;
            hash_key_a = python_hash(buffer);
            pos_x = hash_key_a % MAX_TABLE_SIZE;
            if (!table[pos_x].check_has(buffer))
            fprintf(fout, "%s", buffer);

            fgets(line, 255, fin);
            }
            GetSystemTimeAsFileTime( (LPFILETIME)&t2 );
            printf( "經(jīng)過(guò)了%I64d.%04I64d毫秒\n", (t2-t1)/10000, (t2-t1)%10000 );
            fclose(fin);
            fclose(fout);
            delete [] table;
            }

            from link:
            http://blog.csdn.net/imjj/archive/2006/03/31/645163.aspx?Pending=true  回復(fù)  更多評(píng)論   

            # re: 為什么Python的性能比較好呢? 2006-04-01 22:10 christanxw

            #include <windows.h>
            #include <cstdio>
            #include <iostream>

            unsigned long cryptTable[0x500];
            const int HASH = 0;
            const int HASH_A = 1;
            const int HASH_B = 2;

            void InitCryptTable()
            {
            unsigned long seed = 0x00100001, index1 = 0, index2 = 0, i;
            for(index1 = 0; index1 < 0x100; index1++)
            {
            for(index2 = index1, i = 0; i < 5; i++, index2 += 0x100)
            {
            unsigned long temp1, temp2;
            seed = (seed * 125 + 3) % 0x2AAAAB;
            temp1 = (seed & 0xFFFF) << 0x10;
            seed = (seed * 125 + 3) % 0x2AAAAB;
            temp2 = (seed & 0xFFFF);
            cryptTable[index2] = (temp1 | temp2);
            }
            }
            }

            unsigned long Hash(char *pStr, unsigned long dwHashType)
            {
            unsigned char *key = (unsigned char *)pStr;
            unsigned long seed1 = 0x7FED7FED, seed2 = 0xEEEEEEEE;
            int ch;

            while(*key != 0)
            {
            ch = toupper(*key++);

            seed1 = cryptTable[(dwHashType << 8) + ch] ^ (seed1 + seed2);
            seed2 = ch + seed1 + seed2 + (seed2 << 5) + 3;
            }
            return seed1;
            }

            struct HashItem
            {
            unsigned long m_nHashKeyA;
            unsigned long m_nHashKeyB;
            bool m_bExist;
            };

            int main()
            {
            __int64 t1, t2;
            GetSystemTimeAsFileTime( (LPFILETIME)&t1 );

            InitCryptTable();
            FILE* fread = fopen("c:\\email.txt","r");
            FILE* fwrite = fopen("c:\\emailnew.txt","w+");

            HashItem *hashTable = new HashItem[780000];

            char line[256] = "";
            fgets(line,255,fread);
            while(!feof(fread))
            {
            int nStart = Hash(line,HASH) % 780000;
            int nPos = nStart;
            if(!(hashTable[nPos].m_bExist
            && hashTable[nPos].m_nHashKeyA ==Hash(line,HASH_A)
            && hashTable[nPos].m_nHashKeyB == Hash(line,HASH_B)))
            {
            hashTable[nPos].m_nHashKeyA = Hash(line,HASH_A);
            hashTable[nPos].m_nHashKeyB = Hash(line,HASH_B);
            hashTable[nPos].m_bExist = true;
            fprintf(fwrite,"%s",line);
            }

            fgets(line,255,fread);
            }

            GetSystemTimeAsFileTime( (LPFILETIME)&t2 );
            printf( "經(jīng)過(guò)了%I64d.%04I64d毫秒\n", (t2-t1)/10000, (t2-t1)%10000 );
            fclose(fread);
            fclose(fwrite);
            delete [] hashTable;

            std::cin.get();
            }

            耗時(shí)343毫秒。很不錯(cuò)了。呵呵。Ptyong也是C寫出來(lái)的,C/C++效率是完全可以比Pyton更快的,就看怎么實(shí)現(xiàn)算法了。在總多的腳本語(yǔ)言中Python是比較慢的一個(gè)了。  回復(fù)  更多評(píng)論   

            # re: 為什么Python的性能比較好呢? 2009-10-16 22:53 MKII

            如果是用了PSYCO,則在我的機(jī)器上為170MS。。。
            PYTHON + PSYCO,怎可能是腳本語(yǔ)言中比較慢的一個(gè)?  回復(fù)  更多評(píng)論   


            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問(wèn)   Chat2DB   管理


            久久天天躁夜夜躁狠狠躁2022 | 日本WV一本一道久久香蕉| 久久这里只有精品久久| 久久精品国产99久久无毒不卡 | 亚洲国产小视频精品久久久三级| www.久久热.com| 九九99精品久久久久久| 99re这里只有精品热久久| 精品免费久久久久久久| 国内精品人妻无码久久久影院 | 久久99精品久久久久子伦| 久久人人爽人人爽人人片AV不| 久久亚洲中文字幕精品一区| 久久人与动人物a级毛片| 亚洲中文字幕久久精品无码喷水 | 久久精品国产亚洲7777| 麻豆国内精品久久久久久| 午夜肉伦伦影院久久精品免费看国产一区二区三区 | 国产91久久综合| 久久久中文字幕日本| 四虎影视久久久免费观看| 性高湖久久久久久久久| 91精品国产高清久久久久久io | 国产精品99久久久久久宅男| 亚洲婷婷国产精品电影人久久| 亚洲欧美成人综合久久久| 精品国产91久久久久久久| 久久天天躁狠狠躁夜夜av浪潮 | 成人精品一区二区久久久| 婷婷久久精品国产| 久久精品无码专区免费青青| 国产精品青草久久久久福利99| 午夜精品久久久久成人| 99久久精品国内| 亚洲精品97久久中文字幕无码| 久久青青草原亚洲av无码app| 精品水蜜桃久久久久久久| 国内精品伊人久久久久777| 亚洲国产精品久久久久| 精品国产99久久久久久麻豆| 大香网伊人久久综合网2020|