summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2003-01-06 22:42:41 (GMT)
committerRaymond Hettinger <python@rcn.com>2003-01-06 22:42:41 (GMT)
commit0a2f849b791d13cf0dde97dce2e71a19e2d60465 (patch)
treed52434d1d5aab5dc4a919018edf12b432a20c0f2 /Objects
parent2720b0dff80650a04f8ebb3cc7163ec6897e02dd (diff)
downloadcpython-0a2f849b791d13cf0dde97dce2e71a19e2d60465.zip
cpython-0a2f849b791d13cf0dde97dce2e71a19e2d60465.tar.gz
cpython-0a2f849b791d13cf0dde97dce2e71a19e2d60465.tar.bz2
GvR's idea to use memset() for the most common special case of repeating
a single character. Shaves another 10% off the running time by avoiding the lg2(N) loops and cache effects for the other cases.
Diffstat (limited to 'Objects')
-rw-r--r--Objects/stringobject.c6
1 files changed, 5 insertions, 1 deletions
diff --git a/Objects/stringobject.c b/Objects/stringobject.c
index acfce8b..1a4a754 100644
--- a/Objects/stringobject.c
+++ b/Objects/stringobject.c
@@ -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;
}