Created
January 22, 2015 02:55
-
-
Save ufcpp/fc4addbf20fe1e66af35 to your computer and use it in GitHub Desktop.
__arglist is much slower than params array.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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