spammify.c: Require a certain count instead of elapsed time
authorW. Trevor King <wking@tremily.us>
Wed, 17 Apr 2013 16:07:19 +0000 (12:07 -0400)
committerW. Trevor King <wking@tremily.us>
Wed, 17 Apr 2013 16:07:19 +0000 (12:07 -0400)
Increasing the number of threads time sharing on the same core does
not increase total execution time if the threads are only checking for
elapsed time.  We need them to actually *do* something.

spammify.c

index 1001f341be1c44635dfaf6c273b7eb5b52321d9c..2482e668c39d20e62767f80ef560715e079489ce 100644 (file)
@@ -1,27 +1,23 @@
 #include <Python.h>
 #include <stdlib.h>
-#include <time.h>
 
 static PyObject *
 spam_busy(PyObject *self, PyObject *args)
 {
-       int seconds;
-       time_t start;
-       long int i = 0;
+       long int count, i;
 
-       if (!PyArg_ParseTuple(args, "i", &seconds))
+       if (!PyArg_ParseTuple(args, "l", &count))
                return NULL;
        Py_BEGIN_ALLOW_THREADS
-       start = time(NULL);
-       while (time(NULL) < start + seconds)
-               i++;
+       for (i = 0; i < count; i++)
+               ;
        Py_END_ALLOW_THREADS
        return PyLong_FromLong(i);
 }
 
 static PyMethodDef SpamMethods[] = {
        {"busy",  spam_busy, METH_VARARGS,
-        "Drop the GIL and run a busy wait for a few seconds."},
+        "Drop the GIL and run a busy wait until you count to `count`."},
        {NULL, NULL, 0, NULL}        /* Sentinel */
 };