維's profileIT : 是工作還是嗜好?PhotosBlogListsMore Tools Help

Blog


    November 28

    平行處理機制,平行Linq和Delphi Prism

    在上篇我從一些蛛絲馬跡來猜測Delphi除了朝原生64位元發展之外,也可能會在非同步,平行執行方向進行突破的發展。當這篇文章貼出之後,很快的我的朋友就問我『這樣的』Delphi會在什麼時候出現,說實話我不知道,因為這也是我從一些蛛絲馬跡『猜測』的,所以我當然不知道,只能等時間來告訴我們了,呵呵。
    Delphi Prism程式語言中提供了許多非同步,平行執行的機制,除了上次我們看到的async和parallel之外,當我們使用這些非同步,平行執行的機制結合PFX 框架時,也能夠很方便的在使用Linq時也享受平行執行的好處,例如下面是傳統使用Linq的一個典型,簡單的範例:

    class method ConsoleApp.Linq;
    var
      words : sequence of string;
    begin
      words := ['嗨', '世界' , 'Linq', 'Parallel Linq' , 'Delphi Prism', 'Delphi 2009', 'BCB 2009', '3rdRail'];
      var rQuery := from word in words select word;

      for each s in rQuery do
        Console.WriteLine(s);

      Console.ReadLine;
    end;

    當我們使用Delphi Prism編譯並且執行之後,會看到如下的結果:
    http://blufiles.storage.live.com/y1pnfguIQKXzsE1F49XruVvqOLio8MYDAdvlGGt7aDrP2UdtPAi5PKF_AcX1PVgZisw

    我們從上圖的執行結果可以看到,它輸出的結果次序和我們在程式碼中定義sequence的次序是一樣。
    但是如果我們稍微修改一點程式碼,在words之後呼叫擴展方法AsParallel如下所示:

    class method ConsoleApp.PLinq;
    var
      words : sequence of string;
    begin
      words := ['嗨', '世界' , 'Linq', 'Parallel Linq' , 'Delphi Prism', 'Delphi 2009', 'BCB 2009', '3rdRail'];
      var rQuery := from word in words.AsParallel select word;

      rQuery.ForAll<string>(word -> Console.WriteLine(word));
      Console.ReadLine;
    end;

    那麼我們會看到如下的執行結果:
    http://blufiles.storage.live.com/y1piJJHrhRgrHyDrKVst5KDd5kJbI-DL-bzA5_85VIIIdXjmXkUNeNYbLbKPucIyLJahttp://blufiles.storage.live.com/y1pFfkk9_1ena1VTyZedMmv5gXjo4_miYp14SPNbsdlE7H2ZAHcplG_1DSSMfteZgcK
     
    從上圖中可以發現每次執行平行查詢時執行的結果次序都不同,而且輸出的次序也不是程式碼中定義的次序,這個證明了當使用Delphi Prism的平行機制結合PFX框架時的確是以多個平行執行的核心來處理的。
    使用.NET Reflector反組編PLinq方法也可以看到它的確是呼叫PFX框架中提供的平行處理機制:

    class method ConsoleApp.PLinq;
    begin
    ParallelEnumerable.ForAll<String>(ParallelQuery.AsParallel<String>(array of(['嗨', '世界', 'Linq', 'Parallel Linq', 'Delphi Prism', 'Delphi 2009', 'BCB 2009', '3rdRail'])), delegate (word: String)
        begin
            Console.WriteLine(word)
        end;
        Console.ReadLine
    end;

    和呼叫Linq方法是截然不同的。

    class method ConsoleApp.Linq;
    begin
        var rQuery: IEnumerable<String> := array of(['嗨', '世界', 'Linq', 'Parallel Linq', 'Delphi Prism', 'Delphi 2009', 'BCB 2009', '3rdRail']);
        if (rQuery <> nil) then
            {pseudo} goto Label_0058;
        rQuery.GetEnumerator;
        var enumerator: IEnumerator<String> := nil;
        if (enumerator <> nil) then
            while enumerator.MoveNext do begin
                var s: String := enumerator.Current;
                Console.WriteLine(s)
            end;
        Console.ReadLine
    end;

    Delphi Prism提供的先進非同步,平行執行機制突然讓原生Delphi 32/64有了新的發展方向。



    Comments (4)

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    No namewrote:
    http://you-tubehk.blogspot.com/
    http://skypetw.blogspot.com/
    http://tw-kmplayer.blogspot.com/
    http://xunlei-tw.blogspot.com/
    http://bt-tw.blogspot.com/
    http://cs-cs16.blogspot.com/
    http://winrar3-tw.blogspot.com/
    http://nds-tw.blogspot.com/
    http://twkmplayer.blogspot.com/
    http://vista-tw.blogspot.com/
    http://jojo-tw.blogspot.com/
    http://emule047-tw.blogspot.com/
    http://directx-90.blogspot.com/
    http://office2007-tw.blogspot.com/
    http://tw-vista.blogspot.com/
    http://bosexe.blogspot.com/
    http://rmvb-tw.blogspot.com/
    http://bosstw.blogspot.com/
    http://java-tw.blogspot.com/
    http://tw-games.blogspot.com/
    http://photoimpat.blogspot.com/
    http://kmplayer2009.blogspot.com/
    http://online-tw.blogspot.com/
    http://sao-omline.blogspot.com/
    <a href="http://www.96177.com/forumdisplay.php?fid=396"> 金銘長大</a>|
    <a href="http://www.96177.com/forumdisplay.php?fid=391">娘家王燦</a>|
    <a href="http://www.96177.com/forumdisplay.php?fid=392">舍利花舍利子</a>|
    <a href="http://www.96177.com/forumdisplay.php?fid=393">心型愛人島</a>|
    <a href="http://www.96177.com/forumdisplay.php?fid=394">彩色珍珠奶茶</a>|
    <a href="http://www.96177.com/forumdisplay.php?fid=395">秒殺麵包</a>|
    <a href="http://www.96177.com/forumdisplay.php?fid=397">錢韋杉婚紗照</a>|
    <a href="http://www.96177.com/forumdisplay.php?fid=398">錢韋杉懷孕照</a>|
    <a href="http://www.96177.com/forumdisplay.php?fid=399">江語晨部落格</a>|
    <a href="http://www.96177.com/forumdisplay.php?fid=400">宮澤理惠</a>|
    <a href="http://www.96177.com/forumdisplay.php?fid=401">余康蘶blog</a>|
    <a href="http://www.96177.com/forumdisplay.php?fid=402">獨角鯨</a>|
    <a href="http://www.96177.com/forumdisplay.php?fid=403">沈玉萍照片</a>|
    <a href="http://www.96177.com/forumdisplay.php?fid=404">金銘旅行社</a>|
    <a href="http://www.96177.com/forumdisplay.php?fid=390">田中聖最新shop照</a>|
    <a href="http://www.96177.com/forumdisplay.php?fid=389">網路試衣間</a>|
    <a href="http://www.96177.com/forumdisplay.php?fid=381">桃花牆</a>|
    <a href="http://www.96177.com/forumdisplay.php?fid=382">侯炳瑩部落格</a>|
    <a href="http://www.96177.com/forumdisplay.php?fid=383">侯炳瑩部落格</a>|
    <a href="http://www.96177.com/forumdisplay.php?fid=384"> 侯炳瑩的男友</a>|
    <a href="http://www.96177.com/forumdisplay.php?fid=385"> 黃騰浩部落格</a>|
    <a href="http://www.96177.com/forumdisplay.php?fid=386">黃騰浩女友/a>|
    <a href="http://www.96177.com/forumdisplay.php?fid=387">北川景子桌布</a>|
    <a href="http://www.96177.com/forumdisplay.php?fid=388">北川景子</a>|
    Feb. 26
    宇 杨wrote:
    namespace DelphiPrism4;
    interface
    uses
    System.Collections.Generic,
    System.Linq;
    type
    ConsoleApp = class
    public
    class method Main;
    end;
    implementation
    class method ConsoleApp.Main;
    var
    words : sequence of string;
    begin
    words := ['嗨', '世界' , 'Linq', 'Parallel Linq' , 'Delphi Prism', 'Delphi 2009', 'BCB 2009', '3rdRail'];
    var rQuery := from word in words.AsParallel select word;

    rQuery.ForAll<string>(word -> Console.WriteLine(word));
    Console.ReadLine;
    end;
    end.

    错误 1 (PE223) "sequence of System.String" does not contain a definition for "AsParallel" in expression "words.AsParallel" DelphiPrism4\Program.pas 24 36 DelphiPrism4
    错误 2 (PE223) "System.Object" does not contain a definition for "ForAll" in expression "rQuery.ForAll" DelphiPrism4\Program.pas 25 10 DelphiPrism4

    无法build,怎么会事?
    Feb. 22
    卢严坚wrote:
    夸平台是最好的,这是vs没有做的,
    然后又在winform方面比java好,
    但是稳定性及性能是我最想了解的.
    Dec. 20
    Picture of Anonymous
    Lex Y. Li wrote:
    Delphi Prism应该已经正式出货了。
    Nov. 29

    Trackbacks

    The trackback URL for this entry is:
    http://gordonliwei.spaces.live.com/blog/cns!CCE1F10BD8108687!2930.trak
    Weblogs that reference this entry
    • None