[Xcb-commit] tutorial.mdwn
XCB site
xcb at freedesktop.org
Tue Oct 7 01:03:05 PDT 2008
tutorial.mdwn | 315 ++++++++++++++++++++++++++--------------------------------
1 file changed, 142 insertions(+), 173 deletions(-)
New commits:
commit 296a7949b615ce430bf1e22ffa9eaeb49485fb29
Author: XCB site <xcb at freedesktop.org>
Date: Tue Oct 7 01:02:56 2008 -0700
web commit by FrederikHertzum: Fixed the comparing example between Xlib and XCB. Also included an implementation for strdup. Must be compiled on a C99 compiler (--std=gnu99 on GCC compilers)
diff --git a/tutorial.mdwn b/tutorial.mdwn
index dbb599a..963d568 100644
--- a/tutorial.mdwn
+++ b/tutorial.mdwn
@@ -89,183 +89,152 @@ Here is a program that computes the time to create 500 atoms with Xlib and XCB.
/* It's a good idea to paste this and other long code examples
into a text editor for easier reading */
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <sys/time.h>
-
- #include <xcb/xcb.h>
-
- #include <X11/Xlib.h>
-
- #define NUM_NAMES 500
-
- /*
- NOTE: For concision, we're going to be cheesy and use
- arrays where real code would use points and memory allocation.
- */
-
-
- /*
- return interval of time (uses time.h)
- */
- double
- get_time (void)
- {
- struct timeval timev;
- gettimeofday(&timev, NULL);
- return (double)timev.tv_sec + (((double)timev.tv_usec) / 1000000);
- }
-
-
- /*
- */
- void
- useXlib (char **names,
- Display *display )
- {
- Atom atoms[NUM_NAMES];
-
- for (int i = 0; i < NUM_NAMES; ++i) {
- atoms[i] = XInternAtom(display, names[i], 0);
- }
- }
-
-
- /*
- */
- void
- useXCBPoorly (char **names,
- xcb_connection_t *connection )
- {
- xcb_atom_t atoms[NUM_NAMES];
+/*
+ It's a good idea to paste this and other long code examples into a text
+ editor for easier reading */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/time.h>
+#include <xcb/xcb.h>
+#include <X11/Xlib.h>
+#define NUM_NAMES 500
+/*
+ NOTE: For concision, we're going to be cheesy and use arrays where real code
+ would use points and memory allocation.s
+*/
+#ifndef __GNUC__
+char* strdup(const char* s) {
+ int n = strlen(s) + 1;
+
+ char *dup = malloc(n);
+
+ if(dup)
+ strcpy(dup, s);
+
+ return dup;
+}
+#endif
+
+/*
+ return interval of time (uses time.h)
+*/
+double
+get_time (void) {
+ struct timeval timev;
+ gettimeofday(&timev, NULL);
+ return (double)timev.tv_sec + (((double)timev.tv_usec) / 1000000);
+}
+
+/*
- // in this bad use of xcb, we use the cookie immediately after posting the request with xcb_intern_atom
+*/
+void
+useXlib (char **names,
+ Display *display ) {
+
+ Atom atoms[NUM_NAMES];
+ for (int i = 0; i < NUM_NAMES; ++i) {
+ atoms[i] = XInternAtom(display, names[i], 0);
+ }
+}
+
+/*
- for (int i = 0; i < NUM_NAMES; ++i) {
- /* make request */
- xcb_intern_atom_cookie_t cookie = xcb_intern_atom (connection,
- 0,
- strlen(names[i]),
- names[i] );
-
- /* get response */
- xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply (connection,
- cookie,
- NULL ); // normally a pointer to receive error, but we'll just ignore error handling
-
- if (reply) {
- atoms[i] = reply->atom;
- free (reply);
- }
- }
-
- // now we have our atoms (replies), but this is just a demo, so we do nothing with them
- }
-
-
- /*
- */
- void
- useXCBProperly (char **names,
- xcb_connection_t *connection )
- {
- xcb_atom_t atoms[NUM_NAMES];
- xcb_intern_atom_cookie_t cookies[NUM_NAMES];
-
- // in this good example, we make all our requests before checking for
- // replies because it's best to queue requests when we have many at once
-
- /* make requests */
- for (int i = 0; i < NUM_NAMES; ++i) {
- cookies[i] = xcb_intern_atom (connection,
- 0,
- strlen (names[i]),
- names[i] );
- }
-
- /* get responses */
- for (int i = 0; i < NUM_NAMES; ++i) {
- xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply (connection,
- cookies[i],
- NULL ); // normally a pointer to receive errors, but we'll just ignore error handling
-
- if (reply) {
- atoms[i] = reply->atom;
- free (reply);
- }
- }
-
- // now we have our atoms (replies), but this is just a demo, so we do nothing with them
+*/
+void
+useXCBPoorly (char **names,
+ xcb_connection_t *connection ) {
+ xcb_atom_t atoms[NUM_NAMES];
+ // in this bad use of xcb, we use the cookie immediately after posting the request with xcb_intern_atom
+ for (int i = 0; i < NUM_NAMES; ++i) {
+ /* make request */
+ xcb_intern_atom_cookie_t cookie = xcb_intern_atom (connection,
+ 0,
+ strlen(names[i]),
+ names[i] );
+ /* get response */
+ xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply (connection,
+ cookie,
+ NULL ); // normally a pointer to receive error, but we'll just ignore error handling
+ if (reply) {
+ atoms[i] = reply->atom;
+ free (reply);
}
-
-
- int
- main ()
- {
- /* setup names for tests */
-
- char (*names)[NUM_NAMES];
-
- // init names to "NAME0", "NAME1", "NAME2" ... and so on
- for (int i = 0; i < NUM_NAMES; ++i) {
- char buf[100];
- sprintf (buf, "NAME%d", i);
- names[i] = strdup (buf);
- }
-
-
- /* do tests */
-
- double start, XlibTime, XCBBadTime, XCBGoodTime;
-
-
- /* test Xlib */
-
- Display *display = XOpenDisplay (getenv("DISPLAY"));
-
- start = get_time ();
- useXlib (names);
- XlibTime = get_time () - start;
-
- XCloseDisplay (display);
-
-
-
-
- /* test XCB */
-
- xcb_connection_t *connection = xcb_connect (NULL, NULL);
-
- start = get_time ();
- useXCBPoorly (names);
- XCBBadTime = get_time () - start;
-
- start = get_time ();
- useXCBProperly (names);
- XCBGoodTime = get_time () - start;
-
- xcb_disconnect (connection);
-
-
- /* report times */
-
- printf ("Xlib time : %f\n", XlibTime);
- printf ("Bad xcb time : %f\n", XCBBadTime);
- printf ("Good xcb time : %f\n", XCBGoodTime);
-
- printf ("ratio of good xcb time to bad xcb time: %f\n", XCBGoodTime / XCBBadTime);
- printf ("ratio of Xlib time to good xcb time: %f\n", XlibTime / XCBGoodTime);
-
-
- /* free names */
-
- for (int i = 0; i < NUM_NAMES; ++i) {
- free (names[i]);
- }
-
- return 0;
+ }
+ // now we have our atoms (replies), but this is just a demo, so we do nothing with them
+}
+
+/*
+*/
+void
+useXCBProperly (char **names,
+ xcb_connection_t *connection ) {
+ xcb_atom_t atoms[NUM_NAMES];
+ xcb_intern_atom_cookie_t cookies[NUM_NAMES];
+ // in this good example, we make all our requests before checking for
+ // replies because it's best to queue requests when we have many at once
+ /* make requests */
+ for (int i = 0; i < NUM_NAMES; ++i) {
+ cookies[i] = xcb_intern_atom (connection,
+ 0,
+ strlen (names[i]),
+ names[i] );
+ }
+ /* get responses */
+ for (int i = 0; i < NUM_NAMES; ++i) {
+ xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply (connection,
+ cookies[i],
+ NULL ); // normally a pointer to receive errors, but we'll just ignore error handling
+ if (reply) {
+ atoms[i] = reply->atom;
+ free (reply);
}
+ }
+ // now we have our atoms (replies), but this is just a demo, so we do nothing with them
+}
+
+int
+main () {
+ /* setup names for tests */
+ char (**names) = malloc(NUM_NAMES);
+ // init names to "NAME0", "NAME1", "NAME2" ... and so on
+ for (int i = 0; i < NUM_NAMES; ++i) {
+ char buf[100];
+ sprintf (buf, "NAME%d", i);
+ names[i] = strdup (buf);
+ }
+
+ /* do tests */
+ double start, XlibTime, XCBBadTime, XCBGoodTime;
+
+ /* test Xlib */
+ Display *display = XOpenDisplay (getenv("DISPLAY"));
+ start = get_time ();
+ useXlib (names, display);
+ XlibTime = get_time () - start;
+ XCloseDisplay (display);
+
+ /* test XCB */
+ xcb_connection_t *connection = xcb_connect (NULL, NULL);
+ start = get_time ();
+ useXCBPoorly (names, connection);
+ XCBBadTime = get_time () - start;
+ start = get_time ();
+ useXCBProperly (names, connection);
+ XCBGoodTime = get_time () - start;
+ xcb_disconnect (connection);
+
+ /* report times */
+ printf ("Xlib time : %f\n", XlibTime);
+ printf ("Bad xcb time : %f\n", XCBBadTime);
+ printf ("Good xcb time : %f\n", XCBGoodTime);
+ printf ("ratio of good xcb time to bad xcb time: %f\n", XCBGoodTime / XCBBadTime);
+ printf ("ratio of Xlib time to good xcb time: %f\n", XlibTime / XCBGoodTime);
+
+ return 0;
+}
### 3. The Graphics Context
More information about the xcb-commit
mailing list