| 式 | 時間(速度) |
|---|---|
| Double <= | 00:00:10.0156250 |
| Double IsNaN | 00:00:03.9062500 |
| Double IsPositiveInfinity | 00:00:09.6250000 |
| Int <= | 00:00:01.2968750 |
| Int64 <= | 00:00:02.4687500 |
int は Double より3倍以上早い。赤くないのに3倍以上も早い。
整数型はやっぱ凄いな。
Double <= が意外に遅い。
Double.IsNaN は健闘している。
Double IsPositiveInfinity は微妙なところ。Double <= より数字は良いので責めるべきではないけどな。
Int64 は intより2倍ぐらい遅いけど、 Double よりマシなんで、 Double を Int64の固定少数で置き換えるテクニックはまだ有効らしい。ただ、面倒だからやらないけどさ。
結論。
Double が遅いのはあたりまえなんであきらめましょう。
大量に Double の演算をしなければならないルーチンを見直すべき。
検証に利用したコード
using System;
using System.Collections.Generic;
using System.Text;
namespace DoubleBench
{
class TimeWatcher : IDisposable
{
private string Message;
private DateTime StartTime;
public TimeWatcher(string inMessage)
{
this.Message = inMessage;
this.StartTime = DateTime.Now;
}
public void Dispose()
{
Console.WriteLine("{0}:{1}", this.Message, DateTime.Now - this.StartTime);
}
}
class Program
{
static void Main(string[] args)
{
int loopCount = 1000000000;
{
Double a = 0;
using( TimeWatcher t = new TimeWatcher("Double <=") )
{
for (int i = 0; i < loopCount; i++)
{
a += 1;
if (a <= -1)
{
Console.WriteLine("<=!"); //絶対成立しないけど最適化防止ぐらいにはなるかな?
}
}
}
}
{
Double a = 0;
using( TimeWatcher t = new TimeWatcher("Double IsNaN") )
{
for (int i = 0; i < loopCount; i++)
{
a += 1;
if (Double.IsNaN(a))
{
Console.WriteLine("NaN!"); //絶対成立しないけど最適化防止ぐらいにはなるかな?
}
}
}
}
{
Double a = 0;
using( TimeWatcher t = new TimeWatcher("Double IsPositiveInfinity") )
{
for (int i = 0; i < loopCount; i++)
{
a += 1;
if (Double.IsPositiveInfinity(a))
{
Console.WriteLine("IsPositiveInfinity!"); //絶対成立しないけど最適化防止ぐらいにはなるかな?
}
}
}
}
{
int a = 0;
using( TimeWatcher t = new TimeWatcher("Int <=") )
{
for (int i = 0; i < loopCount; i++)
{
a += 1;
if (a <= -1)
{
Console.WriteLine("<=!"); //絶対成立しないけど最適化防止ぐらいにはなるかな?
}
}
}
}
{
Int64 a = 0;
using( TimeWatcher t = new TimeWatcher("Int64 <=") )
{
for (int i = 0; i < loopCount; i++)
{
a += 1;
if (a <= -1)
{
Console.WriteLine("<=!"); //絶対成立しないけど最適化防止ぐらいにはなるかな?
}
}
}
}
}
}
}

