x


What is the difference between: int[][] arr; and int[,] arr;

Is there anydifference between to two?

Also if there is, which is preferred i.e. is one faster then the other?

thanks Alex

more ▼

asked Nov 26 '10 at 01:06 PM

Alexandria gravatar image

Alexandria
48 3 3 3

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

int[][] is an array of arrays, where array[0] could be 42 items long, and array[1] could have just 2 as each individual inner array can be created independently

int[,] has a predefined width and height (so you don't need to check bounds for each column), and is internally just one array, so is generally going to be slightly faster

Both have their uses though, so you may still want to try both to see which fits your use case better

more ▼

answered Nov 26 '10 at 01:22 PM

Mike 3 gravatar image

Mike 3
30.5k 10 65 253

(comments are locked)
10|3000 characters needed characters left

cool thanks, well it's for a procedural sphere terrain set up in the editor. Performance will not be that important here (yet). Hehe the progressbar slows things down when creating 65000 verts

For grids the arr[,] would be preferred then. However I always use arr[ y * depth + x ] wich propably is a bit faster too. Not sure there

Alex

more ▼

answered Nov 26 '10 at 01:31 PM

Alexandria gravatar image

Alexandria
48 3 3 3

arr[,] should be the same speed as arr[y*depth + x], it just maps arr[x,y] to internal_arr[y*width + x] internally

Nov 26 '10 at 01:43 PM Mike 3

No, at the CLR level, using a multidimensional array is not at all the same as using a vector (single-dimensional array). The runtime has special optimized IL instructions for creating and handling vectors, whereas for multidimensional arrays, method calls must be used, so all the benefit of locality is lost.

Feb 28 '12 at 08:51 PM ti89TProgrammer
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x4165
x1363

asked: Nov 26 '10 at 01:06 PM

Seen: 1391 times

Last Updated: Feb 28 '12 at 08:51 PM