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

ANY OF U REDDY 2 CHALLENGE ME YET?

Name: L. A. Calculus !!wKyoNUUHDOmjW7I 2013-06-20 0:33

CARN

Name: L. A. Calculus !!wKyoNUUHDOmjW7I 2013-06-20 12:57

>>34
ACCEPTED.

>>38
The best solution wins.
MAYBE IN UR SHITTY GAME, BUT ALL I'M PARTICIPATING IN IS DA FUCKING CHALLENGE. I'VE POSTED MY RULES EARLIER; IF IT IMPRESSES ME I'M OFF DESE FUCKING BOARDS. NO WINNERS, NO LOSERS.

>>39
MORE BULLSHIT.

>>40
SHUT UP U FUCKING MATH BOY.

ANYWAY, I'M DONE AND UPLOADED TO ideone. TIME FOR U TO POST UR FUCKIN CODE.

Name: Anonymous 2013-06-20 13:05

>>21,25,26
Alright, faggots, you've angered an EXPERT PROGRAMMER.

numelems.h:
#define NUM_ELEMS 10000000

list.c
#include <stdio.h>
#include <stdlib.h>
#include "numelems.h"

typedef struct node {
    int n;
    struct node *next;
} node;

node *new(int n) {
    node *l;

    l = malloc(sizeof(*l));
    l->n = n;
    l->next = NULL;
    return l;
}

int main() {
    int i;
    unsigned long total;
    node *head;
    node *tail;

    head = NULL;
    for (i = 0; i < NUM_ELEMS; ++i) {
        if (head) {
            tail = tail->next = new(i);
        } else {
            tail = head = new(i);
        }
    }
    total = 0;
    tail = head;
    while (tail) {
        total += tail->n;
        tail = tail->next;
    }
    printf("%lu\n", total);
    return 0;
}


array.c
#include <stdio.h>
#include <stdlib.h>
#include "numelems.h"

int main() {
    int *a;
    int i;
    size_t n;
    unsigned long total;

    n = 1;
    a = malloc(n * sizeof(*a));
    for (i = 0; i < NUM_ELEMS; ++i) {
        while (n <= i) {
            n *= 2;
            a = realloc(a, n * sizeof(*a));
        }
        a[i] = i;
    }
    while (--i)
        total += a[i];
    printf("%lu\n", total);
    return 0;
}


I made the initialized the array with a size of 1, so that it needs to be reallocated as often as possible, just to demonstrate how much faster it is regardless.

$ cc -o array array.c
$ cc -o list list.c
$ time ./list
49999995000000
./list  0.71s user 0.15s system 99% cpu 0.869 total
$ time ./list
49999995000000
./list  0.72s user 0.15s system 99% cpu 0.878 total
$ time ./list
49999995000000
./list  0.71s user 0.15s system 99% cpu 0.870 total
$ time ./list
49999995000000
./list  0.71s user 0.15s system 99% cpu 0.873 total
$ time ./list                                                                                                                                 49999995000000
./list  0.71s user 0.15s system 99% cpu 0.871 total
$ time ./array
49999995000000
./array  0.08s user 0.04s system 95% cpu 0.133 total
$ time ./array               
49999995000000
./array  0.08s user 0.04s system 97% cpu 0.131 total
$ time ./array                                                                                                                                49999995000000
./array  0.08s user 0.04s system 94% cpu 0.130 total
$ time ./array                                                                                                                                49999995000000
./array  0.08s user 0.04s system 95% cpu 0.129 total
$ time ./array                                                                                                                                49999995000000
./array  0.08s user 0.04s system 97% cpu 0.129 total


Feel free to do your own benchmarks, listboys.

Name: Anonymous 2013-06-20 13:08

>>42
Also, you'll notice that array.c looks like C, whereas list.c looks more like Javashit.

Name: Anonymous 2013-06-20 13:16

Shit insertion speeds.

Name: !!YtwzkeoLMFQUgZv 2013-06-20 13:35

>>41

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

