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

Pages: 1-4041-8081-120121-

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.

Name: Anonymous 2012-01-16 12:18

>>34
Implement it in Java, it should be EASILY DEPLOYABLE AND SCALABLE IN CORPORATE ENVIRONMENTS

Name: Anonymous 2012-01-16 12:19

>>34
size / i will probably result in something bad when i == 0.

Name: Anonymous 2012-01-16 12:19

>>34
Are you tring to imply these two do the same thing?
size/i with i<size
huh?

Name: Anonymous 2012-01-16 12:20

Fuck you, dubs guy.

Name: Anonymous 2012-01-16 12:21

>>44
;_;

J-Jackson, forty-five, g-get... I c-can't do this~

Name: Anonymous 2012-01-16 12:26

>>34
IHBT

size/0 && size%0

Are both undefined.

Name: Anonymous 2012-01-16 12:41

>>37
Are you fucking dense or retarded? for (i = 0; i < X; ++i) and for (i = 0; i < X; i++) result in the same thing, I used the example with int var = ++i and int var = i++ to prove the point that i++ and ++i are two very different things and one of them (depending on the compiler/interpreter/whatever) may yield better performance than the other, unless the compiler figures it out.

Now get some basic reading comprehension and go back to /g/ where you belong.

Name: Anonymous 2012-01-16 12:42

>>37
Seriously, HOW FUCKING HARD IS IT TO READ?

Name: kodak_gallery_programmer !!kCq+A64Losi56ze 2012-01-16 12:55

>>48
Perhaps you should write some actual code instead of googling shit. Anyways, here is what I came up with...

public class Main {

    public static void main(String[] args) {
        int i = 0;

        for (i = 0; i < 5; i++) {

        }//end first for

        System.out.println("The value of i is " + i);

        i = 0;

        for (i = 0; i < 5; ++i) {

        }//end second for

        System.out.println("The value of i again is " + i);
    }

}

And the output is..

run:
The value of i is 5
The value of i again is 5
BUILD SUCCESSFUL (total time: 0 seconds)

Name: Anonymous 2012-01-16 12:55

>>34

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

The fuck is this?

1) First of all, division by zero in size / 0 and size % 0
2) Then, if size is 10, your indexes will be as follows:

10 0
5 0
3 1
2 2
2 0
1 4
1 3
1 2
1 1


You're not covering all the elements, and you are certainly going out of bounds if size = height * width.

Name: Anonymous 2012-01-16 12:57

>>49
Yes, they produce the same output you mental midget. That's not what is being discussed. Keep up or go back to /g/

Name: Anonymous 2012-01-16 12:59

>>49

