Skip to content

Instantly share code, notes, and snippets.

@eqr
Last active July 24, 2019 17:03
Show Gist options
  • Save eqr/6885f2df58e863f4ea62722920c51451 to your computer and use it in GitHub Desktop.
Save eqr/6885f2df58e863f4ea62722920c51451 to your computer and use it in GitHub Desktop.
array iteration
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApp1
{
using System.Diagnostics;
using System.Threading.Tasks;
class Program
{
static void Main(string[] args)
{
var random = new Random();
var length = 100000;
var array1 = Enumerable.Range(0, length).Select(
i =>
{
return (byte)random.Next(255);
}).ToArray();
var array2 = new byte[length];
Array.Copy(array1, array2, length);
int testLength = 100000;
bool equals = true;
var sw = Stopwatch.StartNew();
for (int j = 0; j < testLength; j++)
{
for (int i = 0; i < array2.Length; i++)
{
if (array1[i] != array2[i])
{
equals = false;
}
}
}
sw.Stop();
Console.WriteLine(equals);
Console.WriteLine(sw.Elapsed);
sw = Stopwatch.StartNew();
for (int j = 0; j < testLength; j++)
{
for (int i = 0; i < array2.Length/2; i++)
{
var lengthToCheck = array2.Length -1 - i;
if (array1[i] != array2[i] || array1[lengthToCheck] != array2[lengthToCheck])
{
equals = false;
}
}
}
sw.Stop();
Console.WriteLine(equals);
Console.WriteLine(sw.Elapsed);
//True
//00:00:07.9054352
//True
//00:00:12.5653817
//Press any key to continue . . .
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment