]> git.cworth.org Git - cworth.org/blob - src/exa/rectangles.c
Add Robert Haight as another solver
[cworth.org] / src / exa / rectangles.c
1 /* gcc `pkg-config --cflags --libs x11` -o rectangles rectangles.c */
2 #include <X11/Xlib.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <sys/time.h>
6 #include <time.h>
7
8 #define MEASUREMENTS 5
9 #define ITERATIONS 4000
10
11 int
12 main (int argc, char **argv)
13 {
14     Display *dpy;
15     Window win;
16     XSetWindowAttributes attr;
17     XEvent xev;
18     GC gc, gc_white, gc_black;
19     unsigned long gcmask = 0l;
20     XGCValues gcvalues;
21     struct timeval tv_start, tv_stop;
22     int i, j;
23     double elapsed;
24     XImage *ximage;
25
26     dpy = XOpenDisplay (NULL);
27     if (dpy == NULL) {
28         fprintf (stderr, "Failed to open display %s\n", XDisplayName (NULL));
29         exit (1);
30     }
31
32     attr.override_redirect = True;
33     win = XCreateWindow (dpy, DefaultRootWindow (dpy), 0, 0,
34                          600, 600, 0, DefaultDepth (dpy, DefaultScreen (dpy)),
35                          InputOutput, DefaultVisual (dpy, DefaultScreen (dpy)),
36                          CWOverrideRedirect, &attr);
37
38     gcmask |= GCFunction;
39     gcvalues.function = GXcopy;
40     gcmask |= GCForeground;
41     gcvalues.foreground = WhitePixel (dpy, DefaultScreen (dpy));
42     gc_white = XCreateGC (dpy, win, gcmask, &gcvalues);
43
44     gcvalues.foreground = BlackPixel (dpy, DefaultScreen (dpy));
45     gc_black = XCreateGC (dpy, win, gcmask, &gcvalues);
46
47     XMapWindow (dpy, win);
48
49     gc = gc_black;
50     for (i = 0; i < MEASUREMENTS; i++) {
51         /* Nothing fancy here, just a gettimeofday system call. */
52         gettimeofday (&tv_start, NULL);
53         for (j = 0; j < ITERATIONS; j++) {
54             XFillRectangle (dpy, win, gc, 0, 0, 500, 500);
55             if (gc == gc_white)
56                 gc = gc_black;
57             else
58                 gc = gc_white;
59         }
60         /* Standard 1x1 XGetImage to wait for rendering to complete. */
61         ximage = XGetImage (dpy, win, 0, 0, 1, 1, AllPlanes, ZPixmap);
62         gettimeofday (&tv_stop, NULL);
63         XDestroyImage (ximage);
64
65         elapsed = tv_stop.tv_sec - tv_start.tv_sec +
66             (tv_stop.tv_usec - tv_start.tv_usec) / 1000000.0;
67         printf ("%d iterations @ %.4f msec ( %.1f/sec): 500x500 XFillRectangle\n",
68                 ITERATIONS, elapsed * 1000 / ITERATIONS, ITERATIONS / elapsed);
69     }
70             
71     XCloseDisplay (dpy);
72
73     return 0;
74 }