Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Which loop is faster?

Name: Anonymous 2012-01-16 8:59


for (I = 0; I < N; I++)
  for (J = 0; J < M; J++)
    update(Xs[I][J]);

or

for (I = 0; I < N; I++)
  for (J = 0; J < M; J++)
    update(Xs[J][I]);

???

Name: Anonymous 2012-01-16 9:03

>>1
First one, due to caching.

Name: Anonymous 2012-01-16 9:14

>>2

GRETAT!! ive BEN doing it right BY ACsiDXENT! lazl this tieeM!

Name: Anonymous 2012-01-16 9:20

The first one for Row-major order.
Second for Column-major order.

Name: Anonymous 2012-01-16 9:21

>>1
I++, not ++i.'
>confirmed for java developer

Name: Anonymous 2012-01-16 9:22

>>5
you're a fucking faggot. you probably don't even know the difference between i++ and ++i.

Name: Anonymous 2012-01-16 9:23

>>6
fuck you fagstorm

Name: Anonymous 2012-01-16 9:31

>>7
u got told, kid

Name: Anonymous 2012-01-16 9:35

>>5-8

back to >>>/b/

Name: Anonymous 2012-01-16 9:40

>>6
Not that it matters when it is a separate statement, such as above, the compiler (assuming you use one) should be able to optimize it. But at least for IA32/IA64, int var = ++i; is fewer instructions than int var = i++;

Deal with it, kid

Name: Anonymous 2012-01-16 9:47

>>10 These statements do different things, fagdrizzle.

Name: Anonymous 2012-01-16 9:49

>>11
That is exactly the point. i++ and ++i are different things, but OP is using it as they are equal even though they result in the same thing.

Name: Anonymous 2012-01-16 9:53

>>11-12'
>inb4 they evaluate to the exact same thing on my computer

What if OP is using a C interpreter and not your bloated GCC.

Name: Anonymous 2012-01-16 9:59

>>10
Even though the following:

int var = ++i;

is a valid C construct. Some compilers will reject it.

Name: Anonymous 2012-01-16 10:00

>>2
That's not the correct reason. Do you wanna try again Walmart boy?

Name: Anonymous 2012-01-16 10:24

>>1
>>15
This question cannot be answered. Not enough details.
Assume no OS/no Virtual memory, small N & M.
what now?

Name: Anonymous 2012-01-16 10:30

>>16
Have you ever taken any computer science classes at a 4 year university?

Name: Anonymous 2012-01-16 10:32

>>16
Also, I've seen similiar questions like this on the Computer Science portion of the GRE.

Name: Anonymous 2012-01-16 10:44

>>17
Naw, I just google/wiki random stuff when not playing Touhou.
I also think that this question cannot be answered untill a complete definition of Xs[I][J] is posted.

For example in our beloved sepples you could probably make overload indexing operators and this would return a constant.

Name: Anonymous 2012-01-16 10:49

>>19
Even if a complete definition of Xs[I][J} is given, the reason why this loop would perform faster or slower relative to the other loop is still incorrect.

Name: Anonymous 2012-01-16 10:53

>>20
My point is that, under some assumptions both of these loops perform at the same speed.

Name: Anonymous 2012-01-16 10:54

Which one is faster is implementation/syste, dependent but on the implementation you're likely using the first one will be faster.

Name: Anonymous 2012-01-16 10:56

Thats which uses sequential access in linear order will be cached. That which does not and skip rows will suffer unpredictable uncached access times(since some will be cached) but if the data is small enough it would not be felt since its cached entirely at start.

Name: Anonymous 2012-01-16 11:11

>>21
And that is still incorrect. Now I see why you work some shit hourly job instead of working a programmer.

Name: Anonymous 2012-01-16 11:20

some shit hourly job instead of working a programmer.
What's the difference?

Name: kodak_gallery_programmer !!kCq+A64Losi56ze 2012-01-16 11:33

>>25
Eeer... that if one runs in linear time, the other will run in quadratric time?

Name: Anonymous 2012-01-16 11:36

>>26
Toilet scrubing for 8 hours is 8 hours.
Java programming for 8 hours is 64 hours.
Wat.
I hope you get paid per hour.

Name: Anonymous 2012-01-16 11:39

>>27
You're just mad because no one wants to hire a mental midget as a programmer.

Name: Anonymous 2012-01-16 11:41

>>23
but if the data is small enough it would not be felt since its cached entirely at start
This

Name: Anonymous 2012-01-16 11:41

>>28
no one
Not true. The world needs Java programmers too.

Name: Anonymous 2012-01-16 11:46

>>28
I sure am mad. As a touhou playing NEET I do not require silly earthly prospects known to the lesser people as `jobs'.

Name: Anonymous 2012-01-16 11:48

>>14
Some compilers will reject it
Not due to the ++i methinks, but rather that some compilers *cough*micro$hit*cough* requires variables do be declared first in functions.

Name: Anonymous 2012-01-16 11:57

>>32
Winner winner chicken dinner.

Name: Anonymous 2012-01-16 12:08

Another question.

Which one is faster?

for (int i = 0; i < height; i++}
    for (int j = 0; j < width; j++)
         array[i][j] = value;

or

for (int i = 0; i < size; i++}
    array[size / i][size % i] = value;

Name: Anonymous 2012-01-16 12:11

>>34
First one should be faster, second one should use less memory.

Name: Anonymous 2012-01-16 12:13

>>35
You really need to take a class that covers stuff like runtime analysis.

Name: Anonymous 2012-01-16 12:14

>>12,13
Listen here, you two fagprecipitations.

If i == 10, then int var = ++i; results in var == 11, while int var = i++; results in var == 10. These two do not result in the same thing.

NOW GET THE FUCK OUT.

Name: Anonymous 2012-01-16 12:15

>>35
Also, "less  memory" is ambigious you homo. Dunno about you, but at least to me, there seems to be a difference in memmory ussage if the array does a "write through" instead of a "write back".

Name: Anonymous 2012-01-16 12:15

>>36
Which one?

Name: Anonymous 2012-01-16 12:18

>>39
I think you need to go reexamine your future. Again, programming doesn't seem to be your thing.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List