Skip to content

Instantly share code, notes, and snippets.

@ufcpp
Created January 22, 2015 02:55
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 ufcpp/fc4addbf20fe1e66af35 to your computer and use it in GitHub Desktop.
Save ufcpp/fc4addbf20fe1e66af35 to your computer and use it in GitHub Desktop.
__arglist is much slower than params array.
using System;
using System.Diagnostics;
public class ArgListIsTooSlow
{
static void Main(string[] args)
{
const int N = 100000;
var sw = new Stopwatch();
sw.Reset();
sw.Start();
for (int i = 0; i < N; i++)
{
Params.Sum(1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0);
}
sw.Stop();
Console.WriteLine(sw.Elapsed);
sw.Reset();
sw.Start();
for (int i = 0; i < N; i++)
{
Arglist.Sum(__arglist(1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0));
}
sw.Stop();
Console.WriteLine(sw.Elapsed);
}
class Params
{
public static int Sum(params int[] values)
{
int sum = 0;
for (int i = 0; i < values.Length; i++)
{
sum += values[i];
}
return sum;
}
}
class Arglist
{
public static int Sum(__arglist)
{
var values = new ArgIterator(__arglist);
int sum = 0;
int count = values.GetRemainingCount();
for (int i = 0; i < count; i++)
{
sum += __refvalue(values.GetNextArg(), int);
}
return sum;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment