All pastes #2093455 Raw Edit

Stuff

public text v1 · immutable
#2093455 ·published 2011-11-10 00:17 UTC
rendered paste body
#include <stdlib.h>
#include <windows.h>
#include <stdio.h>
#include <assert.h>

typedef struct elem {
    struct elem* next;
} elem;

int main()
{
    elem* head = 0;
    elem* curr, *late;
    int i;
    LARGE_INTEGER start, end, freq;
    int skips = 5;

    for (i = 0; i < 2000000; ++i) {
	elem* n = malloc (sizeof *n);

	n->next = head;
	head = n;
    }
    QueryPerformanceFrequency (&freq);
    printf ("freq %I64d\n", *(__int64*)&freq);

    for (curr = head; curr; curr = curr->next) {}

    QueryPerformanceCounter (&start);
    for (curr = head; curr; curr = curr->next) {}
    QueryPerformanceCounter (&end);
    printf ("%I64d\n", *(__int64*)&end - *(__int64*)&start);

    QueryPerformanceCounter (&start);
    for (curr = head; curr; curr = curr->next) {}
    for (curr = head; curr; curr = curr->next) {}
    QueryPerformanceCounter (&end);
    printf ("%I64d\n", *(__int64*)&end - *(__int64*)&start);

    QueryPerformanceCounter (&start);
    for (curr = head; curr && skips-- > 0; curr = curr->next) {}
    assert (skips <= 0);
    late = head;
    for (; curr; curr = curr->next, late = late->next) {}
    QueryPerformanceCounter (&end);
    printf ("%I64d\n", *(__int64*)&end - *(__int64*)&start);

    printf ("head %p curr %p late %p\n", head, curr, late);

    return 0;
}