Created
          June 8, 2015 09:04 
        
      - 
      
- 
        Save hodzanassredin/5650af2c02ada71505e6 to your computer and use it in GitHub Desktop. 
    ring threads benchmark async vs hopac job
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | open System | |
| open System.Diagnostics | |
| let ringLength = 503 | |
| let cells = Array.zeroCreate ringLength | |
| let threads = Array.zeroCreate ringLength | |
| let answer = ref -1 | |
| let createWorker i = | |
| let next = (i+1)%ringLength | |
| async { let value = cells.[i] | |
| if false then () | |
| match value with | |
| | 0 -> answer := i+1 | |
| | _ -> | |
| cells.[next] <- value - 1 | |
| return! threads.[next] } | |
| [<EntryPoint>] | |
| let main args = | |
| cells.[0] <- if args.Length>0 then int args.[0] else 50000000 | |
| for i in 0..ringLength-1 do | |
| threads.[i]<-createWorker i | |
| let stopwatch = new Stopwatch() | |
| stopwatch.Start() | |
| let result = Async.StartImmediate(threads.[0]) | |
| stopwatch.Stop() | |
| printfn "%d in %A" !answer stopwatch.Elapsed | |
| Console.ReadKey() |> ignore | |
| 0 | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | open System | |
| open System.Diagnostics | |
| open Hopac | |
| let ringLength = 503 | |
| let cells = Array.zeroCreate ringLength | |
| let threads : Job<unit>[] = Array.zeroCreate ringLength | |
| let answer = ref -1 | |
| let createWorker i = | |
| let next = (i+1)%ringLength | |
| job { let value = cells.[i] | |
| if false then () | |
| match value with | |
| | 0 -> answer := i+1 | |
| | _ -> | |
| cells.[next] <- value - 1 | |
| return! threads.[next] } | |
| [<EntryPoint>] | |
| let main args = | |
| cells.[0] <- if args.Length>0 then int args.[0] else 50000000 | |
| for i in 0..ringLength-1 do | |
| threads.[i]<-createWorker i | |
| let stopwatch = new Stopwatch() | |
| stopwatch.Start() | |
| let _ = threads.[0] |> run | |
| stopwatch.Stop() | |
| printfn "%d in %A" !answer stopwatch.Elapsed | |
| Console.ReadKey() |> ignore | |
| 0 | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
            
But mainly I was inspired by this video https://t.co/x3nGFJDnmm. And decided to check this naive version bacause I think authors of that video uses this kind of thread ring for tests.