Skip to content

Instantly share code, notes, and snippets.

@nenoNaninu
Last active August 7, 2019 15:51
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 nenoNaninu/23abfa283b62a73f250e375ce8f42bf5 to your computer and use it in GitHub Desktop.
Save nenoNaninu/23abfa283b62a73f250e375ce8f42bf5 to your computer and use it in GitHub Desktop.
Genericsのほうが早んだけど!?
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace GenericsSpeedTest
{
static class Program
{
static int BiggerInt(int i, int j)
{
return (i.CompareTo(j) > 0) ? i : j;
}
static T Bigger<T>(T i, T j) where T : IComparable<T>
{
return (i.CompareTo(j) > 0) ? i : j;
}
static int _count = 10000;
static void NonJeneric()
{
for (int j = 0; j < _count; ++j)
{
for (int i = 0; i < _count; ++i)
{
var tmp = BiggerInt(i, j);
}
}
}
static void GenericsInt()
{
for (int j = 0; j < _count; ++j)
{
for (int i = 0; i < _count; ++i)
{
var tmp = Bigger(i, j);
}
}
}
static void Main(string[] args)
{
var sw = new Stopwatch();
sw.Restart();
GenericsInt();
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds);
sw.Restart();
GenericsInt();
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds);
sw.Restart();
NonJeneric();
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment