• <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)

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

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

            在vckbase上看到有討論這樣一個(gè)問題:
            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好了,但是該解答畢竟是針對了具體的應(yīng)用,比如定死了hash桶的大小之類的。

            我也湊熱鬧給了一個(gè)實(shí)現(xiàn),只使用標(biāo)準(zhǔn)C++的一些算法解決此問題,性能上還是沒有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)過了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)過了%I64d.%04I64d毫秒\n", (t2-t1)/10000, (t2-t1)%10000 );
            }
            對比的其他兩個(gè)實(shí)現(xiàn):
            1、vector + sort + unique
            2、set
            最后還是我的這個(gè)實(shí)現(xiàn)好一點(diǎn):)
            PS:編譯器用的是VC2005

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

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

            評論

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

            為了方便日后查看(怕那些鏈接無效了),特意把一些其他實(shí)現(xiàn)的代碼摘錄下來:
            ====================================================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ù)  更多評論   

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

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

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

            #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)過了%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ù)  更多評論   

            # 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)過了%I64d.%04I64d毫秒\n", (t2-t1)/10000, (t2-t1)%10000 );
            fclose(fread);
            fclose(fwrite);
            delete [] hashTable;

            std::cin.get();
            }

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

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

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


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


            青青国产成人久久91网| 久久天天躁狠狠躁夜夜躁2O2O| 精品蜜臀久久久久99网站| 99久久精品国内| 久久无码一区二区三区少妇 | 亚洲va久久久久| 久久婷婷国产综合精品| 精品久久久无码中文字幕| 久久婷婷五月综合97色直播| 99999久久久久久亚洲| 久久久久国产日韩精品网站| 久久久久久国产精品美女| 欧美综合天天夜夜久久| 久久久久se色偷偷亚洲精品av| 精品国产热久久久福利| 综合人妻久久一区二区精品| 久久久久久狠狠丁香| 久久人妻少妇嫩草AV无码专区| 四虎国产精品成人免费久久| 国产午夜久久影院| 色欲久久久天天天综合网精品| 2020久久精品亚洲热综合一本| 久久精品一区二区三区中文字幕| 国产精品9999久久久久| 久久精品极品盛宴观看| 91久久精品电影| 岛国搬运www久久| 国产精品成人无码久久久久久 | 看久久久久久a级毛片| 久久国产福利免费| 亚洲国产精品婷婷久久| 久久久久久九九99精品| 欧美丰满熟妇BBB久久久| 综合久久精品色| 午夜精品久久久久| 久久亚洲精品国产亚洲老地址| 久久久黄色大片| 三级三级久久三级久久| 亚洲国产精品无码久久久秋霞2| 久久国产精品77777| 欧洲精品久久久av无码电影|