int main(void) {
  char buf[BUFSIZ] = {0};
  size_t n, i, j, k;
  struct { size_t size, pos; } *m;

  fread(buf, 1, BUFSIZ - 1, stdin);
  n = strlen(buf);
  for(i = j = 0; j < n;
      j += strcspn(buf + j, "\n") + 1, i++);
  assert(i && (m = malloc(sizeof *m * i)));
  for(j = 0; j < i; j++) {
    m[j].pos = (j > 0) ? m[j-1].pos + m[j-1].size + 1 : 0;
    m[j].size = strcspn(buf + m[j].pos, "\n");
  }
  for(j = n = 0; j < i; j++)
    n = (m[j].size > n) ? m[j].size : n;
  for(j = 0; j < n*i; j++) {
    k = j % i;
    if(k == 0 && j != 0) putchar('\n');
    if(m[k].size*i > j)
      putchar(buf[m[k].pos + j/i]);
    else
      putchar(' ');
  }
  putchar('\n');
  free(m);
  return 0;
}

Name: Anonymous 2013-06-20 13:40

>>42
I found this amusing:

array.py:
#!/usr/bin/env python
NUM_ELEMS = 10000000
print sum(range(NUM_ELEMS))


$ time ./array.py
49999995000000
./array.py  0.76s user 0.36s system 99% cpu 1.126 total
$ time ./list
49999995000000
./list  0.71s user 0.15s system 99% cpu 0.860 total
$ time ./array
49999995000000
./array  0.08s user 0.04s system 97% cpu 0.124 total


The linked list is only slightly faster than the equivalent Python code, whereas the dynamic array is almost 10x faster.

Name: Anonymous 2013-06-20 13:52

It's not equivalent Python code because Python "lists" are really dynamic arrays.

Name: Anonymous 2013-06-20 13:55

>>47
They're equivalent in the sense that they do the same thing, retoid.

Name: Anonymous 2013-06-20 13:58

le optmized: printf("49999995000000")

Name: Anonymous 2013-06-20 13:58

>>48
Two pieces of code can do the same thing and not be equivalent. For instance, one piece may be more optimized than the other.

Name: Anonymous 2013-06-20 14:07

>>50
``Equivalent'' means that ==input==> [BLACK BOX 1] ==output==> == <==output== [BLACK BOX 2] <==input==

Name: Anonymous 2013-06-20 14:09

>>51
No, what you described is "do the same thing". If one of them modifies some global state and the other doesn't, they're not equivalent.

Name: Anonymous 2013-06-20 14:11

>>52

