-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathunique.cpp
More file actions
122 lines (100 loc) · 2.41 KB
/
unique.cpp
File metadata and controls
122 lines (100 loc) · 2.41 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <iostream>
#include <memory>
#include <unistd.h>
// compile: g++ -std=c++14 -O3 unique.cpp -o unique
static int numinserts = 1000;
struct node {
int val;
std::unique_ptr<struct node> next;
};
static unsigned long long rdtsc()
{
unsigned hi, lo;
asm volatile("rdtsc" : "=a"(lo), "=d"(hi));
return (((unsigned long long)hi) << 32) | lo;
}
int delete_node(std::unique_ptr<struct node>& root, int idx)
{
if (root == NULL || idx < 0)
return 0;
if (idx == 0) {
root = std::move(root->next);
return 1;
}
std::unique_ptr<struct node>* rootp = &root;
while (*rootp) {
if (idx == 1) {
std::unique_ptr<struct node> todel = std::move((*rootp)->next);
if (todel)
(*rootp)->next = std::move(todel->next);
return 1;
}
rootp = &(*rootp)->next;
idx--;
}
return 0;
}
int iterate_list(std::unique_ptr<struct node>& root)
{
if (root == NULL)
return 0;
std::unique_ptr<struct node>* rootp = &root;
int ret = 0;
while (*rootp) {
ret++;
rootp = &(*rootp)->next;
}
return ret;
}
int insert_nodes(std::unique_ptr<struct node>& root)
{
std::unique_ptr<struct node> elem;
elem = std::unique_ptr<struct node>(new(struct node));
if (elem == NULL)
return 0;
elem->val = 0;
elem->next = std::move(root);
root = std::move(elem);
return 1;
}
int main(int argc, const char *argv[])
{
std::unique_ptr<struct node> head = NULL;
int numelems;
unsigned long long time;
if (argc == 2) {
numinserts = atoi(argv[1]);
}
printf("numinserts : %d\n", numinserts);
if (numinserts < 0)
return 0;
time = rdtsc();
for (int i = 0; i < numinserts; i++) {
if (!insert_nodes(head)) {
std::cout << "failed to insert node : " << i << std::endl;
return 0;
}
}
printf("time taken during insert: %lld ms\n", (rdtsc() - time)/2530000);
numelems = iterate_list(head);
if (numelems != numinserts) {
printf("test failed! %d %d\n", numelems, numinserts);
}
time = rdtsc();
for (int i = 0; i < numinserts/20; i++) {
int idx = rand() % ((numinserts/20) - i);
if (!delete_node(head, idx)) {
std::cout << "failed to delete node : " << idx << std::endl;
return 0;
}
}
printf("time taken during delete: %lld ms\n", (rdtsc() - time)/2530000);
numelems = iterate_list(head);
if (numelems != numinserts - (numinserts/20)) {
printf("test failed! %d %d\n", numelems, numinserts);
}
time = rdtsc();
head = NULL;
printf("time taken during cleanup: %lld ms\n", (rdtsc() - time)/2530000);
return 0;
}