Advertisement
fsb4000

F#

Feb 12th, 2020
2,767
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 2.48 KB | None | 0 0
  1. open System;
  2. open System.Diagnostics;
  3. open System.Threading; // для ThreadLocal
  4.  
  5. module SafeRandom =
  6.   let private rnd = new Random()
  7.   let New() =
  8.     lock rnd (fun () ->
  9.         new Random(rnd.Next()))
  10.  
  11. #if !NET20
  12. type ThreadSafeRandom() =
  13.     let r = new ThreadLocal<Random>(fun _ -> SafeRandom.New())
  14.     member _.Next = r.Value.Next
  15.     interface IDisposable with
  16.         member _.Dispose() = r.Dispose()
  17. #endif
  18.  
  19. #if NET20
  20. type ThreadSafeRandom() =
  21.     [<ThreadStatic>]
  22.     [<DefaultValue>]
  23.     static val mutable private r: Random
  24.     member _.Next =
  25.         if ThreadSafeRandom.r = null then
  26.             ThreadSafeRandom.r <- SafeRandom.New()
  27.         ThreadSafeRandom.r.Next
  28.     interface IDisposable with // для совместимости с более новой версией
  29.         member _.Dispose() = ()
  30. #endif
  31.    
  32. #if NET20
  33. module Array =
  34.     module Parallel =
  35.         let map f (arr: _[]) =
  36.             let len = arr.GetLength(0)
  37.             let res = Array.zeroCreate len
  38.             let loop i =
  39.                 let start = (i*len)/Environment.ProcessorCount
  40.                 let finish = ((i+1)*len)/Environment.ProcessorCount - 1
  41.                 for j in start .. finish do
  42.                     res.[j] <- f(arr.[j])
  43.             Async.Parallel [ for i in 0..Environment.ProcessorCount-1 -> async {do loop(i)} ]
  44.                 |> Async.Ignore
  45.                 |> Async.RunSynchronously
  46.             res
  47. #endif
  48. [<EntryPoint>]
  49. let main _ =
  50.     use random = new ThreadSafeRandom()
  51.     let randomStr =
  52.         let chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
  53.         let charsLen = chars.Length
  54.         fun len ->
  55.             let randomChars = [|for _ in 0..len-1 -> chars.[random.Next(charsLen)]|]
  56.             new string(randomChars)
  57.     let randomString100(_) = randomStr(100)
  58.     let randomString20() = randomStr(20)
  59.     for _ in 1..10 do
  60.         let stopWatch = Stopwatch.StartNew()
  61.         let listString100 = Array.init 100000 randomString100
  62.         let  matches =
  63.             [|1..1000|]
  64.             |> Array.Parallel.map (fun _ ->
  65.                 let s20 = randomString20()
  66.                 listString100
  67.                     |> Array.exists (fun s100 -> s100.Contains(s20))
  68.                     |> fun exists -> if exists then 1 else 0)
  69.             |> Array.sum
  70.         printfn "%d strings matches, time = %f"
  71.             matches stopWatch.Elapsed.TotalMilliseconds
  72.     0 // return an integer exit code
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement