summaryrefslogtreecommitdiffstats
path: root/Modules/_testcapimodule.c
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2007-01-14 03:31:43 (GMT)
committerGuido van Rossum <guido@python.org>2007-01-14 03:31:43 (GMT)
commitddefaf31b366ea84250fc5090837c2b764a04102 (patch)
treeab3d7b5172f4e6a064165468fc70beb41bdca1d3 /Modules/_testcapimodule.c
parent5b787e8bc2dbda5583eee039cb6a6e47c8d8a034 (diff)
downloadcpython-ddefaf31b366ea84250fc5090837c2b764a04102.zip
cpython-ddefaf31b366ea84250fc5090837c2b764a04102.tar.gz
cpython-ddefaf31b366ea84250fc5090837c2b764a04102.tar.bz2
Merged the int/long unification branch, by very crude means (sorry Thomas!).
I banged on the code (beyond what's in that branch) to make fewer tests fail; the only tests that fail now are: test_descr -- can't pickle ints?! test_pickletools -- ??? test_socket -- See python.org/sf/1619659 test_sqlite -- ??? I'll deal with those later.
Diffstat (limited to 'Modules/_testcapimodule.c')
-rw-r--r--Modules/_testcapimodule.c116
1 files changed, 116 insertions, 0 deletions
diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c
index b11f0ae..09bb4ff 100644
--- a/Modules/_testcapimodule.c
+++ b/Modules/_testcapimodule.c
@@ -718,6 +718,119 @@ test_with_docstring(PyObject *self)
Py_RETURN_NONE;
}
+#ifdef HAVE_GETTIMEOFDAY
+/* Profiling of integer performance */
+void print_delta(int test, struct timeval *s, struct timeval *e)
+{
+ e->tv_sec -= s->tv_sec;
+ e->tv_usec -= s->tv_usec;
+ if (e->tv_usec < 0) {
+ e->tv_sec -=1;
+ e->tv_usec += 1000000;
+ }
+ printf("Test %d: %d.%06ds\n", test, (int)e->tv_sec, e->tv_usec);
+}
+
+static PyObject *
+profile_int(PyObject *self, PyObject* args)
+{
+ int i, k;
+ struct timeval start, stop;
+ PyObject *single, **multiple, *op1, *result;
+
+ /* Test 1: Allocate and immediately deallocate
+ many small integers */
+ gettimeofday(&start, NULL);
+ for(k=0; k < 20000; k++)
+ for(i=0; i < 1000; i++) {
+ single = PyInt_FromLong(i);
+ Py_DECREF(single);
+ }
+ gettimeofday(&stop, NULL);
+ print_delta(1, &start, &stop);
+
+ /* Test 2: Allocate and immediately deallocate
+ many large integers */
+ gettimeofday(&start, NULL);
+ for(k=0; k < 20000; k++)
+ for(i=0; i < 1000; i++) {
+ single = PyInt_FromLong(i+1000000);
+ Py_DECREF(single);
+ }
+ gettimeofday(&stop, NULL);
+ print_delta(2, &start, &stop);
+
+ /* Test 3: Allocate a few integers, then release
+ them all simultaneously. */
+ multiple = malloc(sizeof(PyObject*) * 1000);
+ gettimeofday(&start, NULL);
+ for(k=0; k < 20000; k++) {
+ for(i=0; i < 1000; i++) {
+ multiple[i] = PyInt_FromLong(i+1000000);
+ }
+ for(i=0; i < 1000; i++) {
+ Py_DECREF(multiple[i]);
+ }
+ }
+ gettimeofday(&stop, NULL);
+ print_delta(3, &start, &stop);
+
+ /* Test 4: Allocate many integers, then release
+ them all simultaneously. */
+ multiple = malloc(sizeof(PyObject*) * 1000000);
+ gettimeofday(&start, NULL);
+ for(k=0; k < 20; k++) {
+ for(i=0; i < 1000000; i++) {
+ multiple[i] = PyInt_FromLong(i+1000000);
+ }
+ for(i=0; i < 1000000; i++) {
+ Py_DECREF(multiple[i]);
+ }
+ }
+ gettimeofday(&stop, NULL);
+ print_delta(4, &start, &stop);
+
+ /* Test 5: Allocate many integers < 32000 */
+ multiple = malloc(sizeof(PyObject*) * 1000000);
+ gettimeofday(&start, NULL);
+ for(k=0; k < 10; k++) {
+ for(i=0; i < 1000000; i++) {
+ multiple[i] = PyInt_FromLong(i+1000);
+ }
+ for(i=0; i < 1000000; i++) {
+ Py_DECREF(multiple[i]);
+ }
+ }
+ gettimeofday(&stop, NULL);
+ print_delta(5, &start, &stop);
+
+ /* Test 6: Perform small int addition */
+ op1 = PyInt_FromLong(1);
+ gettimeofday(&start, NULL);
+ for(i=0; i < 10000000; i++) {
+ result = PyNumber_Add(op1, op1);
+ Py_DECREF(result);
+ }
+ gettimeofday(&stop, NULL);
+ Py_DECREF(op1);
+ print_delta(6, &start, &stop);
+
+ /* Test 7: Perform medium int addition */
+ op1 = PyInt_FromLong(1000);
+ gettimeofday(&start, NULL);
+ for(i=0; i < 10000000; i++) {
+ result = PyNumber_Add(op1, op1);
+ Py_DECREF(result);
+ }
+ gettimeofday(&stop, NULL);
+ Py_DECREF(op1);
+ print_delta(7, &start, &stop);
+
+ Py_INCREF(Py_None);
+ return Py_None;
+}
+#endif
+
static PyMethodDef TestMethods[] = {
{"raise_exception", raise_exception, METH_VARARGS},
{"test_config", (PyCFunction)test_config, METH_NOARGS},
@@ -756,6 +869,9 @@ static PyMethodDef TestMethods[] = {
#ifdef WITH_THREAD
{"_test_thread_state", test_thread_state, METH_VARARGS},
#endif
+#ifdef HAVE_GETTIMEOFDAY
+ {"profile_int", profile_int, METH_NOARGS},
+#endif
{NULL, NULL} /* sentinel */
};