日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
詳解F#版本的CodeTimer方法實現(xiàn)

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

創(chuàng)新互聯(lián)公司主要從事網(wǎng)站設(shè)計制作、成都網(wǎng)站制作、網(wǎng)頁設(shè)計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務。立足成都服務尼開遠,10余年網(wǎng)站建設(shè)經(jīng)驗,價格優(yōu)惠、服務專業(yè),歡迎來電咨詢建站服務:18980820575

#T#

CodeTimer很好用,自從在今年三月在.NET技術(shù)大會上看到Jeffrey Richter用類似的東西之后,我就自己寫了一個。不過,當時是用C#寫的,現(xiàn)在我需要在F#里做相同的事情就不那么方便了。當然,F(xiàn)#與.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. extern int QueryThreadCycleTime(IntPtr threadHandle, uint64* cycleTime)
  9. []
  10. extern IntPtr GetCurrentThread();
  11. let private getCycleCount() = 
  12.     let mutable cycle = 0UL
  13.     let threadHandle = GetCurrentThread()
  14.     QueryThreadCycleTime(threadHandle, &&cycle) |> ignore
  15.     cycle
  16. let time name iteration action =
  17.     if (String.IsNullOrEmpty(name)) then ignore 0 else
  18.     // keep current color
  19.     let currentForeColor = Console.ForegroundColor
  20.     Console.ForegroundColor <- ConsoleColor.Yellow
  21.     printfn "%s" name
  22.     // keep current gc count
  23.     GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
  24.     let gcCounts =
  25.         [0 .. GC.MaxGeneration]
  26.         |> List.map (fun i -> (i, GC.CollectionCount(i)))
  27.         |> List.fold (fun acc i -> i :: acc) []
  28.         |> List.rev
  29.     // keep cycle count and start watch
  30.     let threadPtr = GetCurrentThread()
  31.     let cycle = getCycleCount()
  32.     let watch = Stopwatch.StartNew()
  33.     
  34.     // run
  35.     for i = 1 to iteration do action();
  36.     
  37.     let cycleUsed = getCycleCount() - cycle
  38.     watch.Stop()
  39.     // restore the color
  40.     Console.ForegroundColor <- currentForeColor;
  41.     // print
  42.     watch.ElapsedMilliseconds.ToString("N0") |> printfn "\tTime Elapsed:\t%sms"
  43.     cycle.ToString("N0") |> printfn "\tCPU Cycles:\t%s"
  44.     gcCounts |> List.iter (fun (i, c) -> 
  45.         printfn "\tGen%i:\t\t%i" i (GC.CollectionCount(i) - c))
  46.     printfn ""
  47. let initialize() =
  48.     Process.GetCurrentProcess().PriorityClass <- ProcessPriorityClass.High
  49.     Thread.CurrentThread.Priority <- ThreadPriority.Highest
  50.     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使用周期的消耗——不是我不想,而是遇到了問題。我當時這樣引入P/Invoke的簽名:

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

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

   
   
   
   
  1. []
  2. let main args =
  3.     let mutable cycle = 0u
  4.     let threadHandle = CodeTimer.GetCurrentThread()
  5.     CodeTimer.QueryThreadCycleTime(threadHandle, &&cycle) |> ignore
  6.     Console.ReadLine() |> ignore
  7.     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同學指出,引入QueryThreadCycleTime的時候,第二個參數(shù)應該是64位而不是32位無符號整數(shù)——我將PULONG64看作PULONG了。改成uint64便沒有問題了。

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


標題名稱:詳解F#版本的CodeTimer方法實現(xiàn)
網(wǎng)站URL:http://www.5511xx.com/article/djcpsei.html