Skip to content

Instantly share code, notes, and snippets.

@pCYSl5EDgo
Last active December 3, 2019 09:59
Show Gist options
  • Save pCYSl5EDgo/1cf287edf90cecabe5c313851020f6f2 to your computer and use it in GitHub Desktop.
Save pCYSl5EDgo/1cf287edf90cecabe5c313851020f6f2 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
namespace test_range
{
class Program
{
static void Main(string[] args)
{
var enumerable0 = new int[114514];
IEnumerable<int> destination = default;
var watach = new Stopwatch();
const int repeat_count = 10000;
const int take_count = 1024;
watach.Restart();
destination = enumerable0.TakeLast(take_count);
for (int i = 0; i < repeat_count; i++)
{
foreach (var item in destination)
{
}
}
watach.Stop();
Console.WriteLine(destination.GetType().FullName);
Console.WriteLine("take last foreach(TakeLast): " + watach.ElapsedMilliseconds + " ms");
watach.Restart();
destination = enumerable0[^take_count..];
for (int i = 0; i < repeat_count; i++)
{
foreach (var item in destination)
{
}
}
watach.Stop();
Console.WriteLine(destination.GetType().FullName);
Console.WriteLine("take last foreach(C#8Range): " + watach.ElapsedMilliseconds + " ms");
watach.Restart();
var span = enumerable0.AsSpan()[^take_count..];
for (int i = 0; i < repeat_count; i++)
{
foreach (var item in span)
{
}
}
watach.Stop();
Console.WriteLine("take last foreach(span): " + watach.ElapsedMilliseconds + " ms");
}
}
}
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
namespace test_range
{
class Program
{
static void Main(string[] args)
{
var enumerable0 = new int[114514];
IEnumerable<int> destination = default;
var watach = new Stopwatch();
const int repeat_count = 10000000;
const int take_count = 1024;
watach.Restart();
for (int i = 0; i < repeat_count; i++)
{
destination = enumerable0.TakeLast(take_count);
}
watach.Stop();
Console.WriteLine(destination.GetType().FullName);
Console.WriteLine("take last(TakeLast): " + watach.ElapsedMilliseconds + " ms");
watach.Restart();
for (int i = 0; i < repeat_count; i++)
{
destination = enumerable0[^take_count..];
}
watach.Stop();
Console.WriteLine(destination.GetType().FullName);
Console.WriteLine("take last(C#8Range): " + watach.ElapsedMilliseconds + " ms");
watach.Restart();
for (int i = 0; i < repeat_count; i++)
{
var span = enumerable0.AsSpan()[^take_count..];
}
watach.Stop();
Console.WriteLine("take last(span): " + watach.ElapsedMilliseconds + " ms");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment