]> granicus.if.org Git - python/commitdiff
GvR's idea to use memset() for the most common special case of repeating
authorRaymond Hettinger <python@rcn.com>
Mon, 6 Jan 2003 22:42:41 +0000 (22:42 +0000)
committerRaymond Hettinger <python@rcn.com>
Mon, 6 Jan 2003 22:42:41 +0000 (22:42 +0000)
a single character.  Shaves another 10% off the running time by avoiding
the lg2(N) loops and cache effects for the other cases.

Objects/stringobject.c

index acfce8b1047a1d3ba6e0d02c892fde253928475a..1a4a7547deb0313e9f070ab54ccf689b1b0c49c7 100644 (file)
@@ -966,6 +966,11 @@ string_repeat(register PyStringObject *a, register int n)
        PyObject_INIT_VAR(op, &PyString_Type, size);
        op->ob_shash = -1;
        op->ob_sstate = SSTATE_NOT_INTERNED;
+       op->ob_sval[size] = '\0';
+       if (a->ob_size == 1 && n > 0) {
+               memset(op->ob_sval, a->ob_sval[0] , n);
+               return (PyObject *) op;
+       }
        i = 0;
        if (i < size) {
                memcpy(op->ob_sval, a->ob_sval, (int) a->ob_size);
@@ -976,7 +981,6 @@ string_repeat(register PyStringObject *a, register int n)
                memcpy(op->ob_sval+i, op->ob_sval, j);
                i += j;
        }
-       op->ob_sval[size] = '\0';
        return (PyObject *) op;
 }