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

詳解F#版本的CodeTimer方法實現

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

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

#T#

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

代碼如下:

  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) 

結果是:

  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使用周期的消耗——不是我不想,而是遇到了問題。我當時這樣引入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簽名中使用*來標記out參數,但是在自定義方法時使用的是byref,這點與C#不同,后者都是使用ref。這個引入看似沒有問題,而且普通調用也能得到正常結果:

  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 

這樣調用的時候就會拋出異常:

拋出異常

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

原文標題:F#版本的CodeTimer(已支持CPU時鐘周期統計)

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

責任編輯:彭凡 來源: 博客園
相關推薦

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#函數式編程

2010-01-15 08:33:13

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

2011-08-01 16:24:04

XCode CodeTimer 測試

2010-03-16 09:09:04

F#

2010-04-07 09:46:05

2010-03-26 19:22:08

F#代理

2009-08-13 17:39:48

F#數據類型Discriminat

2011-06-09 09:52:41

F#

2010-08-16 16:12:58

F#

2010-03-08 09:17:13

F#異步

2009-09-10 14:18:59

Functional F#

2012-03-12 12:34:02

JavaF#

2012-11-06 10:01:35

ContinuatioF#

2009-12-04 09:16:44

Visual Stud

2009-12-14 09:04:10

F#運算符

2019-07-11 08:00:00

JavaScriptJulia編程語言
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 日本特黄特色aaa大片免费 | 欧美成人激情 | 亚洲精品二三区 | 成人av影院 | 久久久久久久久久久成人 | 男女污污动态图 | 91视频88av | 在线成人 | 九九国产在线观看 | 久久精品中文 | 免费一区二区三区 | 男人天堂网址 | 中文字幕亚洲一区 | 国产成人免费视频网站高清观看视频 | 国产精品毛片在线 | 在线观看中文字幕 | 在线视频成人 | 亚洲精品麻豆 | 日韩精品1区2区3区 爱爱综合网 | 天天干夜夜操 | 久草青青草 | 99精品久久 | 国产精品久久久久久久久久久新郎 | 欧美精品久久久久 | 337p日本欧洲亚洲大胆 | 欧美色成人 | 国产精品视频一区二区三区四区国 | 日韩欧美三级电影 | 欧美日韩三级视频 | 九九热在线免费视频 | 在线成人精品视频 | 91精品国产综合久久婷婷香蕉 | 一区二区精品在线 | 国产亚洲精品美女久久久久久久久久 | 欧美日韩在线高清 | 精品日本久久久久久久久久 | 91 在线 | 91一区二区三区 | 日韩欧美精品 | 亚洲一区二区精品视频 | 中文字幕丁香5月 |