using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
BenchmarkRunner
.Run<RandomBenchmarks>();
[MemoryDiagnoser]
public class RandomBenchmarks
{
private static readonly Random Random = new();
[Benchmark]
public void Reuse()
{
Random.Next();
}
[Benchmark]
public void CreateNew()
{
new Random().Next();
}
}
// * Summary *
BenchmarkDotNet=v0.13.1, OS=Windows 10.0.22463
AMD Ryzen 5 4600H with Radeon Graphics, 1 CPU, 12 logical and 6 physical cores
.NET SDK=6.0.100-rc.1.21463.6
[Host] : .NET 6.0.0 (6.0.21.45113), X64 RyuJIT
DefaultJob : .NET 6.0.0 (6.0.21.45113), X64 RyuJIT
| Method | Mean | Error | StdDev | Gen 0 | Allocated |
|---------- |-----------:|----------:|----------:|-------:|----------:|
| Reuse | 3.128 ns | 0.0987 ns | 0.1351 ns | - | - |
| CreateNew | 114.934 ns | 2.3160 ns | 4.8852 ns | 0.0343 | 72 B |
// * Legends *
Mean : Arithmetic mean of all measurements
Error : Half of 99.9% confidence interval
StdDev : Standard deviation of all measurements
Gen 0 : GC Generation 0 collects per 1000 operations
Allocated : Allocated memory per single operation (managed only, inclusive, 1KB = 1024B)
1 ns : 1 Nanosecond (0.000000001 sec)
var arr = new int[] { 1, 1, 1, 2, 3, 3, 4, 4, 4, 4, 5, 6, 6, 7, 8 };
var index = arr.GroupBy(x => x).ToDictionary(x => x.Key, x => x.Count());
var result = arr.OrderByDescending(x => x, new CountComparer(index)).ToArray();
class CountComparer : IComparer<int>
{
private readonly IReadOnlyDictionary<int, int> _index;
public CountComparer(IReadOnlyDictionary<int, int> index)
{
_index = index;
}
public int Compare(int x, int y)
{
var comparsion = _index[x].CompareTo(_index[y]);
if (comparsion == 0)
return x.CompareTo(y);
return comparsion;
}
}
using System;
for (var i = 1; i < 100; i++)
{
var dividesBy3 = i % 3 == 0;
var dividesBy5 = i % 5 == 0;
if (dividesBy3 && dividesBy5)
Console.WriteLine("OuuMay");
else if (dividesBy3)
Console.WriteLine("Ouu");
else if (dividesBy5)
Console.WriteLine("May");
else
Console.WriteLine(i);
}
Console.ReadKey();
T заранее не известен, но можно ли написать какую-нибудь конструкцию, чтобы уже в процессе выполнения программы по переданному параметру Т обращаться к функции Parse?(или прекращать выполнение подпрограммы, если T некорректен)
Dictionary<Type, IParser> parsers
interface IParser { object Parse(string str); }
var parser = parsers[typeof(T)];
if(parser.Parse(str) is T data)
u.Add(data);
else
Console.WriteLine("Некорректно введенные данные в файле in.txt в строке " + lineNumber);
[JsonPropertyName("data")]
public JsonElement[][] Data {get; init;}
var regex = new Regex(@"\d+", RegexOptions.Compiled);
var text= "is2 Thi1s T4est 3a";
var matches = regex.Matches(text);
var result = matches.Select(x=>x.Value).Select(int.Parse).ToArray(); // {2, 1, 4, 3}
class Value {
public int Id {get; set;}
public string File {get;set;}
}
var dictionary = new Dictionary<string, Value> {
["asd"] = new Value { Id = 1, File = "File.png" }
}
var dictionary = new Dictionary<string, (int Id, string File)> {
["asd"] = (1, "File.png")
}
class Text {
private static byte maxCharIndex = 255;
public static string generateText(int textLength = 1)
{
char[] chars = new char[textLength]; // всё равно размер для массивов задаётся интом
Random r = new Random();
for (T i = 0; i < textLength; i++)
{
chars[i] = Convert.ToChar(r.Next(0, maxCharIndex));
}
return new string(chars);
}
}
2. Не избыточна ли тут ConcurrentQueue?
3. Как сообщить основному потоку из Task.run() что все готово к обновлению карт? Я понимаю что есть Task.Result, но если Task выполняется постоянно (while true), смогу ли я это использовать?
var desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
var targetPath = Path.Combine(desktop, "sus(42).jpg");
File.Copy(@"./Resources/peele.jpg", targetPath);