Guest User

Jagged arrays are faster?

a guest
Jan 13th, 2018
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.89 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Test : MonoBehaviour {
  6.  
  7.     // Use this for initialization
  8.     void Start() {
  9.         StartCoroutine(RunTest());
  10.     }
  11.  
  12.     private IEnumerator RunTest() {
  13.  
  14.         yield return new WaitForSeconds(0.5f);
  15.  
  16.         var size = 10000;
  17.         var stopwatch = new System.Diagnostics.Stopwatch();
  18.        
  19.         var a = new int[size, size];
  20.         var b = new int[size][];
  21.         for(int i = 0; i < size; i++) {
  22.             b[i] = new int[size];
  23.         }
  24.  
  25.         stopwatch.Start();
  26.         for(int i = 0; i < size; i++) {
  27.             for(int j = 0; j < size; j++) {
  28.                 b[i][j] = 5;
  29.             }
  30.         }
  31.         stopwatch.Stop();
  32.  
  33.         Debug.Log(stopwatch.ElapsedMilliseconds);
  34.         stopwatch.Reset();
  35.  
  36.         stopwatch.Start();
  37.         for(int i = 0; i < size; i++) {
  38.             for(int j = 0; j < size; j++) {
  39.                 a[i, j] = 5;
  40.             }
  41.         }
  42.         stopwatch.Stop();
  43.  
  44.         Debug.Log(stopwatch.ElapsedMilliseconds);
  45.  
  46.  
  47.     }
  48.  
  49. }
Advertisement
Add Comment
Please, Sign In to add comment