. . . . .. . . . . . . . . . . ,.-‘”. . . . . . . . . .``~.,
. . . . . . . .. . . . . .,.-”. . . . . . . . . . . . . . . . . .“-.,
. . . . .. . . . . . ..,/. . . . . . . . . . . . . . . . . . . . . . . ”:,
. . . . . . . .. .,?. . . . . . . . . . . . . . . . . . . . . . . . . . .\,
. . . . . . . . . /. . . . . . . . . . . . . . . . . . . . . . . . . . . . ,}
. . . . . . . . ./. . . . . . . . . . . . . . . . . . . . . . . . . . ,:`^`.}
. . . . . . . ./. . . . . . . . . . . . . . . . . . . . . . . . . ,:”. . . ./
. . . . . . .?. . . __. . . . . . . . . . . . . . . . . . . . :`. . . ./
. . . . . . . /__.(. . .“~-,_. . . . . . . . . . . . . . ,:`. . . .. ./
. . . . . . /(_. . ”~,_. . . ..“~,_. . . . . . . . . .,:`. . . . _/
. . . .. .{.._$;_. . .”=,_. . . .“-,_. . . ,.-~-,}, .~”; /. .. .}
. . .. . .((. . .*~_. . . .”=-._. . .“;,,./`. . /” . . . ./. .. ../
. . . .. . .\`~,. . ..“~.,. . . . . . . . . ..`. . .}. . . . . . ../
. . . . . .(. ..`=-,,. . . .`. . . . . . . . . . . ..(. . . ;_,,-”
. . . . . ../.`~,. . ..`-.. . . . . . . . . . . . . . ..\. . /\
. . . . . . \`~.*-,. . . . . . . . . . . . . . . . . ..|,./.....\,__
,,_. . . . . }.>-._\. . . . . . . . . . . . . . . . . .|. . . . . . ..`=~-,
. .. `=~-,_\_. . . `\,. . . . . . . . . . . . . . . . .\
. . . . . . . . . .`=~-,,.\,. . . . . . . . . . . . . . . .\
. . . . . . . . . . . . . . . . `:,, . . . . . . . . . . . . . `\. . . . . . ..__
. . . . . . . . . . . . . . . . . . .`=-,. . . . . . . . . .,%`>--==``
. . . . . . . . . . . . . . . . . . . . _\. . . . . ._,-%. . . ..`

Name: Anonymous 2013-06-20 14:13

>>53
Yep, kid, it's real life: not all code that "does the same thing" is equivalent. And you will understand that once you have enough experience catchings bugs caused by seemingly innocent changes to code.

Name: Anonymous 2013-06-20 14:15

>>52
To clarify my picard facepalm: In my little world, global states do not exist. It's all de bruijn indices y0.

Name: Anonymous 2013-06-20 14:16

>>54
Okay. Can I now go to my room and play with my TI? Please?

Name: Anonymous 2013-06-20 14:18

>>54
real life
VB is real life. PHP is real life. Java is real life. Your point being? FML I code under my bedsheets with my raspberry.

Name: Anonymous 2013-06-20 14:19

>>42
Let's not forget that in a real program, we'll want to cleanup after ourselves. With the dynamic array, this is as simple as one free

array.c:
#include <stdio.h>
#include <stdlib.h>
#include "numelems.h"

int main() {
    int *a;
    int i;
    size_t n;
    unsigned long total;

    n = 1;
    a = malloc(n * sizeof(*a));
    for (i = 0; i < NUM_ELEMS; ++i) {
        while (n <= i) {
            n *= 2;
            a = realloc(a, n * sizeof(*a));
        }
        a[i] = i;
    }
    while (--i)
        total += a[i];
    printf("%lu\n", total);
    free(a);
    return 0;
}


With the linked-list, of course, you have free every node.

list.c
#include <stdio.h>
#include <stdlib.h>
#include "numelems.h"

typedef struct node {
    int n;
    struct node *next;
} node;

node *new(int n) {
    node *l;

    l = malloc(sizeof(*l));
    l->n = n;
    l->next = NULL;
    return l;
}

int main() {
    int i;
    unsigned long total;
    node *head;
    node *tail;

    head = NULL;
    for (i = 0; i < NUM_ELEMS; ++i) {
        if (head) {
            tail = tail->next = new(i);
        } else {
            tail = head = new(i);
        }
    }
    total = 0;
    tail = head;
    while (tail) {
        total += tail->n;
        tail = tail->next;
    }
    printf("%lu\n", total);
    while (head) {
        tail = head->next;
        free(head);
        head = tail;
    }
    return 0;
}


And let's see how this scales:
time ./array
49999995000000
./array  0.08s user 0.05s system 97% cpu 0.134 total
$ time ./list
49999995000000
./list  1.96s user 0.18s system 99% cpu 2.141 total


OUCH! Two seconds? You mean 10000000 mallocs and frees isn't BLINDING FAST?! Who would have thought?

``B-b-but, muh O(n) traversal.''
-- Lambda A. ``List Boy'' Calculus and his fag buddy

List: O(n) traversal, O(n) allocation, O(n) deallocation, O(n) access.
Array O(n) traversal, O(sqrt n) allocation, O(1) deallocation, O(1) access.

List: non-primitive, requires struct, optional auxiliary functions, etc.
Array: native to C, just declare and use it, fast as FUCK.

Time to the Standard again, Lambda. Maybe work out some Project Euler problems until you understand how to write non-shitty, non-slow, non-bloated, non-standard code.

Name: Anonymous 2013-06-20 14:23

>>55
Go back to your little haskieworld, pussy.

Name: Anonymous 2013-06-20 14:39

Nice to see some actual coding in /prog/.
Good job, guises.

Name: Anonymous 2013-06-20 14:42

>>57
Don't see any contradictions. Any language that allows non-referentially-transparent everywhere is bound to have problems with "does what it's supposed to, but not quite equivalent" code. And any language that runs on machines with finite resources is bound to have them too. For instance, in Haskell there's a lot of referential transparency, but use a foldl instead of foldl', and you'll get a stack overflow.

Name: Anonymous 2013-06-20 14:47

>>61
/I don't write my code to run on machines; I write it to run as a concept. Finiteness of resources is not my problem, unless it is strictly mentioned in the specification/

In any language you'd like to have access to the disk or tape, and that alone is enough to throw away any short of referential transparency. As for haskell, I've never written anything in it. I'm a lisper.

Name: Anonymous 2013-06-20 14:49

>>62
I'm a lisper.
You should've mentioned that up front so I wouldn't waste my time talking to you, wacko.

Name: Anonymous 2013-06-20 16:23

>>63
says the YAHT haskell troll IHBT

Name: Anonymous 2013-06-20 18:14

Nice to see some actual PROGRAMMING (NOT ``CODING'') in /prog/.
Good job, guys (fuck off with your le epic meme ``guise'' bullshit).

Name: L. A. Calculus !!wKyoNUUHDOmjW7I 2013-06-20 23:11

>>45
I GOTTA SAY, DAT'S PRETTY IMPRESSIVE, THOUGH I THOUGHT IT WAS SUPPOSED TO WORK WITH INPUT OF ANY SIZE. OH WELL. IT'S STILL PRETTY INTERESTING -- I'LL REED INTO IT A BIT MORE.

HERE'S MINE:

TIMESTAMP: http://ideone.com/SnV26P

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

enum {
    SIZE = 8192
};

size_t count(const char *s, int c)
{
    size_t r;

    for (r = 0; *s; r += *s++ == (char) c)
        ;
    return r;
}

size_t max_strcspn(const char *s, int c)
{
    size_t max, tmp;

    for (max = tmp = 0; *s; s++) {
        if (*s != (char) c) {
            tmp++;
            continue;
        }
        if (tmp > max)
            max = tmp;
        tmp = 0;
    }
    return max;
}

char *fill(char *to, size_t longest, size_t lines, const char *from)
{
    char *save = to;
    size_t n;

    for (n = longest; lines; from++)
        if (*from == '\n') {
            memset(to, ' ', n);
            to += n;
            n = longest;
            lines--;
        } else {
            *to++ = *from;
            n--;
        }
    return save;
}

char *readall(FILE *iop)
{
    char buf[SIZE];
    size_t a = 0, n;
    char *r, *tmp;

    for (r = 0; (n = fread(buf, 1, SIZE, iop)) == SIZE; r = tmp) {
        if (!(tmp = realloc(r, a + SIZE))) {
            free(r);
            return 0;
        }
        memcpy(tmp + SIZE, buf, SIZE);
        a += SIZE;
    }
    if (!(tmp = realloc(r, a + n + 1))) {
        free(r);
        return 0;
    }
    memcpy(tmp + a, buf, n);
    tmp[a + n] = '\0';
    return tmp;
}

int main(void)
{
    size_t lines, longest;
    char *ptr, *s;

    if (!(s = readall(stdin))
     || !(ptr = malloc((lines = count(s, '\n')) *
               (longest = max_strcspn(s, '\n'))))) {
        free(s);
        return EXIT_FAILURE;
    }
    fill(ptr, longest, lines, s);
    for (size_t i = 0; i < longest; i++) {
        for (size_t n = 0; n < lines; n++)
            putchar(ptr[longest * n + i]);
        puts("");
    }
    free(s);
    free(ptr);
}


>>46,58
I AINT EVEN GONNA REED DIS SHIT UNTIL YOU POINT OUT WHERE I USE malloc IN MY PROGRAM. QUIT TRYING TO MISREPRESENT MY PROGRAM YA FUCKING COWARD.

Name: L. A. Calculus !!wKyoNUUHDOmjW7I 2013-06-21 0:24

HERE, I HAVE A CHALLENGE FOR ALL OF /prog/. FIND THE CALL TO malloc IN DIS FUCKING PROGRAM:

http://dis.4chan.org/read/prog/1366682174/12%3E

IF ANYONE CAN SHOW ME WHERE DA CALL TO malloc IS IN DAT PROGRAM, I'LL TYPE OUT EVERY PAGE OF C11 AND UPLOAD IT.

SERIOUSLY.

GET TO IT, >>58. GET ON YOUR FUCKING BIKE.

Name: Anonymous 2013-06-21 0:32

>>67
It's when the compiler allocates space to prepare the 501 nodes that you declared.

Name: Anonymous 2013-06-21 0:56

Is LAC some kind of stupid nigger bytefucker?

Name: Anonymous 2013-06-21 1:46

>>68
Isn't that just 1 call instead of using malloc on each node like dynamic array sucker up there.

Name: Anonymous 2013-06-21 1:54

>>70
That would depend on the implementation of the compiler.

Name: L. A. Calculus !!wKyoNUUHDOmjW7I 2013-06-21 2:55

>>68
A COMPILER SIMPLY TRANSLATES A PROGRAM FROM ONE LANGUAGE TO ANOTHER. IT DOESN'T INTERPRET DA FUCKING PROGRAM.

YA DON'T KNO WAT A COMPILER IS, YA DON'T KNO DA DIFFERENCE BETWEEN STATIC AND ALLOCATED STORAGE, AND YAINT RED DA FUCKING STANDARD -- DATS FOR DAMN SURE.

AND ON TOP OF DAT, YAINT EVEN RED MY FUCKIN PROGRAM. DATS 1000 struct nodeS. WAT UR REFERRING TO ARE struct node *S.

Name: Anonymous 2013-06-21 3:07

Name: L. A. Calculus !!wKyoNUUHDOmjW7I 2013-06-21 3:09

>>73
WANNA SEE MY PECS YA FUCKIN RETOID?

Name: Mod 2013-06-21 3:21

>>74
Stop insulting everyone and writing in caps. This is my first and last warning.

Name: L. A. Calculus !!wKyoNUUHDOmjW7I 2013-06-21 4:02

>>75
I'LL GET RID OF MY CHARACTER IF >>45'S CODE PROVES TO BE A GOOD READ WHEN I STUDY IT THOROUGHLY LATER ON. IF IT DOESN'T, I'LL WAIT FOR SOME OTHER DECENT PROGRAMMER TO SHOW SOME INTERESTING CODE.

OH YEAH, AND IF YOU ARE A REAL MOD, BACK DA FUCK OFF AND GO HASSLE  THOSE FAGGOTS WHO WHINE ABOUT JEWS ALL DAY LONG. DATS MY FIRST AND LAST FUCKING WARNING. MY POSTS ENCOURAGE PEOPLE TO REED DA FUCKING STANDARD, WRITE USEFUL PROGRAMS, AND THINK CAREFULLY ABOUT PROGRAMMING-RELATED TOPICS. IF UR A SOFTIE WHO WANTS TO CRAWL BACK INTO HIS MOTHER'S WOMB WHENEVER I TELL U UR FULL OF SHIT AND NEED TO REED DA FUCKING STANDARD, UR IN DA WRONG FUCKING PLACE. U KNO WHERE UR MEANT TO BE. GIVE UR FUCKING MOTHER A CALL, >>75. U DON'T HAVE WHAT IT TAKES TO BE A PROGRAMMER.

NOW ARE U FUCKING WIMPS GOING TO MAN DA FUCK UP AND TALK ABOUT PROGRAMMING, OR ARE YOU GONNA GO BACK TO TALKING ABOUT WHICH TOOHOO YOU LIKE BETTER? HUH?

Name: Anonymous 2013-06-21 4:26

>>76
Are you /u/POLITE_ALLCAPS_GUY? :3

Name: L. A. Calculus !!wKyoNUUHDOmjW7I 2013-06-21 4:32

>>77
I DON'T THINK SO. I WAS SUMMONED BY ALL OF DESE RETOIDS: http://dis.4chan.org/read/prog/1338478276

Name: Anonymous 2013-06-21 6:37

>>78
No, you definitely are /u/POLITE_ALLCAPS_GUY! I was your biggest fan when you posted on reddit, I'm so glad you came here <3 Please keep posting! :3

Name: !!YtwzkeoLMFQUgZv 2013-06-21 14:24

>>66
The idea is to track down the positions of the \n's and the lengths of sentences that come before them, which are stored in .pos and .size correspondingly. It's basically what >>40 described, using indices instead of pointers. I could've saved myself a line, and more importantly a needless loop, by having n = m[j].size > n ? m[j].size : n; inside the second loop.

Your (char) cast is sheisty, me thinks. Also, it'd be fun if you'd cast [code]ptr[code] to be a variable length array and used it with its indices instead of multiplying. Props for nice code.

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