Skip to content

Instantly share code, notes, and snippets.

@altbodhi
Created August 19, 2022 01:48
Show Gist options
  • Save altbodhi/19041dd34dd335ab540aacfef0263ad7 to your computer and use it in GitHub Desktop.
Save altbodhi/19041dd34dd335ab540aacfef0263ad7 to your computer and use it in GitHub Desktop.
Реализация тестового задания на примере https://github.com/podkolzzzin/Stream.Profiling
open System
open System.IO
let num () = Random.Shared.Next(0, 10_000)
let alf =
[| ' ' |]
|> Array.append [| 'A' .. 'Z' |]
|> Array.append [| 'a' .. 'z' |]
let str () =
String(
[| for i = 0 to Random.Shared.Next(20, 100) do
yield alf.[Random.Shared.Next(0, alf.Length)] |]
)
let gen count =
Seq.init count (fun i -> $"{num ()}.{str ()}")
let count = 2_000_000
Directory.SetCurrentDirectory(@"c:\\tmp\\tests\\1")
let source = $"L{count}.txt"
let dist = $"S{count}.txt"
let do_it () =
File.WriteAllLines(source, gen count)
File.ReadAllLines(source)
|> Seq.sortWith (fun s1 s2 ->
let p1 = s1.IndexOf('.')
let v1 = s1.AsSpan(p1 + 2)
let p2 = s2.IndexOf('.')
let v2 = s2.AsSpan(p2 + 2)
let vc =
v1.CompareTo(v2, StringComparison.Ordinal)
if not (vc = 0) then
vc
else
let n1 = Int32.Parse(s1.AsSpan(0, p1))
let n2 = Int32.Parse(s2.AsSpan(0, p2))
n1.CompareTo(n2))
|> fun result -> File.WriteAllLines(dist, result)
#time
do_it ()
(*
C:\tmp\tests\Stream.Profiling\Stream.Profiling>dotnet run
Execution took: 00:00:57.0215479
C:\tmp\tests\1>dotnet fsi --exec sortfile.fsx
Real: 00:00:13.280, ЦП: 00:00:13.687, GC gen0: 2051, gen1: 23, gen2: 3
WOW! In 4.4 faster! I am shocked!
*)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment