Skip to content

Instantly share code, notes, and snippets.

@atifaziz
Created October 8, 2008 08:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atifaziz/15474 to your computer and use it in GitHub Desktop.
Save atifaziz/15474 to your computer and use it in GitHub Desktop.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
internal static class Program
{
private static void Main(string[] args)
{
Thread.CurrentThread.Priority = ThreadPriority.AboveNormal;
Action<string> print = Console.WriteLine; // alias
int iterations = args.Length > 0 ? int.Parse(args[0]) : 10000000;
int[] array = new int[] { 1, 2, 3 };
List<int> list = new List<int>(array);
print("Iterations = " + iterations.ToString("N0"));
print("Array...Enumerator....: " + Time(iterations, delegate { Any1(array); }));
print("Array...ICollection...: " + Time(iterations, delegate { Any2(array); }));
print("Array...ICollection<T>: " + Time(iterations, delegate { Any3(array); }));
print("List....Enumerator....: " + Time(iterations, delegate { Any1(list); }));
print("List....ICollection...: " + Time(iterations, delegate { Any2(list); }));
print("List....ICollection<T>: " + Time(iterations, delegate { Any3(list); }));
}
private delegate void Block();
private static TimeSpan Time(int iterations, Block block)
{
block(); // Heat up things, like pre-JIT
Stopwatch sw = new Stopwatch();
sw.Start();
for (int i = 0; i < iterations; i++)
block();
sw.Stop();
return sw.Elapsed;
}
private static bool Any1<TSource>(
IEnumerable<TSource> source)
{
using (IEnumerator<TSource> e = source.GetEnumerator())
return e.MoveNext();
}
private static bool Any2<TSource>(
IEnumerable<TSource> source)
{
return ((ICollection)source).Count > 0;
}
private static bool Any3<TSource>(
IEnumerable<TSource> source)
{
return ((ICollection<TSource>)source).Count > 0;
}
}
Microsoft (R) Visual C# 2005 Compiler version 8.00.50727.3053
for Microsoft (R) Windows (R) 2005 Framework version 2.0.50727
Copyright (C) Microsoft Corporation 2001-2005. All rights reserved.
Iterations = 10,000,000
Array...Enumerator....: 00:00:00.9804561
Array...ICollection...: 00:00:00.2222011
Array...ICollection<T>: 00:00:14.2870269
List....Enumerator....: 00:00:00.8043882
List....ICollection...: 00:00:00.2742192
List....ICollection<T>: 00:00:00.1998851
Microsoft (R) Visual C# 2008 Compiler version 3.5.30729.1
for Microsoft (R) .NET Framework version 3.5
Copyright (C) Microsoft Corporation. All rights reserved.
Iterations = 10,000,000
Array...Enumerator....: 00:00:00.9361854
Array...ICollection...: 00:00:00.2228922
Array...ICollection<T>: 00:00:14.4773950
List....Enumerator....: 00:00:00.7489351
List....ICollection...: 00:00:00.2767027
List....ICollection<T>: 00:00:00.1987006
@if /i "%1" neq "//x" (
echo off
) else (
shift
)
setlocal
set NETFX_BASE_PATH=%SystemRoot%\Microsoft.NET\Framework
set NETFX_2_0=%NETFX_BASE_PATH%\v2.0.50727
set NETFX_3_5=%NETFX_BASE_PATH%\v3.5
set CSC_OPT=/target:exe /noconfig /nowarn:1701,1702 /errorreport:prompt /warn:4 /define:TRACE /reference:%NETFX_2_0%\System.dll /debug:pdbonly /optimize+ *.cs
for %%i in (test?.exe test?.pdb) do del %%i
%NETFX_2_0%\csc.exe /out:test2.exe %CSC_OPT%
if %ERRORLEVEL% equ 0 test2 %1 %2 %3 %4 & echo.
%NETFX_3_5%\csc.exe /out:test3.exe %CSC_OPT%
if %ERRORLEVEL% equ 0 test3 %1 %2 %3 %4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment