Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using System.Diagnostics;
- using System.IO;
- public class SpeedTest : MonoBehaviour
- {
- int sampleCount = 1000000;
- void Start ()
- {
- //################################################################################################# Vector3
- File.WriteAllText("Log.txt", "Samples: " + sampleCount + "\r\n");
- Stopwatch watch = new Stopwatch();
- float x = 2;
- File.AppendAllText("Log.txt", "Test: Vector3 => res += Vector3.one: ");
- Vector3 res = Vector3.one;
- watch.Reset();
- watch.Start();
- for (int i = 0; i < sampleCount; i++)
- {
- res += Vector3.one;
- }
- watch.Stop();
- File.AppendAllText("Log.txt", "" + watch.ElapsedMilliseconds + " ms\r\n");
- File.AppendAllText("Log.txt", "Test: Vector3 => Manual Add: ");
- res = Vector3.one;
- watch.Reset();
- watch.Start();
- for (int i = 0; i < sampleCount; i++)
- {
- res.x += Vector3.one.x;
- res.y += Vector3.one.y;
- res.z += Vector3.one.z;
- }
- watch.Stop();
- File.AppendAllText("Log.txt", "" + watch.ElapsedMilliseconds + " ms\r\n");
- File.AppendAllText("Log.txt", "Test: Vector3 => vec += vec ");
- res = Vector3.one;
- watch.Reset();
- watch.Start();
- Vector3 toAdd = Vector3.one;
- for (int i = 0; i < sampleCount; i++)
- {
- res += toAdd;
- }
- watch.Stop();
- File.AppendAllText("Log.txt", "" + watch.ElapsedMilliseconds + " ms\r\n");
- File.AppendAllText("Log.txt", "Test: Vector3 => Manual Add (non-property) ");
- res = Vector3.one;
- watch.Reset();
- watch.Start();
- toAdd = Vector3.one;
- for (int i = 0; i < sampleCount; i++)
- {
- res.x += toAdd.x;
- res.y += toAdd.y;
- res.z += toAdd.z;
- }
- watch.Stop();
- File.AppendAllText("Log.txt", "" + watch.ElapsedMilliseconds + " ms\r\n");
- File.AppendAllText("Log.txt", "Test: Vector3 => res *= x: ");
- res = Vector3.one;
- watch.Reset();
- watch.Start();
- for (int i = 0; i < sampleCount; i++)
- {
- res.x *= x;
- }
- watch.Stop();
- File.AppendAllText("Log.txt", "" + watch.ElapsedMilliseconds + " ms\r\n");
- File.AppendAllText("Log.txt", "\r\n");
- //################################################################################################# Vec3Str
- File.AppendAllText("Log.txt", "Test: Vec3Str => res += Vec3Str.one: ");
- Vec3Str res2 = Vec3Str.one;
- watch.Reset();
- watch.Start();
- for (int i = 0; i < sampleCount; i++)
- {
- res2 += Vec3Str.one;
- }
- watch.Stop();
- File.AppendAllText("Log.txt", "" + watch.ElapsedMilliseconds + " ms\r\n");
- File.AppendAllText("Log.txt", "Test: Vec3Str => res.Add(Vec3Str.one): ");
- res2 = Vec3Str.one;
- watch.Reset();
- watch.Start();
- for (int i = 0; i < sampleCount; i++)
- {
- res2.Add(Vec3Str.one);
- }
- watch.Stop();
- File.AppendAllText("Log.txt", "" + watch.ElapsedMilliseconds + " ms\r\n");
- File.AppendAllText("Log.txt", "Test: Vec3Str => res += Vec3Str.oneProp: ");
- res2 = Vec3Str.one;
- watch.Reset();
- watch.Start();
- for (int i = 0; i < sampleCount; i++)
- {
- res2 += Vec3Str.oneProp;
- }
- watch.Stop();
- File.AppendAllText("Log.txt", "" + watch.ElapsedMilliseconds + " ms\r\n");
- File.AppendAllText("Log.txt", "Test: Vec3Str => res.Add(Vec3Str.oneProp) : ");
- res2 = Vec3Str.one;
- watch.Reset();
- watch.Start();
- for (int i = 0; i < sampleCount; i++)
- {
- res2.Add(Vec3Str.oneProp);
- }
- watch.Stop();
- File.AppendAllText("Log.txt", "" + watch.ElapsedMilliseconds + " ms\r\n");
- File.AppendAllText("Log.txt", "Test: Vec3Str => Manual Add : ");
- res2 = Vec3Str.one;
- watch.Reset();
- watch.Start();
- for (int i = 0; i < sampleCount; i++)
- {
- res2.x += Vec3Str.one.x;
- res2.y += Vec3Str.one.y;
- res2.z += Vec3Str.one.z;
- }
- watch.Stop();
- File.AppendAllText("Log.txt", "" + watch.ElapsedMilliseconds + " ms\r\n");
- File.AppendAllText("Log.txt", "Test: Vec3Str => res *= x: ");
- res2 = Vec3Str.one;
- watch.Reset();
- watch.Start();
- for (int i = 0; i < sampleCount; i++)
- {
- res2 *= x;
- }
- watch.Stop();
- File.AppendAllText("Log.txt", "" + watch.ElapsedMilliseconds + " ms\r\n");
- File.AppendAllText("Log.txt", "Test: Vec3Str => res.Mul(x): ");
- res2 = Vec3Str.one;
- watch.Reset();
- watch.Start();
- for (int i = 0; i < sampleCount; i++)
- {
- res2.Mul(x);
- }
- watch.Stop();
- File.AppendAllText("Log.txt", "" + watch.ElapsedMilliseconds + " ms\r\n");
- File.AppendAllText("Log.txt", "\r\n");
- //################################################################################################# Vec3Cls
- File.AppendAllText("Log.txt", "Test: Vec3Cls => res += Vec3Cls.one: ");
- Vec3Cls res3 = Vec3Cls.one;
- watch.Reset();
- watch.Start();
- for (int i = 0; i < sampleCount; i++)
- {
- res3 += Vec3Cls.one;
- }
- watch.Stop();
- File.AppendAllText("Log.txt", "" + watch.ElapsedMilliseconds + " ms\r\n");
- File.AppendAllText("Log.txt", "Test: Vec3Cls => res.Add(Vec3Cls.one): ");
- res3 = Vec3Cls.one;
- watch.Reset();
- watch.Start();
- for (int i = 0; i < sampleCount; i++)
- {
- res3.Add(Vec3Cls.one);
- }
- watch.Stop();
- File.AppendAllText("Log.txt", "" + watch.ElapsedMilliseconds + " ms\r\n");
- File.AppendAllText("Log.txt", "Test: Vec3Cls => res += Vec3Cls.oneProp: ");
- res3 = Vec3Cls.one;
- watch.Reset();
- watch.Start();
- for (int i = 0; i < sampleCount; i++)
- {
- res3 += Vec3Cls.oneProp;
- }
- watch.Stop();
- File.AppendAllText("Log.txt", "" + watch.ElapsedMilliseconds + " ms\r\n");
- File.AppendAllText("Log.txt", "Test: Vec3Cls => res.Add(Vec3Cls.one): ");
- res3 = Vec3Cls.one;
- watch.Reset();
- watch.Start();
- for (int i = 0; i < sampleCount; i++)
- {
- res3.Add(Vec3Cls.oneProp);
- }
- watch.Stop();
- File.AppendAllText("Log.txt", "" + watch.ElapsedMilliseconds + " ms\r\n");
- File.AppendAllText("Log.txt", "Test: Vec3Cls => res += ManualAdd: ");
- res3 = Vec3Cls.one;
- watch.Reset();
- watch.Start();
- for (int i = 0; i < sampleCount; i++)
- {
- res3.x += Vec3Cls.one.x;
- res3.y += Vec3Cls.one.y;
- res3.z += Vec3Cls.one.z;
- }
- watch.Stop();
- File.AppendAllText("Log.txt", "" + watch.ElapsedMilliseconds + " ms\r\n");
- File.AppendAllText("Log.txt", "Test: Vec3Cls => res *= x: ");
- res3 = Vec3Cls.one;
- watch.Reset();
- watch.Start();
- for (int i = 0; i < sampleCount; i++)
- {
- res3 *= x;
- }
- watch.Stop();
- File.AppendAllText("Log.txt", "" + watch.ElapsedMilliseconds + " ms\r\n");
- File.AppendAllText("Log.txt", "Test: Vec3Cls => res.Mul(x): ");
- res3 = Vec3Cls.one;
- watch.Reset();
- watch.Start();
- for (int i = 0; i < sampleCount; i++)
- {
- res3.Mul(x);
- }
- watch.Stop();
- File.AppendAllText("Log.txt", "" + watch.ElapsedMilliseconds + " ms\r\n");
- }
- }
- struct Vec3Str
- {
- public static readonly Vec3Str one = new Vec3Str(1, 1, 1);
- public static Vec3Str oneProp
- {
- get { return one; }
- }
- public float x;
- public float y;
- public float z;
- public Vec3Str(float x, float y, float z)
- {
- this.x = x;
- this.y = y;
- this.z = z;
- }
- public static Vec3Str operator+(Vec3Str a, Vec3Str b)
- {
- return new Vec3Str(a.x + b.x, a.y + b.y, a.z + b.z);
- }
- public static Vec3Str operator -(Vec3Str a, Vec3Str b)
- {
- return new Vec3Str(a.x - b.x, a.y - b.y, a.z - b.z);
- }
- public static Vec3Str operator *(Vec3Str a, float b)
- {
- return new Vec3Str(a.x * b, a.y * b, a.z * b);
- }
- public void Add(Vec3Str a)
- {
- this.x += a.x;
- this.y += a.y;
- this.z += a.z;
- }
- public void Mul(float a)
- {
- this.x *= a;
- this.y *= a;
- this.z *= a;
- }
- }
- class Vec3Cls
- {
- public static readonly Vec3Cls one = new Vec3Cls(1, 1, 1);
- public static Vec3Cls oneProp
- {
- get { return one; }
- }
- public float x;
- public float y;
- public float z;
- public Vec3Cls(float x, float y, float z)
- {
- this.x = x;
- this.y = y;
- this.z = z;
- }
- public static Vec3Cls operator +(Vec3Cls a, Vec3Cls b)
- {
- return new Vec3Cls(a.x + b.x, a.y + b.y, a.z + b.z);
- }
- public static Vec3Cls operator -(Vec3Cls a, Vec3Cls b)
- {
- return new Vec3Cls(a.x - b.x, a.y - b.y, a.z - b.z);
- }
- public static Vec3Cls operator *(Vec3Cls a, float b)
- {
- return new Vec3Cls(a.x * b, a.y * b, a.z * b);
- }
- public void Add(Vec3Cls a)
- {
- this.x += a.x;
- this.y += a.y;
- this.z += a.z;
- }
- public void Mul(float a)
- {
- this.x *= a;
- this.y *= a;
- this.z *= a;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement