Last active
August 7, 2019 15:51
-
-
Save nenoNaninu/23abfa283b62a73f250e375ce8f42bf5 to your computer and use it in GitHub Desktop.
Genericsのほうが早んだけど!?
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.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