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

            woaidongmao

            文章均收錄自他人博客,但不喜標(biāo)題前加-[轉(zhuǎn)貼],因其丑陋,見(jiàn)諒!~
            隨筆 - 1469, 文章 - 0, 評(píng)論 - 661, 引用 - 0
            數(shù)據(jù)加載中……

            腳本引擎小PK: SpiderMonkey vs V8(一)

            介紹

            SpiderMonkeyFirefox使用的腳本引擎,V8Google Chrome使用的腳本引擎。這篇文章介紹了怎樣在自己的C++程序中嵌入這兩種腳本引擎,以及簡(jiǎn)單做了一些橫向的對(duì)比。

            編譯

            SpiderMonkey

            SpiderMonkey支持1.0~1.8版本的JavaScript語(yǔ)法,包括ECMAScriptECMA 263-3,以及Mozilla擴(kuò)展,可選支持E4X

            由于SpiderMonkeymakefile只支持GNU Make。為了便于編譯,建議使用MSYS,請(qǐng)先準(zhǔn)備好MSYShttp://www.mingw.org

            下載SpiderMonkey源代碼(https://developer.mozilla.org/En/SpiderMonkey/1.8)

            解壓...

            1.                “VC命令提示進(jìn)入控制臺(tái),然后進(jìn)入到MSYS中,這樣做的目的是為了讓MSYS能使用VC的編譯器。

            2.                MSYS里,用cd命令進(jìn)入到SpiderMonkey源代碼目錄中

            3.                輸入make -f makefile.ref BUILD_OPT=1

            這樣就編譯完成了,還算簡(jiǎn)單吧。

            BUILD_OPT=1參數(shù)的作用是把SpiderMonkey編譯成Release版本。

            另外,默認(rèn)是使用VC的運(yùn)行庫(kù)的(即使用-MD編譯參數(shù)),如果不喜歡,可以修改src\confg目錄下的*.mk文件(比如把-MD改成-MT

            對(duì)于MinGW用戶,請(qǐng)參考這里http://jargon.ca/spidermonkey/編譯

            V8

            不知道Google為什么把他們的腳本引擎稱為V8,其宣稱V8的速度遠(yuǎn)遠(yuǎn)超過(guò)SpiderMonkey,同樣支持支持ECMAScriptECMA-262-3

            下載V8源代碼,需要安裝SVN,命令是svn checkout http://v8.googlecode.com/svn/trunk/ v8 或者可以在本站下載(見(jiàn)文章末尾)

            下載后解壓...

            可以在tools\visual_studio里找到工程文件,直接打開(kāi)工程文件就可以編譯了。

            如果編譯成功,可以無(wú)視下面這段。如果沒(méi)搞定,請(qǐng)靜下心來(lái)繼續(xù)...

            命令行編譯方法

            需要Python2.4以上版本(http://www.python.org )

            安裝sconshttp://www.scons.org ),這是一個(gè)python的編譯腳本軟件,可以把它看作是高級(jí)一點(diǎn)的make

            1.                進(jìn)入“VC命令提示

            2.                進(jìn)入V8目錄

            3.                設(shè)置Python路徑 set path=c:\Python25;c:\Python25\Scripts;%path%

            官方文檔說(shuō)到這一步輸入scons就可以開(kāi)始編譯了

            不過(guò)例外總是有的,比如我的VC2005就不行,一會(huì)兒說(shuō)找不到cl命令,一會(huì)兒又找不到頭文件-_-

            我們得告訴它環(huán)境變量的值,這樣寫(xiě)就可以了:

            scons env="PATH:%path%,INCLUDE:%include%,LIB:%lib%"

            默認(rèn)是靜態(tài)鏈接,靜態(tài)VC庫(kù),可以這樣修改編譯參數(shù)

            scons library=shared msvcrt=shared env=...

            輸入scons --help能看到更多編譯參數(shù)

            想在MinGW里編譯,要到這里下個(gè)補(bǔ)丁才行:http://codereview.chromium.org/18309

            注意:如果編譯成動(dòng)態(tài)庫(kù),使用時(shí)包含頭文件v8.h之前應(yīng)該先來(lái)一句 #define USING_V8_SHARED 1

            對(duì)比

            偶不打算把這篇文章寫(xiě)成權(quán)威的橫向評(píng)測(cè)報(bào)告(聽(tīng)見(jiàn)有人說(shuō):切~~沒(méi)這個(gè)能力還嘴硬,-_-!!!)。只是簡(jiǎn)單地比較一下腳本運(yùn)行速度、腳本與宿主程序之間通信速度以及編程的簡(jiǎn)易程度。我想這也是C++編程人員關(guān)心的主要問(wèn)題。

            先放上比較用的腳本:

            腳本運(yùn)行速度測(cè)試腳本

            這是一個(gè)從網(wǎng)上找來(lái)的計(jì)算PiJS腳本

            1.                mess="";

            2.                //10^11 seems to be the maximum

            3.                //too high a figure for the base introduces errors

            4.                Base=Math.pow(10,11);

            5.                //num digits in each array item

            6.                cellSize=Math.floor(Math.log(Base)/Math.LN10);

            7.                //below is not used in this script

            8.                a=Number.MAX_VALUE;

            9.                MaxDiv=Math.floor(Math.sqrt(a));

            10.            function makeArray (n, aX, Integer) {

            11.                var i=0;

            12.                for (i=1; i<n; i++)

            13.                    aX[i] = null;

            14.                aX[0] = Integer;

            15.            }

            16.            function isEmpty (aX) {

            17.                var empty=true

            18.                    for (i=0; i<aX.length; i++)

            19.                        if (aX[i])

            20.                        {

            21.                            empty= false;

            22.                            break;

            23.                        }

            24.                return empty;

            25.            }

            26.            //junior school math

            27.            function Add (n, aX,aY) {

            28.                carry=0

            29.                    for (i=n-1; i>=0; i--) {

            30.                        aX[i] += Number(aY[i])+Number(carry);

            31.                        if (aX[i]<Base)

            32.                            carry = 0;

            33.                        else {

            34.                            carry = 1;

            35.                            aX[i] =Number(aX[i])- Number(Base);

            36.                        }

            37.                    }

            38.            }

            39.            //subtract

            40.            function Sub (n, aX,aY) {

            41.                for (i=n-1; i>=0; i--) {

            42.                    aX[i] -= aY[i];

            43.                    if (aX[i]<0) {

            44.                        if (i>0) {

            45.                            aX[i] += Base;

            46.                            aX[i-1]--;

            47.                        }

            48.                    }

            49.                }

            50.            }

            51.            //multiply big number by "small" number

            52.            function Mul (n, aX, iMult) {

            53.                carry=0;

            54.                for (i=n-1; i>=0; i--) {

            55.                    prod = (aX[i])*iMult;

            56.                    prod += carry;

            57.                    if (prod>=Base) {

            58.                        carry = Math.floor(prod/Base);

            59.                        prod -= (carry*Base);

            60.                    }

            61.                    else

            62.                        carry = 0;

            63.                    aX[i] = prod;

            64.                }

            65.            }

            66.            //divide big number by "small" number

            67.            function Div (n, aX, iDiv,aY) {

            68.                carry=0;

            69.                for (i=0; i<n; i++) {

            70.                    //add any previous carry

            71.                    currVal = Number(aX[i])+Number(carry*Base);

            72.                    //divide

            73.                    theDiv =Math.floor(currVal/iDiv);

            74.                    //find next carry

            75.                    carry = currVal-theDiv*iDiv;

            76.                    //put the result of division in the current slot

            77.                    aY[i] = theDiv;

            78.                }

            79.            }

            80.            //compute arctan

            81.            function arctan (iAng, n, aX) {

            82.                iAng_squared=iAng*iAng;

            83.                k=3; //k is the coefficient in the series 2n-1, 3,5..

            84.                sign=0;

            85.                makeArray (n, aX, 0); //aX is aArctan

            86.                makeArray (n, aAngle, 1);

            87.                Div (n, aAngle, iAng, aAngle); //aAngle = 1/iAng, eg 1/5

            88.                Add (n, aX, aAngle); // aX = aAngle or long angle

            89.                while (!isEmpty(aAngle)) {

            90.                    Div (n, aAngle, iAng_squared, aAngle); //aAngle=aAngle/iAng_squared, iAng_squared is iAng*iAng

            91.                    Div (n, aAngle, k, aDivK); /* aDivK = aAngle/k */

            92.                    if (sign)

            93.                        Add (n, aX, aDivK); /* aX = aX+aDivK */

            94.                    else Sub (n, aX, aDivK); /* aX = aX-aDivK */

            95.                    k+=2;

            96.                    sign = 1-sign;

            97.                }

            98.                mess+="aArctan="+aArctan+"<br>";

            99.            }

            100.        // Calculate pi

            101.        function calcPI (numDec) {

            102.            var ans="";

            103.            t1=new Date();

            104.            numDec=Number(numDec)+5;

            105.            iAng=new Array(10);

            106.            coeff=new Array(10);

            107.            arrayLength=Math.ceil(1+numDec/cellSize);

            108.            aPI = new Array(arrayLength);

            109.            aArctan = new Array(arrayLength);

            110.            aAngle=new Array(arrayLength);

            111.            aDivK=new Array(arrayLength);

            112.            //Pi/4 = 4*arctan(1/5)-arctan(1/239)

            113.            //coeff is an array of the coefficients

            114.            //the last item is 0!

            115.            coeff[0] = 4;

            116.            coeff[1] = -1;

            117.            coeff[2] = 0;

            118.            //iAng holds the angles, 5 for 1/5, etc

            119.            iAng[0] = 5;

            120.            iAng[1] = 239;

            121.            iAng[2] = 0;

            122.            makeArray (arrayLength, aPI, 0);

            123.            //Machin: Pi/4 = 4*arctan(1/5)-arctan(1/239)

            124.            makeArray(arrayLength,aAngle,0);

            125.            makeArray(arrayLength,aDivK,0);

            126.            for (var i=0; coeff[i]!=0; i++) {

            127.                arctan(iAng[i], arrayLength, aArctan);

            128.                //multiply by coefficients of arctan

            129.                Mul (arrayLength, aArctan, Math.abs(coeff[i]));

            130.                if (coeff[i]>0)

            131.                    Add (arrayLength, aPI, aArctan);

            132.                else

            133.                    Sub (arrayLength, aPI, aArctan);

            134.            }

            135.            //we have calculated pi/4, so need to finally multiply

            136.            Mul (arrayLength, aPI, 4);

            137.            //we have now calculated PI, and need to format the answer

            138.            //to print it out

            139.            sPI="";

            140.            tempPI="";

            141.            //put the figures in the array into the string tempPI

            142.            for (i=0;i<aPI.length;i++)

            143.            {

            144.                aPI[i]=String(aPI[i]);

            145.                //ensure there are enough digits in each cell

            146.                //if not, pad with leading zeros

            147.                if (aPI[i].length<cellSize&&i!=0)

            148.                {

            149.                    while (aPI[i].length<cellSize)

            150.                        aPI[i]="0"+aPI[i];

            151.                }

            152.                tempPI+=aPI[i];

            153.            }

            154.            //now put the characters into the string sPI

            155.            for (i=0;i<=numDec;i++)

            156.            {

            157.                //put the 3 on a different line, and add a decimal point

            158.                if (i==0)

            159.                    sPI+=tempPI.charAt(i)+". ";

            160.                else

            161.                {

            162.                    if ((i)%50==0&&i!=0)

            163.                        sPI+=tempPI.charAt(i)+" ";

            164.                    else

            165.                        sPI+=tempPI.charAt(i);

            166.                }//i not zero

            167.            }

            168.            //now put the print-out together

            169.            //print our pi

            170.            ans+=("PI ("+numDec+")="+sPI+" ");

            171.            //Window's calculator Pi (for confirmation);

            172.            ans+=("Win PI= 3.1415926535897932384626433832795 ");

            173.            t2=new Date();

            174.            timeTaken=(t2.getTime()-t1.getTime())/1000;

            175.            ans+="It took: "+timeTaken+" seconds";

            176.            return ans;

            177.         

            178.        }

            179.         

            180.        myprint(calcPI(1000));

            腳本與宿主程序之間通信速度測(cè)試腳本

            1.                // MyClass是宿主提供給JS的類(一個(gè)queue的直接包裝)

            2.                var c = new MyClass();

            3.                t1=new Date();

            4.                 

            5.                //10萬(wàn)次pushpop,測(cè)試JS調(diào)用宿主代碼的速度

            6.                for(i=0; i<100000; i++)

            7.                {

            8.                    c.push(i);

            9.                    c.pop();

            10.            }

            11.            t2=new Date();

            12.            timeTaken=(t2.getTime()-t1.getTime())/1000;

            13.            //myprint也是宿主提供的全局函數(shù),用于顯示結(jié)果

            14.            myprint("It took:",timeTaken,"seconds");

            接下來(lái)我們開(kāi)始編寫(xiě)代碼,利用SpiderMonkeyV8引擎分別實(shí)現(xiàn)出能夠執(zhí)行上述腳本的程序,看看哪個(gè)速度更快吧~~

            下載

            posted on 2010-05-17 12:15 肥仔 閱讀(3354) 評(píng)論(0)  編輯 收藏 引用 所屬分類: 腳本語(yǔ)言

            久久综合视频网站| 国内精品久久久久影院薰衣草| 色悠久久久久久久综合网| 久久久久久亚洲精品无码| 亚洲国产日韩欧美久久| 久久综合亚洲色一区二区三区| 亚洲AV日韩精品久久久久久久| 精品久久人妻av中文字幕| 久久国产一片免费观看| 蜜臀av性久久久久蜜臀aⅴ麻豆| 91久久福利国产成人精品| 亚洲精品国产第一综合99久久| 91精品国产乱码久久久久久| 久久夜色精品国产亚洲av| 韩国无遮挡三级久久| 久久夜色精品国产亚洲| 久久AⅤ人妻少妇嫩草影院| 久久精品国产亚洲av高清漫画| 久久久精品久久久久久 | 国产成人精品久久免费动漫 | 久久久久亚洲?V成人无码| 色综合久久久久无码专区| 欧洲性大片xxxxx久久久| 色综合久久天天综合| 性高湖久久久久久久久| 亚洲日本va午夜中文字幕久久| 国产精品美女久久久免费 | 无码人妻少妇久久中文字幕| 日本久久久久久中文字幕| 国内精品久久人妻互换| 色欲综合久久中文字幕网| 一本色道久久综合亚洲精品| 久久天天躁夜夜躁狠狠| 久久无码AV中文出轨人妻| 色妞色综合久久夜夜| 成人综合久久精品色婷婷 | 日韩亚洲欧美久久久www综合网| 久久这里只有精品18| 欧美午夜精品久久久久免费视| 久久精品国产亚洲av影院| 成人国内精品久久久久影院|