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

Another Elegence vs. Readability

Name: Anonymous 2011-05-31 3:57

/* Problem: We want [4][4], [4][5], [5][4], and [5][5] to all be four, and the rest to be generated between one and three */

/* snippet the first */
int arrays[10][10];
for(int i = 0; i < 10; i++){
    for(int j = 0; j < 10; j++){
        arrays[i][j] = /* randomly generate a number between one and three */;
    }
}
for(int i = 4; i < 6; i++){
    for(int j = 4; j < 6; j++){
        arrays[i][j] = 4;
    }
}

/* snippet the second */
int arrays[10][10];
for(int i = 0; i < 10; i++){
    for(int j = 0; j < 10; j++){
        if(/*  */){
            arrays[i][j] = 4;
        } else {
            arrays[i][j] = /* randomly generate a number between one and three */;
        }
    }
}


Which snippet would you use?
Excuse the pseudo-ish code.

Name: Anonymous 2011-05-31 4:10

I prefer the first. Would rather do 4 extra assignments than 200 unnecessary comparisons.

Name: Anonymous 2011-05-31 4:10

First one is much faster, so obviously that.

Name: Anonymous 2011-05-31 4:11

>>2
Well, more than just 4 assignments, but still much fewer than *100 comparisons.

Name: Anonymous 2011-05-31 5:11

>>1
I can't tell which one is the elegant one.

Name: Anonymous 2011-05-31 5:19

I might consider memcpy()ing the 4's for extra speed.

Name: Anonymous 2011-05-31 5:27

>>3,6
VROOM VROOM

Name: Anonymous 2011-05-31 6:41

>>1
I don't see any vector ops.  Why do you hate your cpu?

Name: Anonymous 2011-05-31 6:44

>>8
That's C, that's portable assembler!!! Why would you use something non-portable! As fast as C VROOM VROOM!

Name: Anonymous 2011-05-31 17:38

>>2
How do you get 200 unnecessary comparisons out of that piece of code?

Name: Anonymous 2011-05-31 18:05

>>10
I see 400 comparisons being made.

Name: Anonymous 2011-05-31 18:16


int *c = ((int*)arrays)+100;
while( c --> arrays) *c = rand()%3 + 1;
c[45] = c[46] = c[55]  = c[56] = 4;

is the best way

Name: Anonymous 2011-05-31 18:19

>>12
Nice use of the goes-to operator.

Name: Anonymous 2011-05-31 18:24

>>12
Enjoy your slow-as-fuck remainder operator.

Name: Anonymous 2011-05-31 18:33

while( >>12 --> fucktard ) world_is_in_order();

Name: Anonymous 2011-05-31 18:39

>>14
so, what do you suggest?

Name: Anonymous 2011-05-31 18:41

>>15
fuck you faggot

Name: Anonymous 2011-05-31 18:51

>>16
(unsigned)rand()%3

Name: Anonymous 2011-06-03 15:11

>>16
you could just bitwise and it by 3. it obviously won't be the same, but it will have the same effect.

Name: Anonymous 2011-06-03 15:15

rand() sucks. I once used it to fill a square of monochrome pixels and there was an outstanding Moire pattern. Since then I've always used Mersenne Twister.

Name: Anonymous 2011-06-03 15:26

>>19
It won't be the same at all, because you'd be picking a number between 0 and 3 inclusively, whereas rand()%3 is between 0 and 2.

Name: Anonymous 2011-06-03 15:58

>>21
That's assuming the rand() follows a normally probability curve. Often times though it doesn't. Hence some numbers appear more often than others.

Name: Anonymous 2011-06-03 16:25

>>22
No shit, that's because it's random. Over enough invocations, you will find that rand()%3 has a range one less than rand()&3.

Name: Anonymous 2011-06-03 16:29

>>23
No, random() over time will have a lopsided gausssian curve.

Name: HugePenis 2011-06-03 16:35

Name: Anonymous 2011-06-03 16:41

>>24,25
my whole life was a lie

Name: Anonymous 2011-06-03 18:34

I want to touch and folder your delicate anus

Name: Anonymous 2011-06-03 22:34

>>25
Out of date. Things have changed since 2002. OTOH, all rand()%n is bad where n isn't a power of 2 (similar story for &).

Name: Anonymous 2011-06-04 16:07

The C-style comments (/* ... */) are often preferred over the C++-style comments (// ...\n) because they do not introduce ``significant whitespaces'', and they're claimed to be more elegant and readable. Now, observe:

int a = 10, b = 0, c = 30;
int *p = b;

printf("%d\n", a/*p);
printf("%d\n", /*c*/<<2);


What will this code do?

Name: Anonymous 2011-06-04 16:12

>>29
I DONT GIVE A FUCK

Name: Anonymous 2011-06-04 16:15

>>29
significant whitespaces
AUTISM

Name: Anonymous 2011-06-04 17:16

>>29
Fail code review.

Name: Anonymous 2011-06-06 11:30


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