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: 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.

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