自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

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

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

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

#T#

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

代碼如下:

  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使用周期的消耗——不是我不想,而是遇到了問題。我當(dāng)時這樣引入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簽名中使用*來標(biāo)記out參數(shù),但是在自定義方法時使用的是byref,這點與C#不同,后者都是使用ref。這個引入看似沒有問題,而且普通調(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的一個方法,如getCycleCount:

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

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

拋出異常

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

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

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

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

2010-01-04 09:40:46

F#對象

2010-04-07 16:51:59

F#

2010-01-26 08:25:06

F#語法F#教程

2009-08-19 09:42:34

F#并行排序算法

2010-01-07 10:04:18

F#函數(shù)式編程

2010-01-15 08:33:13

F#F#類型推斷F#教程

2010-03-16 09:09:04

F#

2011-08-01 16:24:04

XCode CodeTimer 測試

2010-04-07 09:46:05

2010-03-26 19:22:08

F#代理

2010-08-16 16:12:58

F#

2009-08-13 17:39:48

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

2011-06-09 09:52:41

F#

2010-03-08 09:17:13

F#異步

2009-09-10 14:18:59

Functional F#

2012-03-12 12:34:02

JavaF#

2019-07-11 08:00:00

JavaScriptJulia編程語言

2009-12-04 09:16:44

Visual Stud

2012-11-06 10:01:35

ContinuatioF#

2009-12-14 09:04:10

F#運算符
點贊
收藏

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