xcb-studies/studies/xcb_good_usage.cc

55 lines
1.4 KiB
C++

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <xcb/xcb.h>
double get_time(void) {
struct timeval timev;
gettimeofday(&timev, NULL);
return (double)timev.tv_sec + (((double)timev.tv_usec) / 1000000);
}
int main() {
int count = 500;
xcb_atom_t* atoms = (xcb_atom_t*)malloc(count * sizeof(atoms));
xcb_intern_atom_cookie_t* cookies =
(xcb_intern_atom_cookie_t*)malloc(count * sizeof(xcb_intern_atom_cookie_t));
char** names;
double end, diff_x;
/* init names */
names = (char**)malloc(count * sizeof(char*));
for (int i = 0; i < count; ++i) {
char buf[100];
sprintf(buf, "NAME%d", i);
names[i] = strdup(buf);
}
auto start = get_time();
xcb_connection_t* connection = xcb_connect(NULL, NULL);
for (int i = 0; i < count; ++i) {
cookies[i] = xcb_intern_atom(connection, 0, strlen(names[i]), names[i]);
}
for (int i = 0; i < count; i++) {
xcb_intern_atom_reply_t* r = xcb_intern_atom_reply(connection, cookies[i], 0);
if (r) {
atoms[i] = r->atom;
free(r);
}
}
xcb_disconnect(connection);
end = get_time();
diff_x = end - start;
printf("XCB (good) use time : %f\n", diff_x);
for (int i = 0; i < count; ++i)
free(names[i]);
free(names);
free(atoms);
free(cookies);
return 0;
}