Moni nonse! Nkhaniyi ndi mndandanda wa Zochita Zabwino zomwe anzanga ndi ine takhala tikugwiritsa ntchito kwa nthawi yayitali pogwira ntchito zosiyanasiyana.
Zambiri za makina omwe mawerengedwewo adawerengera:BenchmarkDotNet=v0.11.5, OS=Windows 10.0.18362
Intel Core i5-8250U CPU 1.60GHz (Kaby Lake R), 1 CPU, 8 zomveka ndi 4 cores
.NET Core SDK=3.0.100
[Wothandizira]: .NET Core 2.2.7 (CoreCLR 4.6.28008.02, CoreFX 4.6.28008.03), 64bit RyuJIT
Kore: .NET Kore 2.2.7 (CoreCLR 4.6.28008.02, CoreFX 4.6.28008.03), 64bit RyuJIT
[Wothandizira]: .NET Core 3.0.0 (CoreCLR 4.700.19.46205, CoreFX 4.700.19.46214), 64bit RyuJIT
Kore: .NET Kore 3.0.0 (CoreCLR 4.700.19.46205, CoreFX 4.700.19.46214), 64bit RyuJIT
M'malo mwa ine ndekha, ndikungofuna kukuwonetsani miyeso ndi zotsatira zanga; Ndinawonjezera malupu kwa iwo okonda "C ++ style" yolembera malupu.
Code:
public class Bench
{
private List<int> _list;
private int[] _array;
[Params(100000, 10000000)] public int N;
[GlobalSetup]
public void Setup()
{
const int MIN = 1;
const int MAX = 10;
Random random = new Random();
_list = Enumerable.Repeat(0, N).Select(i => random.Next(MIN, MAX)).ToList();
_array = _list.ToArray();
}
[Benchmark]
public int ForList()
{
int total = 0;
for (int i = 0; i < _list.Count; i++)
{
total += _list[i];
}
return total;
}
[Benchmark]
public int ForListFromEnd()
{
int total = 0;t
for (int i = _list.Count-1; i > 0; i--)
{
total += _list[i];
}
return total;
}
[Benchmark]
public int ForeachList()
{
int total = 0;
foreach (int i in _list)
{
total += i;
}
return total;
}
[Benchmark]
public int ForeachArray()
{
int total = 0;
foreach (int i in _array)
{
total += i;
}
return total;
}
[Benchmark]
public int ForArray()
{
int total = 0;
for (int i = 0; i < _array.Length; i++)
{
total += _array[i];
}
return total;
}
[Benchmark]
public int ForArrayFromEnd()
{
int total = 0;
for (int i = _array.Length-1; i > 0; i--)
{
total += _array[i];
}
return total;
}
}
Kuthamanga kwa machitidwe mu NET Core 2.2 ndi 3.0 ndi pafupifupi zofanana. Izi ndi zomwe ndidakwanitsa kulowa mu NET Core 3.0:
Titha kunena kuti kubwerezabwereza kwa gulu la Array ndikwachangu chifukwa cha kukhathamiritsa kwake mkati komanso kugawika kwa makulidwe otolera. Ndikoyeneranso kukumbukira kuti mndandanda wa List uli ndi ubwino wake ndipo muyenera kugwiritsa ntchito zosonkhanitsa zoyenera malinga ndi mawerengedwe ofunikira. Ngakhale mutalemba zomveka zogwirira ntchito ndi malupu, musaiwale kuti iyi ndi loop wamba ndipo imakhalanso ndi kukhathamiritsa kwa loop. Nkhani idasindikizidwa pa habr kalekale: https://habr.com/ru/post/124910/. Ikadali yofunikira komanso yovomerezeka kuwerenga.
Kutaya
Chaka chapitacho, ndinagwira ntchito pakampani ina yomwe inakhalapo kale, pulojekitiyi zinali zachilendo kukonza zovomerezeka pogwiritsa ntchito kuyesa-kuponya. Ndinamvetsetsa kale kuti iyi inali malingaliro olakwika abizinesi pantchitoyo, kotero ngati kuli kotheka ndimayesetsa kusagwiritsa ntchito kapangidwe kake. Koma tiyeni tiwone chifukwa chake njira yothanirana ndi zolakwika ndi zomangamanga zotere ndi yoyipa. Ndinalemba kachidindo kakang'ono kuti ndifanizire njira ziwirizo ndikupanga zizindikiro za njira iliyonse.
Code:
public bool ContainsHash()
{
bool result = false;
foreach (var file in _files)
{
var extension = Path.GetExtension(file);
if (_hash.Contains(extension))
result = true;
}
return result;
}
public bool ContainsHashTryCatch()
{
bool result = false;
try
{
foreach (var file in _files)
{
var extension = Path.GetExtension(file);
if (_hash.Contains(extension))
result = true;
}
if(!result)
throw new Exception("false");
}
catch (Exception e)
{
result = false;
}
return result;
}
Zotsatira mu .NET Core 3.0 ndi Core 2.2 zili ndi zotsatira zofanana (.NET Core 3.0):