Created
May 22, 2023 11:00
-
-
Save Szer/22f9bc58048eac1027e5cd4bb597c5ad to your computer and use it in GitHub Desktop.
This file contains 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
<Project Sdk="Microsoft.NET.Sdk"> | |
<PropertyGroup> | |
<OutputType>Library</OutputType> | |
<TargetFramework>net7.0</TargetFramework> | |
<ImplicitUsings>enable</ImplicitUsings> | |
<Nullable>enable</Nullable> | |
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> | |
<LangVersion>10</LangVersion> | |
</PropertyGroup> | |
</Project> |
This file contains 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
<Project Sdk="Microsoft.NET.Sdk"> | |
<PropertyGroup> | |
<OutputType>Exe</OutputType> | |
<TargetFramework>net7.0</TargetFramework> | |
<WarnOn>3390;$(WarnOn)</WarnOn> | |
</PropertyGroup> | |
<ItemGroup> | |
<Compile Include="Skynet.fs" /> | |
<Compile Include="Program.fs" /> | |
</ItemGroup> | |
<ItemGroup> | |
<ProjectReference Include="csharp.csproj" /> | |
</ItemGroup> | |
<ItemGroup> | |
<PackageReference Include="Hopac" Version="0.5.1" /> | |
</ItemGroup> | |
</Project> |
This file contains 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
namespace csharp | |
{ | |
using System.Linq; | |
public class Skynet | |
{ | |
static int children = 10; | |
static async Task<long> Loop(int level, long num) | |
{ | |
if (level == 0) | |
{ | |
return num; | |
} | |
var numFirst = num * children; | |
var numLast = numFirst + children - 1; | |
var lvl1 = level - 1; | |
IEnumerable<long> RangeOfNonSmoker(long start, long end) | |
{ | |
var i = start; | |
while (i <= end) | |
{ | |
yield return i++; | |
} | |
} | |
var tasks = RangeOfNonSmoker(numFirst, numLast).Select(x => Loop(lvl1, x)); | |
var result = await Task.WhenAll(tasks); | |
return result.Sum(); | |
} | |
public static long Run(int level) | |
{ | |
return Loop(level, 0).Result; | |
} | |
} | |
} |
This file contains 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
module Bench | |
open System | |
open System.Diagnostics | |
module GC = | |
let clean () = | |
GC.Collect() | |
GC.Collect() | |
let time name skynet = | |
GC.clean () | |
let timer = Stopwatch.StartNew() | |
let sum = skynet 9 | |
printfn $"%s{name} %d{sum} %A{timer.Elapsed}" | |
do | |
for i = 1 to 5 do | |
time "Tasks:" csharp.Skynet.Run | |
for i = 1 to 5 do | |
time "Hopac: " hopac.Skynet.run |
This file contains 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
namespace hopac | |
module Skynet = | |
open Hopac | |
open Hopac.Infixes | |
open Hopac.Extensions | |
let children = 10L | |
let rec skynet lvl num = | |
if lvl = 0 then | |
Job.result num | |
else | |
let numFirst = num * children | |
let numLast = numFirst + children - 1L | |
let lvl1 = lvl - 1 | |
seq { numFirst..numLast } | |
|> Seq.Con.mapJob (skynet lvl1) | |
>>- Seq.sum | |
let run level = | |
skynet level 0L | |
|> run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment