-
Notifications
You must be signed in to change notification settings - Fork 58
/
skiplist_test.c
95 lines (81 loc) · 2.31 KB
/
skiplist_test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
* Copyright (C) 2015, Leo Ma <[email protected]>
*/
#include <stdio.h>
#include <stdlib.h>
#if defined(__MACH__) && !defined(CLOCK_REALTIME)
#include <sys/time.h>
#define CLOCK_REALTIME 0
#define CLOCK_MONOTONIC 1
int clock_gettime(int clk_id, struct timespec* t) {
struct timeval now;
int rv = gettimeofday(&now, NULL);
if (rv) return rv;
t->tv_sec = now.tv_sec;
t->tv_nsec = now.tv_usec * 1000;
return 0;
}
#else
#include <time.h>
#endif
#include "skiplist.h"
#define N 2 * 1024 * 1024
// #define SKIPLIST_DEBUG
int
main(void)
{
int i;
struct timespec start, end;
int *key = malloc(N * sizeof(int));
if (key == NULL) {
exit(-1);
}
struct skiplist *list = skiplist_new();
if (list == NULL) {
exit(-1);
}
printf("Test start!\n");
printf("Add %d nodes...\n", N);
/* Insert test */
srandom(time(NULL));
clock_gettime(CLOCK_MONOTONIC, &start);
for (i = 0; i < N; i++) {
int value = key[i] = (int)random();
skiplist_insert(list, key[i], value);
}
clock_gettime(CLOCK_MONOTONIC, &end);
printf("time span: %ldms\n", (end.tv_sec - start.tv_sec)*1000 + (end.tv_nsec - start.tv_nsec)/1000000);
#ifdef SKIPLIST_DEBUG
skiplist_dump(list);
#endif
/* Search test */
printf("Now search each node...\n");
clock_gettime(CLOCK_MONOTONIC, &start);
for (i = 0; i < N; i++) {
struct skipnode *node = skiplist_search(list, key[i]);
if (node != NULL) {
#ifdef SKIPLIST_DEBUG
printf("key:0x%08x value:0x%08x\n", node->key, node->value);
#endif
} else {
printf("Not found:0x%08x\n", key[i]);
}
}
clock_gettime(CLOCK_MONOTONIC, &end);
printf("time span: %ldms\n", (end.tv_sec - start.tv_sec)*1000 + (end.tv_nsec - start.tv_nsec)/1000000);
/* Delete test */
printf("Now remove all nodes...\n");
clock_gettime(CLOCK_MONOTONIC, &start);
for (i = 0; i < N; i++) {
skiplist_remove(list, key[i]);
}
clock_gettime(CLOCK_MONOTONIC, &end);
printf("time span: %ldms\n", (end.tv_sec - start.tv_sec)*1000 + (end.tv_nsec - start.tv_nsec)/1000000);
#ifdef SKIPLIST_DEBUG
skiplist_dump(list);
#endif
printf("End of Test.\n");
skiplist_delete(list);
free(key);
return 0;
}