Skip to content

Instantly share code, notes, and snippets.

@vmeln
Created August 8, 2014 13:59
Show Gist options
  • Save vmeln/7d345d8aac17f5a5e61b to your computer and use it in GitHub Desktop.
Save vmeln/7d345d8aac17f5a5e61b to your computer and use it in GitHub Desktop.
Performance of invoking method with params array parameter
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Console
{
class Program
{
static void Main(string[] args)
{
// Method invoking without params specifying
var watch = Stopwatch.StartNew();
for (int i = 0; i < Int32.MaxValue; i++)
{
ParamMethod(String.Empty);
}
System.Console.WriteLine("Secods elapsed: {0}", watch.Elapsed.Seconds);
System.Console.WriteLine("Garbage collection times: {0}", System.GC.CollectionCount(0));
// Method invoking with null params specifying
watch = Stopwatch.StartNew();
for (int i = 0; i < Int32.MaxValue; i++)
{
ParamMethod(String.Empty, null);
}
System.Console.WriteLine("Secods elapsed: {0}", watch.Elapsed.Seconds);
System.Console.WriteLine("Garbage collection times: {0}", System.GC.CollectionCount(0));
// Method invoking without params specifying
// to prove Garbage Collections and array allocation
watch = Stopwatch.StartNew();
for (int i = 0; i < Int32.MaxValue; i++)
{
ParamMethod(String.Empty);
}
System.Console.WriteLine("Secods elapsed: {0}", watch.Elapsed.Seconds);
System.Console.WriteLine("Garbage collection times: {0}", System.GC.CollectionCount(0));
}
static void ParamMethod(string empty, params int[] rest)
{
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment