Last active
August 29, 2015 14:19
-
-
Save MrSmith33/0ed293e334c9f73c553d to your computer and use it in GitHub Desktop.
Benchmarking ints vs floats
This file contains hidden or 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
// x86 release | |
// 0.000,294 secs byte | |
// 0.000,237 secs short | |
// 0.000,282 secs int | |
// 0.002,368 secs long | |
// 0.110,046 secs float | |
// 0.109,296 secs double | |
// 0.109,388 secs real | |
// | |
// x86_64 release | |
// 0.000,239 secs byte | |
// 0.000,245 secs short | |
// 0.000,246 secs int | |
// 0.000,313 secs long | |
// 0.001,158 secs float | |
// 0.001,197 secs double | |
// 0.109,964 secs real | |
import core.time; | |
import std.datetime; | |
import std.stdio; | |
import std.conv : to; | |
import std.algorithm; | |
import std.typetuple; | |
@system: | |
enum entityCount = 100_000; | |
enum numRuns = 10; | |
align(4) | |
struct Transform(T) | |
{ | |
ulong id; | |
T x, y, z; | |
} | |
Transform!long[] tLong; | |
Transform!int[] tInt; | |
Transform!short[] tShort; | |
Transform!byte[] tByte; | |
Transform!float[] tFloat; | |
Transform!double[] tDouble; | |
Transform!real[] tReal; | |
void main() | |
{ | |
tLong.length = entityCount; | |
tInt.length = entityCount; | |
tShort.length = entityCount; | |
tByte.length = entityCount; | |
tFloat.length = entityCount; | |
tDouble.length = entityCount; | |
tReal.length = entityCount; | |
auto r = benchmark!( | |
fun!tByte, | |
fun!tShort, | |
fun!tInt, | |
fun!tLong, | |
fun!tFloat, | |
fun!tDouble, | |
fun!tReal)(numRuns); | |
auto range = r[].map!(a => (to!Duration(a)/numRuns).formatDuration); | |
foreach(item; range) | |
item.writeln; | |
} | |
string formatDuration(Duration dur) | |
{ | |
import std.string : format; | |
auto splitted = dur.split(); | |
return format("%s.%03s,%03s secs", | |
splitted.seconds, splitted.msecs, splitted.usecs); | |
} | |
void fun(alias transforms)() | |
{ | |
foreach(ref transform; transforms) | |
{ | |
transform.x = transform.y / 2 + 4; | |
transform.y = transform.z / 2 + 4; | |
transform.z = transform.x / 2 + 4; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment