Created
August 19, 2022 23:52
-
-
Save altbodhi/0eae7e2fecc64029907fef42fe8ff616 to your computer and use it in GitHub Desktop.
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
using System.IO; | |
using System.Diagnostics; | |
using static System.Console; | |
var sw = Stopwatch.StartNew(); | |
var alf = get_alf(); | |
do_it(2_000_000); | |
sw.Stop(); | |
WriteLine(sw.Elapsed); // <= 00:00:13.4216229 it is very fast! fantasctic! | |
char[] get_alf() | |
{ | |
var list = new List<char>(); | |
for (char x = 'A'; x <= 'Z'; x++) | |
list.Add(x); | |
for (char x = 'a'; x <= 'z'; x++) | |
list.Add(x); | |
list.Add(' '); | |
return list.ToArray(); | |
} | |
string gen_str() | |
{ | |
var range = Enumerable.Range(0, Random.Shared.Next(20, 100)); | |
var chars = range.Select(x => alf[Random.Shared.Next(0, alf.Length)]).ToArray(); | |
return new string(chars); | |
} | |
int gen_num() => Random.Shared.Next(0, 10_000); | |
void do_it(int count) | |
{ | |
var source = $"L{count}.txt"; | |
var dist = $"S{count}.txt"; | |
var comp = new SimpleComparer(); | |
File.WriteAllLines(source, Enumerable.Range(0, count).Select(x => $"{gen_num()}.{gen_str()}")); | |
File.WriteAllLines(dist, File.ReadAllLines(source).OrderBy(x => x, comp)); | |
} | |
class SimpleComparer : IComparer<string> | |
{ | |
public int Compare(string s1, string s2) | |
{ | |
var p1 = s1.IndexOf('.'); | |
var v1 = s1.AsSpan(p1 + 2); | |
var p2 = s2.IndexOf('.'); | |
var v2 = s2.AsSpan(p2 + 2); | |
var vc = | |
v1.CompareTo(v2, StringComparison.Ordinal); | |
if (vc != 0) | |
return vc; | |
else | |
{ | |
var n1 = Int32.Parse(s1.AsSpan(0, p1)); | |
var n2 = Int32.Parse(s2.AsSpan(0, p2)); | |
return n1.CompareTo(n2); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment