0

I'm new to C and our professor has given us a performance assignment where we have to manipulate some 2d arrays. I'm trying to figure out how to correctly move a value between two arrays. I believe using the *(array*i+j) might help speed things up but I cannot get it to compile. I am aware that array[i][j] is usually acceptable but I need to get this to be as fast as possible. A problem line would look something like

out[x] = *( *(in+i) + j);

The error I'm getting is "incompatible types when assigning to type int[10000] from type int. should I be making pointers for out and in? I am not allowed to change the implementation which is

define N 10000

/* The input matrix */

long in[N][N];

/* The output matrix */
long out[N][N];

I am certain the answer is depressingly obvious but none of my changes have worked. I just want to change the value at out[x] or out+x.

Brian Tracy
  • 6,801
  • 2
  • 33
  • 48
Xun
  • 43
  • 1
  • 5
  • `out` is a 2D array, what do you mean by `out[x]` ? – brokenfoot Apr 06 '14 at 21:09
  • `a[x]` and `*(a+x)` are exactly equivalent, there is no performance gain from using one or the other. – M.M Apr 06 '14 at 21:16
  • From what I've found out, memory for 2d arrays are stored linearly so using *(out + x) would be an acceptable way of incrementing through the array? I've been looking at some algorithms and some seem to use this format, ex) http://stackoverflow.com/questions/16737298/what-is-the-fastest-way-to-transpose-a-matrix-in-c – Xun Apr 06 '14 at 21:46
  • `*(out+x)` and `out[x]` are exactly the same, it is alternative syntax for the same thing. They both designate an array[100] (not a `long`). – M.M Apr 06 '14 at 21:59
  • In the page you link to, `A` is a 1-D array. You can pretend your array is a 1-D array by doing `((long *)&in)`, and then write `((long *)&in)[i * N + j]`. However you just make your code harder to read, for no performance gain. Performance != obfuscation. – M.M Apr 06 '14 at 22:01
  • Ah, I was assuming since it was transposing a matrix the output should be 2D as well. If it does not speed up my program I'd like to at least learn this properly lol – Xun Apr 06 '14 at 22:18

1 Answers1

1

Try this

out[column][row] = *( *(in+i) + j);

You were forgetting to index the 2nd dismension of the array you were assgining to.

user3476738
  • 176
  • 2
  • 12