Advertisement
Guest User

Untitled

a guest
Feb 7th, 2018
446
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.93 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.Diagnostics;
  5. using System.IO;
  6.  
  7. public class SpeedTest : MonoBehaviour
  8. {
  9.     int sampleCount = 1000000;
  10.  
  11.     void Start ()
  12.     {
  13.         //################################################################################################# Vector3
  14.         File.WriteAllText("Log.txt", "Samples: " + sampleCount + "\r\n");
  15.         Stopwatch watch = new Stopwatch();
  16.         float x = 2;
  17.  
  18.         File.AppendAllText("Log.txt", "Test: Vector3 => res += Vector3.one: ");
  19.         Vector3 res = Vector3.one;
  20.         watch.Reset();
  21.         watch.Start();
  22.         for (int i = 0; i < sampleCount; i++)
  23.         {
  24.             res += Vector3.one;
  25.         }
  26.         watch.Stop();
  27.         File.AppendAllText("Log.txt", "" + watch.ElapsedMilliseconds + " ms\r\n");
  28.  
  29.  
  30.         File.AppendAllText("Log.txt", "Test: Vector3 => Manual Add: ");
  31.         res = Vector3.one;
  32.         watch.Reset();
  33.         watch.Start();
  34.         for (int i = 0; i < sampleCount; i++)
  35.         {
  36.             res.x += Vector3.one.x;
  37.             res.y += Vector3.one.y;
  38.             res.z += Vector3.one.z;
  39.         }
  40.         watch.Stop();
  41.         File.AppendAllText("Log.txt", "" + watch.ElapsedMilliseconds + " ms\r\n");
  42.  
  43.         File.AppendAllText("Log.txt", "Test: Vector3 => vec += vec ");
  44.         res = Vector3.one;
  45.         watch.Reset();
  46.         watch.Start();
  47.         Vector3 toAdd = Vector3.one;
  48.         for (int i = 0; i < sampleCount; i++)
  49.         {
  50.             res += toAdd;
  51.         }
  52.         watch.Stop();
  53.         File.AppendAllText("Log.txt", "" + watch.ElapsedMilliseconds + " ms\r\n");
  54.  
  55.  
  56.         File.AppendAllText("Log.txt", "Test: Vector3 => Manual Add (non-property) ");
  57.         res = Vector3.one;
  58.         watch.Reset();
  59.         watch.Start();
  60.         toAdd = Vector3.one;
  61.         for (int i = 0; i < sampleCount; i++)
  62.         {
  63.             res.x += toAdd.x;
  64.             res.y += toAdd.y;
  65.             res.z += toAdd.z;
  66.         }
  67.         watch.Stop();
  68.         File.AppendAllText("Log.txt", "" + watch.ElapsedMilliseconds + " ms\r\n");
  69.  
  70.         File.AppendAllText("Log.txt", "Test: Vector3 => res *= x: ");
  71.         res = Vector3.one;
  72.         watch.Reset();
  73.         watch.Start();
  74.         for (int i = 0; i < sampleCount; i++)
  75.         {
  76.             res.x *= x;
  77.         }
  78.         watch.Stop();
  79.         File.AppendAllText("Log.txt", "" + watch.ElapsedMilliseconds + " ms\r\n");
  80.  
  81.         File.AppendAllText("Log.txt", "\r\n");
  82.         //################################################################################################# Vec3Str
  83.  
  84.         File.AppendAllText("Log.txt", "Test: Vec3Str => res += Vec3Str.one: ");
  85.         Vec3Str res2 = Vec3Str.one;
  86.         watch.Reset();
  87.         watch.Start();
  88.         for (int i = 0; i < sampleCount; i++)
  89.         {
  90.             res2 += Vec3Str.one;
  91.         }
  92.         watch.Stop();
  93.         File.AppendAllText("Log.txt", "" + watch.ElapsedMilliseconds + " ms\r\n");
  94.  
  95.         File.AppendAllText("Log.txt", "Test: Vec3Str => res.Add(Vec3Str.one): ");
  96.         res2 = Vec3Str.one;
  97.         watch.Reset();
  98.         watch.Start();
  99.         for (int i = 0; i < sampleCount; i++)
  100.         {
  101.             res2.Add(Vec3Str.one);
  102.         }
  103.         watch.Stop();
  104.         File.AppendAllText("Log.txt", "" + watch.ElapsedMilliseconds + " ms\r\n");
  105.  
  106.         File.AppendAllText("Log.txt", "Test: Vec3Str => res += Vec3Str.oneProp: ");
  107.         res2 = Vec3Str.one;
  108.         watch.Reset();
  109.         watch.Start();
  110.         for (int i = 0; i < sampleCount; i++)
  111.         {
  112.             res2 += Vec3Str.oneProp;
  113.         }
  114.         watch.Stop();
  115.         File.AppendAllText("Log.txt", "" + watch.ElapsedMilliseconds + " ms\r\n");
  116.  
  117.  
  118.         File.AppendAllText("Log.txt", "Test: Vec3Str => res.Add(Vec3Str.oneProp) : ");
  119.         res2 = Vec3Str.one;
  120.         watch.Reset();
  121.         watch.Start();
  122.         for (int i = 0; i < sampleCount; i++)
  123.         {
  124.             res2.Add(Vec3Str.oneProp);
  125.         }
  126.         watch.Stop();
  127.         File.AppendAllText("Log.txt", "" + watch.ElapsedMilliseconds + " ms\r\n");
  128.  
  129.         File.AppendAllText("Log.txt", "Test: Vec3Str => Manual Add : ");
  130.         res2 = Vec3Str.one;
  131.         watch.Reset();
  132.         watch.Start();
  133.         for (int i = 0; i < sampleCount; i++)
  134.         {
  135.             res2.x += Vec3Str.one.x;
  136.             res2.y += Vec3Str.one.y;
  137.             res2.z += Vec3Str.one.z;
  138.         }
  139.         watch.Stop();
  140.         File.AppendAllText("Log.txt", "" + watch.ElapsedMilliseconds + " ms\r\n");
  141.  
  142.         File.AppendAllText("Log.txt", "Test: Vec3Str => res *= x: ");
  143.         res2 = Vec3Str.one;
  144.         watch.Reset();
  145.         watch.Start();
  146.         for (int i = 0; i < sampleCount; i++)
  147.         {
  148.             res2 *= x;
  149.         }
  150.         watch.Stop();
  151.         File.AppendAllText("Log.txt", "" + watch.ElapsedMilliseconds + " ms\r\n");
  152.  
  153.         File.AppendAllText("Log.txt", "Test: Vec3Str => res.Mul(x): ");
  154.         res2 = Vec3Str.one;
  155.         watch.Reset();
  156.         watch.Start();
  157.         for (int i = 0; i < sampleCount; i++)
  158.         {
  159.             res2.Mul(x);
  160.         }
  161.         watch.Stop();
  162.         File.AppendAllText("Log.txt", "" + watch.ElapsedMilliseconds + " ms\r\n");
  163.  
  164.         File.AppendAllText("Log.txt", "\r\n");
  165.         //################################################################################################# Vec3Cls
  166.  
  167.         File.AppendAllText("Log.txt", "Test: Vec3Cls => res += Vec3Cls.one: ");
  168.         Vec3Cls res3 = Vec3Cls.one;
  169.         watch.Reset();
  170.         watch.Start();
  171.         for (int i = 0; i < sampleCount; i++)
  172.         {
  173.             res3 += Vec3Cls.one;
  174.         }
  175.         watch.Stop();
  176.         File.AppendAllText("Log.txt", "" + watch.ElapsedMilliseconds + " ms\r\n");
  177.  
  178.         File.AppendAllText("Log.txt", "Test: Vec3Cls => res.Add(Vec3Cls.one): ");
  179.         res3 = Vec3Cls.one;
  180.         watch.Reset();
  181.         watch.Start();
  182.         for (int i = 0; i < sampleCount; i++)
  183.         {
  184.             res3.Add(Vec3Cls.one);
  185.         }
  186.         watch.Stop();
  187.         File.AppendAllText("Log.txt", "" + watch.ElapsedMilliseconds + " ms\r\n");
  188.  
  189.         File.AppendAllText("Log.txt", "Test: Vec3Cls => res += Vec3Cls.oneProp: ");
  190.         res3 = Vec3Cls.one;
  191.         watch.Reset();
  192.         watch.Start();
  193.         for (int i = 0; i < sampleCount; i++)
  194.         {
  195.             res3 += Vec3Cls.oneProp;
  196.         }
  197.         watch.Stop();
  198.         File.AppendAllText("Log.txt", "" + watch.ElapsedMilliseconds + " ms\r\n");
  199.  
  200.         File.AppendAllText("Log.txt", "Test: Vec3Cls => res.Add(Vec3Cls.one): ");
  201.         res3 = Vec3Cls.one;
  202.         watch.Reset();
  203.         watch.Start();
  204.         for (int i = 0; i < sampleCount; i++)
  205.         {
  206.             res3.Add(Vec3Cls.oneProp);
  207.         }
  208.         watch.Stop();
  209.         File.AppendAllText("Log.txt", "" + watch.ElapsedMilliseconds + " ms\r\n");
  210.  
  211.         File.AppendAllText("Log.txt", "Test: Vec3Cls => res += ManualAdd: ");
  212.         res3 = Vec3Cls.one;
  213.         watch.Reset();
  214.         watch.Start();
  215.         for (int i = 0; i < sampleCount; i++)
  216.         {
  217.             res3.x += Vec3Cls.one.x;
  218.             res3.y += Vec3Cls.one.y;
  219.             res3.z += Vec3Cls.one.z;
  220.         }
  221.         watch.Stop();
  222.         File.AppendAllText("Log.txt", "" + watch.ElapsedMilliseconds + " ms\r\n");
  223.  
  224.         File.AppendAllText("Log.txt", "Test: Vec3Cls => res *= x: ");
  225.         res3 = Vec3Cls.one;
  226.         watch.Reset();
  227.         watch.Start();
  228.         for (int i = 0; i < sampleCount; i++)
  229.         {
  230.             res3 *= x;
  231.         }
  232.         watch.Stop();
  233.         File.AppendAllText("Log.txt", "" + watch.ElapsedMilliseconds + " ms\r\n");
  234.  
  235.         File.AppendAllText("Log.txt", "Test: Vec3Cls => res.Mul(x): ");
  236.         res3 = Vec3Cls.one;
  237.         watch.Reset();
  238.         watch.Start();
  239.         for (int i = 0; i < sampleCount; i++)
  240.         {
  241.             res3.Mul(x);
  242.         }
  243.         watch.Stop();
  244.         File.AppendAllText("Log.txt", "" + watch.ElapsedMilliseconds + " ms\r\n");
  245.     }
  246. }
  247.  
  248. struct Vec3Str
  249. {
  250.     public static readonly Vec3Str one = new Vec3Str(1, 1, 1);
  251.     public static Vec3Str oneProp
  252.     {
  253.         get { return one; }
  254.     }
  255.  
  256.     public float x;
  257.     public float y;
  258.     public float z;
  259.  
  260.     public Vec3Str(float x, float y, float z)
  261.     {
  262.         this.x = x;
  263.         this.y = y;
  264.         this.z = z;
  265.     }
  266.  
  267.     public static Vec3Str operator+(Vec3Str a, Vec3Str b)
  268.     {
  269.         return new Vec3Str(a.x + b.x, a.y + b.y, a.z + b.z);
  270.     }
  271.  
  272.     public static Vec3Str operator -(Vec3Str a, Vec3Str b)
  273.     {
  274.         return new Vec3Str(a.x - b.x, a.y - b.y, a.z - b.z);
  275.     }
  276.  
  277.     public static Vec3Str operator *(Vec3Str a, float b)
  278.     {
  279.         return new Vec3Str(a.x * b, a.y * b, a.z * b);
  280.     }
  281.  
  282.     public void Add(Vec3Str a)
  283.     {
  284.         this.x += a.x;
  285.         this.y += a.y;
  286.         this.z += a.z;
  287.     }
  288.  
  289.     public void Mul(float a)
  290.     {
  291.         this.x *= a;
  292.         this.y *= a;
  293.         this.z *= a;
  294.     }
  295. }
  296.  
  297. class Vec3Cls
  298. {
  299.     public static readonly Vec3Cls one = new Vec3Cls(1, 1, 1);
  300.     public static Vec3Cls oneProp
  301.     {
  302.         get { return one; }
  303.     }
  304.  
  305.     public float x;
  306.     public float y;
  307.     public float z;
  308.  
  309.     public Vec3Cls(float x, float y, float z)
  310.     {
  311.         this.x = x;
  312.         this.y = y;
  313.         this.z = z;
  314.     }
  315.  
  316.     public static Vec3Cls operator +(Vec3Cls a, Vec3Cls b)
  317.     {
  318.         return new Vec3Cls(a.x + b.x, a.y + b.y, a.z + b.z);
  319.     }
  320.  
  321.     public static Vec3Cls operator -(Vec3Cls a, Vec3Cls b)
  322.     {
  323.         return new Vec3Cls(a.x - b.x, a.y - b.y, a.z - b.z);
  324.     }
  325.  
  326.     public static Vec3Cls operator *(Vec3Cls a, float b)
  327.     {
  328.         return new Vec3Cls(a.x * b, a.y * b, a.z * b);
  329.     }
  330.  
  331.     public void Add(Vec3Cls a)
  332.     {
  333.         this.x += a.x;
  334.         this.y += a.y;
  335.         this.z += a.z;
  336.     }
  337.  
  338.     public void Mul(float a)
  339.     {
  340.         this.x *= a;
  341.         this.y *= a;
  342.         this.z *= a;
  343.     }
  344. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement