成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

詳解F#版本的CodeTimer方法實(shí)現(xiàn)

開(kāi)發(fā) 后端
在這里我們將介紹F#版本的CodeTimer方法實(shí)現(xiàn),這一方法在C#中已經(jīng)廣泛使用了,希望本文對(duì)大家有所幫助。

對(duì)于F#這個(gè)微軟的新丁,很多人并不熟悉。很多開(kāi)發(fā)人員知道函數(shù)式編程方面Scala可以算一個(gè),但是不知道F#中CodeTimer妙用。本文借用作者的文章,希望能讓大家對(duì)F#有更深刻的了解。

#T#

CodeTimer很好用,自從在今年三月在.NET技術(shù)大會(huì)上看到Jeffrey Richter用類(lèi)似的東西之后,我就自己寫(xiě)了一個(gè)。不過(guò),當(dāng)時(shí)是用C#寫(xiě)的,現(xiàn)在我需要在F#里做相同的事情就不那么方便了。當(dāng)然,F(xiàn)#與.NET本是無(wú)縫集成,因此C#寫(xiě)的CodeTimer也應(yīng)該可以被F#使用。不過(guò),我平時(shí)在使用CodeTimer時(shí)并不是通過(guò)程序集引用,而是使用代碼復(fù)制的方式,因此如果有個(gè)F#版本那么應(yīng)該使用起來(lái)更加方便。

代碼如下:

  1. #light  
  2. module CodeTimer  
  3. open System  
  4. open System.Diagnostics  
  5. open System.Threading  
  6. open System.Runtime.InteropServices  
  7.  
  8. [<DllImport("kernel32.dll")>]  
  9. extern int QueryThreadCycleTime(IntPtr threadHandle, uint64* cycleTime)  
  10.  
  11. [<DllImport("kernel32.dll")>]  
  12. extern IntPtr GetCurrentThread();  
  13.  
  14. let private getCycleCount() =   
  15.     let mutable cycle = 0UL  
  16.     let threadHandle = GetCurrentThread()  
  17.     QueryThreadCycleTime(threadHandle, &&cycle) |> ignore  
  18.     cycle  
  19.  
  20. let time name iteration action =  
  21.  
  22.     if (String.IsNullOrEmpty(name)) then ignore 0 else 
  23.  
  24.     // keep current color  
  25.     let currentForeColor = Console.ForegroundColor  
  26.     Console.ForegroundColor <- ConsoleColor.Yellow  
  27.     printfn "%s" name  
  28.  
  29.     // keep current gc count  
  30.     GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);  
  31.     let gcCounts =  
  32.         [0 .. GC.MaxGeneration]  
  33.         |> List.map (fun i -> (i, GC.CollectionCount(i)))  
  34.         |> List.fold (fun acc i -> i :: acc) []  
  35.         |> List.rev  
  36.  
  37.     // keep cycle count and start watch  
  38.     let threadPtr = GetCurrentThread()  
  39.     let cycle = getCycleCount()  
  40.     let watch = Stopwatch.StartNew()  
  41.       
  42.     // run  
  43.     for i = 1 to iteration do action();  
  44.       
  45.     let cycleUsed = getCycleCount() - cycle  
  46.     watch.Stop()  
  47.  
  48.     // restore the color  
  49.     Console.ForegroundColor <- currentForeColor;  
  50.  
  51.     // print  
  52.     watch.ElapsedMilliseconds.ToString("N0") |> printfn "\tTime Elapsed:\t%sms" 
  53.     cycle.ToString("N0") |> printfn "\tCPU Cycles:\t%s" 
  54.     gcCounts |> List.iter (fun (i, c) ->   
  55.         printfn "\tGen%i:\t\t%i" i (GC.CollectionCount(i) - c))  
  56.  
  57.     printfn "" 
  58.  
  59. let initialize() =  
  60.     Process.GetCurrentProcess().PriorityClass <- ProcessPriorityClass.High  
  61.     Thread.CurrentThread.Priority <- ThreadPriority.Highest  
  62.     time "" 0 (fun() -> ignore 0) 

結(jié)果是:

  1. Wait  
  2.         Time Elapsed:   684ms  
  3.         CPU Cycles:     372,709,908  
  4.         Gen0:           0  
  5.         Gen1:           0  
  6.         Gen2:           0 

