Skip to content

Commit 51f7a02

Browse files
committed
1
1 parent 18543c8 commit 51f7a02

File tree

3 files changed

+74
-85
lines changed

3 files changed

+74
-85
lines changed

examples/perftest.c

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#include <locale.h>
2+
#include "opencstl.h"
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
6+
int count_lines(const char *filename) {
7+
FILE *fp = NULL;
8+
FOPEN(&fp, filename, "r");
9+
if (!fp) return -1;
10+
11+
int lines = 0;
12+
int c;
13+
while ((c = fgetc(fp)) != EOF) {
14+
if (c == '\n') lines++;
15+
}
16+
fclose(fp);
17+
return lines;
18+
}
19+
20+
int main() {
21+
setlocale(LC_ALL, "");
22+
23+
int lines = count_lines("words_random.txt");
24+
25+
FILE *fp = NULL;
26+
27+
FOPEN(&fp, "words_random.txt", "rt");
28+
29+
30+
if (!fp) {
31+
fprintf(stderr, "Error opening file\n");
32+
}
33+
VECTOR(char*) arr = new_vector(char*);
34+
35+
const int LINE_SIZE = 256;
36+
char *strings = (char *) calloc(lines, sizeof(char) * LINE_SIZE);
37+
memset(strings, 0, sizeof(char) * LINE_SIZE);
38+
watch t_beg = tick();
39+
int index = 0;
40+
char *line = strings;
41+
//int max_size = 0;
42+
while (GETLINE(fp, line, LINE_SIZE)) {
43+
size_t len = strlen(line);
44+
push_back(arr, line);
45+
line += LINE_SIZE;
46+
index++;
47+
}
48+
//printf("max size: %lld\n", max_size);
49+
double push_back_ms = lap(t_beg, tick());
50+
printf("size: %lu\telapsed_time: %lf\n",cstl_size(arr), push_back_ms);
51+
52+
53+
t_beg = tick();
54+
55+
//char ***tree = cstl_set(char*, StringCmp);
56+
UNORDERED_SET(char*) tree = new_unordered_set(char*);
57+
for (int i = 0; i < size(arr); i++) {
58+
char *line = arr[i];
59+
insert(tree, line);
60+
//puts(line);
61+
}
62+
double insert_time = lap(t_beg, tick());
63+
printf("size: %lu\telapsed_time: %lf\n",size(tree), insert_time);
64+
65+
66+
// for (char **it = cstl_begin(tree); it != cstl_end(tree); it = cstl_next(it)) {
67+
// printf("%s\n",cstl_value(it, char*));
68+
// }
69+
free(strings);
70+
cstl_free(tree);
71+
cstl_free(arr);
72+
return 0;
73+
}

opencstl/cstl_file.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
bool cstl_fopen(FILE **fp, const char *filename, const char *mode) {
1616
#if defined(_WIN32) || defined(_WIN64) ||defined(__TINYC__)
17-
*fp = fopen(filename, mode);
17+
fopen_s(fp, filename, mode);
1818
#elif defined(__linux__) && defined(__GNUC__)
1919
*fp = fopen(filename, mode);
2020
#elif defined(__APPLE__)

perftest.c

Lines changed: 0 additions & 84 deletions
This file was deleted.

0 commit comments

Comments
 (0)