i = 0;
for (i = 0; [...]

Why?

Name: Anonymous 2012-01-16 13:02

>>51
That reply was meant for >>37.

Name: Anonymous 2012-01-16 13:02

>>34-36,38-41
You all need to learn some basic maths. You nitpick about size / 0 when that snippet of code isn't even going to cover half of the elements in that array.

Name: Anonymous 2012-01-16 13:03

>>53
Okay then, sorry.

Name: kodak_gallery_programmer !!kCq+A64Losi56ze 2012-01-16 13:05

>>50
Again, I think you need to write some code instead of just googling shit. Oh wait, you don't have the mental capacity to code! Anyways, here is what I get when I run the loop in Java...

public class Main {

    public static void main(String[] args) {
        int value = 0;
        int size = 5;

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

run:
Exception in thread "main" java.lang.ArithmeticException: / by zero
        at Main.main(Main.java:10)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

Name: Anonymous 2012-01-16 13:07

>>56
Division by zero isn't the only problem there. (And why are you responding to me, I clearly pointed that out in >>50.

1) First of all, division by zero in size / 0 and size % 0

Name: Anonymous 2012-01-16 13:07

>>56
More to the point, look at the error messsage. Psst... the complaint is about / and not %.

Name: Anonymous 2012-01-16 13:07

>>54
Since it starts at 0
It's gonna crash x segfault at the start, there was no point in even trying to figure out what's gonna happen next.

Name: Anonymous 2012-01-16 13:08

>>56
What are you on about?
Exception in thread "main" java.lang.ArithmeticException: / by zero

That's EXACTLY WHAT I SAID:
>>50
1) First of all, division by zero in size / 0 and size % 0
division by zero

Name: Anonymous 2012-01-16 13:11

>>59
Division by zero is an error easy to make, but also easy to fix (comparable to a syntax error). Not understanding how division and modulo works, however, shows a fundamental lack of understanding.

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

>>60
Ughhh... I keep forgetting that % isn't the same as % in other programming languages.

Name: Anonymous 2012-01-16 13:12

>>49
They produce the same output in this case.
The pre-increment operator(++i), increments and returns (only one value stored)
The post-increment operator(i++), returns a value of i and holds the incremeneted value for itself(2 values in memory);

Name: kodak_gallery_programmer !!kCq+A64Losi56ze 2012-01-16 13:13

>>61
But according to the JSP, % isn't modulo.

Name: Anonymous 2012-01-16 13:13

>>62
wat

It's the same in most C-like languages.

Name: Anonymous 2012-01-16 13:14

>>64
HOLY SHIT % NOT MODULO OPERATOR,
stop the presses

Name: Anonymous 2012-01-16 13:15

>>64
I'm not following your aspie thought of train at all... Why are you calling me a mental midget and responding to my posts, when you point out the exact same things I just said in my posts.

Name: kodak_gallery_programmer !!kCq+A64Losi56ze 2012-01-16 13:15

>>65
No it isn't! Read the Java Standard. It isn't the same.

Name: Anonymous 2012-01-16 13:16

>>63
>The pre-increment operator(++i), increments and returns (only one value stored)
>The post-increment operator(i++), returns a value of i and holds the incremeneted value for itself(2 values in memory);
Unless the compiler notices that there are no assignments and decides to optimize it, thus the two statements are identical for this purpose.

Name: kodak_gallery_programmer !!kCq+A64Losi56ze 2012-01-16 13:17

>>66
It really isn't. And the fact that you think it is reflects the fact that you don't understand how crap languages like Java work.

Name: Anonymous 2012-01-16 13:17

>>68
Java Standard
JSP isn't Java.

Name: Anonymous 2012-01-16 13:18

>>70
JSP == Java
Nope, try again. Go back to the image boards.

Name: Anonymous 2012-01-16 13:18

>>67
I'm confusing you with the moron toilet scrubber.

Name: Anonymous 2012-01-16 13:18

>>73
You don't read before you respond. How surprising...

Name: Anonymous 2012-01-16 13:19

>>72
Go learn what % is you mental midget.

Name: Anonymous 2012-01-16 13:21

>>75
What the fuck is going on in this thread? % is modulo, even in JSP. <% however is not, but then again, that's not valid syntax in any C-like language.

Kodak either confirmed for troll or a complete retard.

Name: Anonymous 2012-01-16 13:22

>>74
For reasons that elude me, my coworkers insist on referring to the Java Language Specification as JSP. Anyways, the language specification clearly states that % isn't modulo.

Name: Anonymous 2012-01-16 13:22

>>76
or both

Name: Anonymous 2012-01-16 13:22

>>76
No it isn't you moron.

Name: Anonymous 2012-01-16 13:23

>>76
No, the Java Language Specification clearly states that % isn't  modulo.

Name: Anonymous 2012-01-16 13:25

Aspies fucking TOLD.

http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.17.3

Remainder operator

In computing, the modulo operation finds the remainder of division of one number by another.

Name: Anonymous 2012-01-16 13:26

Guys
http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.17.3
it's the remainder operator,
What we understand by modulo division.

Name: Anonymous 2012-01-16 13:27

The binary % operator is said to yield the remainder of its operands from an implied division; the left-hand operand is the dividend and the right-hand operand is the divisor.

Name: kodak_gallery_programmer !!kCq+A64Losi56ze 2012-01-16 13:27

And this is why I work as a programmer for a living -).

Name: Anonymous 2012-01-16 13:28

Java
And this thread went quickly to shit. Just like GC.