與C#版本的CodeTimer相比,第一版的F# CodeTimer少算了CPU使用周期的消耗——不是我不想,而是遇到了問(wèn)題。我當(dāng)時(shí)這樣引入P/Invoke的簽名:

  1. open System.Runtime.InteropServices  
  2. [<DllImport("kernel32.dll")>]  
  3. extern int QueryThreadCycleTime(IntPtr threadHandle, uint32* cycleTime)  
  4. [<DllImport("kernel32.dll")>]  
  5. extern IntPtr GetCurrentThread(); 

F#在P/Invoke簽名中使用*來(lái)標(biāo)記out參數(shù),但是在自定義方法時(shí)使用的是byref,這點(diǎn)與C#不同,后者都是使用ref。這個(gè)引入看似沒(méi)有問(wèn)題,而且普通調(diào)用也能得到正常結(jié)果:

  1. [<EntryPoint>]  
  2. let main args =  
  3.  
  4.     let mutable cycle = 0u 
  5.     let threadHandle = CodeTimer.GetCurrentThread()  
  6.     CodeTimer.QueryThreadCycleTime(threadHandle, &&cycle) |> ignore  
  7.  
  8.     Console.ReadLine() |> ignore  
  9.     0 

但是,一旦我把它加為CodeTimer的一個(gè)方法,如getCycleCount:

  1. let getCycleCount() =   
  2.     let mutable cycle = 0u  
  3.     let threadHandle = GetCurrentThread()  
  4.     QueryThreadCycleTime(threadHandle, &&cycle) |> ignore  
  5.     cycle 

這樣調(diào)用的時(shí)候就會(huì)拋出異常:

拋出異常

后經(jīng)alonesail同學(xué)指出,引入QueryThreadCycleTime的時(shí)候,第二個(gè)參數(shù)應(yīng)該是64位而不是32位無(wú)符號(hào)整數(shù)——我將PULONG64看作PULONG了。改成uint64便沒(méi)有問(wèn)題了。

原文標(biāo)題:F#版本的CodeTimer(已支持CPU時(shí)鐘周期統(tǒng)計(jì))

鏈接:http://www.cnblogs.com/JeffreyZhao/archive/2009/11/13/fsharp-codetimer.html

責(zé)任編輯:彭凡 來(lái)源: 博客園
相關(guān)推薦

2010-01-04 09:40:46

F#對(duì)象

2010-04-07 16:51:59

F#

2010-01-26 08:25:06

F#語(yǔ)法F#教程

2009-08-19 09:42:34

F#并行排序算法

2010-01-07 10:04:18

F#函數(shù)式編程

2010-01-15 08:33:13

F#F#類(lèi)型推斷F#教程

2011-08-01 16:24:04

XCode CodeTimer 測(cè)試

2010-04-07 09:46:05

2010-03-16 09:09:04

F#

2010-03-26 19:22:08

F#代理

2009-08-13 17:39:48

F#數(shù)據(jù)類(lèi)型Discriminat

2011-06-09 09:52:41

F#

2010-08-16 16:12:58

F#

2009-09-10 14:18:59

Functional F#

2012-03-12 12:34:02

JavaF#

2010-03-08 09:17:13

F#異步

2019-07-11 08:00:00

JavaScriptJulia編程語(yǔ)言

2012-11-06 10:01:35

ContinuatioF#

2009-12-04 09:16:44

Visual Stud

2009-12-14 09:04:10

F#運(yùn)算符
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)

主站蜘蛛池模板: 精品精品视频 | 欧美黑人一级爽快片淫片高清 | 日韩日b视频 | 久久国产精品免费 | 日韩成人av在线 | 亚洲一区二区精品视频 | 怡红院怡春院一级毛片 | 国产h在线 | 中文字幕在线网 | 蜜臀网 | 国产成人精品一区二区 | 极品国产视频 | 日韩成人在线观看 | 天天天天操 | 日韩精品视频一区二区三区 | 澳门永久av免费网站 | 久久久久久久91 | 亚洲精品一区二区三区蜜桃久 | 91成人免费观看 | 草久久| 国产精品久久国产精品久久 | 免费看91 | 欧美激情在线精品一区二区三区 | 久久高清 | 国产精品91视频 | 超碰在线播 | 亚洲视频在线免费观看 | 91色视频在线观看 | 日韩中文字幕在线 | 欧美日韩在线播放 | 亚洲精品久久久一区二区三区 | 国产区免费视频 | 午夜视频在线视频 | 国产成人影院 | 亚洲九九色 | 国产美女在线观看 | 国产精品免费一区二区三区 | 成人在线视频一区 | 天天操天天摸天天爽 | 91九色porny首页最多播放 | 亚洲国产精品久久 |