xcb-studies/studies/xcb_poor_usage.cc

45 lines
1.1 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));
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) {
atoms[i] = xcb_intern_atom_reply(
connection, xcb_intern_atom(connection, 0, strlen(names[i]), names[i]), NULL)
->atom;
}
xcb_disconnect(connection);
end = get_time();
diff_x = end - start;
printf("XCB (poor) use time : %f\n", diff_x);
for (int i = 0; i < count; ++i)
free(names[i]);
free(names);
return 0;
}