Name: Anonymous 2012-01-16 13:29

Name: Anonymous 2012-01-16 13:29

>>84
How does failing to understand that the remainder operation is same as modulo division help?

Name: Anonymous 2012-01-16 13:30

>>84
And this is why I work as a programmer for a living
Confirmed for Java code monkey. Enjoying being a toilet scrubber?

Name: Anonymous 2012-01-16 13:31

>>87
Uhh... because one is divisible by zero and the other isn't?

Name: Anonymous 2012-01-16 13:32

>>88
You're just mad because you screwed the pooch.

Name: Anonymous 2012-01-16 13:37

>>89
No

Name: kodak_gallery_programmer !!kCq+A64Losi56ze 2012-01-16 13:39

>>87
Look at the examples given in the document.

Name: Anonymous 2012-01-16 13:39

>>89
Uhh... because one is divisible by zero and the other isn't?
Nope. Neither is divisible by zero.

$ cat test.java

class test {
    public static void main(String[] args) {
        int herp = 3, derp = 0;
        System.out.println(herp % derp);
    }
}

$ java test
Exception in thread "main" java.lang.ArithmeticException: / by zero
        at test.main(test.java:4)


n mod 1 is always 0; n mod 0 is undefined, possibly resulting in a "Division by zero" error in computer programming languages

Name: Anonymous 2012-01-16 13:41

>>89
No. Just shut up, retard.

Name: Anonymous 2012-01-16 13:42

>>92
What examples? Also >>93

Fucking told.

Name: kodak_gallery_programmer !!kCq+A64Losi56ze 2012-01-16 13:45

>>95
http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.17.3

Has the following:

5%3 produces 2                 (note that 5/3 produces 1)
5%(-3) produces 2               (note that 5/(-3) produces -1)
(-5)%3 produces -2              (note that (-5)/3 produces -1)
(-5)%(-3) produces -2           (note that (-5)/(-3) produces 1)

Name: Anonymous 2012-01-16 13:45

>>96
divisible by zero
I see none.

Name: Anonymous 2012-01-16 13:47

>>97
If the dividend is an infinity, or the divisor is a zero, or both, the result is NaN.
It's still not divisble by zero FYI, but atleast defined.

Name: Anonymous 2012-01-16 13:48

>>98
Oh, and NaN just means that it's an undefined number.

Name: kodak_gallery_programmer !!kCq+A64Losi56ze 2012-01-16 13:48

>>97
It was in response to the person who asked

>How does failing to understand that the remainder operation is same as modulo division help?

Name: Anonymous 2012-01-16 13:48

>>98'
>cool story bro

>>93

Name: Anonymous 2012-01-16 13:49

>>99
it's defined, it's undefined
ITT: Autism

Name: Anonymous 2012-01-16 13:51

>>101
the part with 0 is only true for floating point numbers

Name: Anonymous 2012-01-16 13:53

>>100
ffs learn to respond correctly

Name: Anonymous 2012-01-16 13:55

ITT:
it's well-defined, it's defined as undefined

Name: Anonymous 2012-01-16 13:57

>>105
Welcome to /prog/
If this surprises you, you must be new here.
Goodbye and go back to /g/ when it's back up.

Name: Anonymous 2012-01-16 14:00

>>106
I'm still failing to see how division by zero is allowed in Java. All I see is the tripfag (you?) claim it

Name: Anonymous 2012-01-16 14:04

>>106
Actually, we'd all prefer it if you went away and never came back.

Name: kodak_gallery_programmer !!kCq+A64Losi56ze 2012-01-16 14:05

>>107
Geeze, this would be easier if you just wrote some code...

public class Main {

    public static void main(String[] args) {
        int x = 5;
        int y = 0;
        int z = 0;

        try {
            z = x/y;
        } catch (Exception e) {
            System.out.println("Can't divide by zero");
        }
       
    }
}


And the result...

run:
Can't divide by zero
BUILD SUCCESSFUL (total time: 0 seconds)

Name: Anonymous 2012-01-16 14:06

>>109
Can't divide by zero
That's what I said... wat.. No one can really follow your aspie train of thought.

Name: Anonymous 2012-01-16 14:07

>>107
And just in case you have me filtered...

Geeze, this would be easier if you just wrote some code...

public class Main {

    public static void main(String[] args) {
        int x = 5;
        int y = 0;
        int z = 0;

        try {
            z = x/y;
        } catch (Exception e) {
            System.out.println("Can't divide by zero");
        }
       
    }
}


And the result...

run:
Can't divide by zero
BUILD SUCCESSFUL (total time: 0 seconds)

Name: Anonymous 2012-01-16 14:08

>>110
I was clearly responding to the toilet scrubber who asked...

>I'm still failing to see how division by zero is allowed in Java. All I see is the tripfag (you?) claim it

Name: Anonymous 2012-01-16 14:10

>>65
% in Java means something different than % in C and C++.

Name: kodak_gallery_programmer !!kCq+A64Losi56ze 2012-01-16 14:10

The point is that it is possible to divide by zero in java.

Name: Anonymous 2012-01-16 14:10

>>111'
>hurr durr


[code]
#include <stdio.h>
#include <setjmp.h>

jmp_buf p;

void sighdlr() {
    longjmp(p, 1);
}

int main() {
    int a = 3, b = 0, c;
    signal(SIGFPE, sighdlr);
    if (!setjmp(p)) {
        c = a / b;
    } else {
        puts("Can't divide by zero");
    }
    return 0;
}

Name: Anonymous 2012-01-16 14:13

>>114
it is possible to divide by in C in the same matter as in Java,
using IEEE floatint point numbers,
as fact C does not support % operator for FPN Java does.

Name: Anonymous 2012-01-16 14:13

>>114
see >>115

Name: kodak_gallery_programmer !!kCq+A64Losi56ze 2012-01-16 14:16

>>115
Yeah, but doing something like that isn't always reliable. Eer... an actual C implementation that emulates someting like try/catch is long and complicated. Seriously. I've seen several talented programmers fuck it up.

Name: Anonymous 2012-01-16 14:18

>>118
I've seen several talented programmers fuck it up.
Your standards are pretty low.

Name: Anonymous 2012-01-16 14:29

>>1
Assuming this is C or something along those lines, arrays are stored in row-major order. This means that the former accesses elements sequentially with respect to their location in memory, while the latter has doesn't, so there is no sequential locality. This can, and in practice will, impact performance. (The only exception is if update() is inlined and all it does is write to Xs, in which case caching wouldn't be attempted.) I don't think the compiler could make any optimizations, especially with more non-trivial cases.

>>13
What difference does it make whether it's interpreted or compiled?

>>15
That is the reason, actually. Unless the structure, or at least some of the rows, are small enough to fit into a cache line, or if there is no attempt to cache the data (see above), the second example won't benefit from caching.

>>17
Have you ever done anything beyond that :o

Name: Anonymous 2012-01-16 14:36

>>119
It's easy to make something really basic, but making an exception system in C that is nestable, scalable, safe and thread safe is difficult.

Name: Anonymous 2012-01-16 14:44

>>121
I used to make an exception system in C that is nestable, scalable, safe and thread safe, but then I took an arrow in the knee.

Name: Anonymous 2012-01-16 14:46

>>122
Bro, I think it's time for you to go.

Name: Anonymous 2012-01-16 14:47

(USER WAS BANNED FOR THIS POST)

Name: Anonymous 2012-01-16 14:48

>>124
The closest you're going to get I think is something like
#(USER WAS BANNED FOR THIS POST)

Name: Anonymous 2012-01-16 14:49

>>121
You don't belong here.

Name: Anonymous 2012-01-16 14:51

>>126
You're free to post an implementation, the ones I've seen from /prog/ have not been anywhere near thread-safe or infinitely nestable.

Name: Anonymous 2012-01-16 15:19

And when you're on it make sure it's signal safe as well.

Name: Anonymous 2012-01-16 20:11

>>49

i = 0;
i = 0;
i = 0;
i = 0;
i = 0;
i = 0;

god you're fucking retarded.

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