Author: Maciej Fijalkowski <fij...@gmail.com> Branch: extradoc Changeset: r3854:9c28e3872bf2 Date: 2011-07-31 00:44 +0200 http://bitbucket.org/pypy/extradoc/changeset/9c28e3872bf2/
Log: update diff --git a/blog/draft/string-formatting.rst b/blog/draft/string-formatting.rst --- a/blog/draft/string-formatting.rst +++ b/blog/draft/string-formatting.rst @@ -4,16 +4,19 @@ String formatting is probably something you do just about every day in Python, and never think about. It's so easy, just ``"%d %d" % (i, i)`` and you're done. No thinking about how to size your result buffer, whether your output -has an appropriae NUL byte at the end, or any other details. A simple C +has an appropriae NULL byte at the end, or any other details. A C equivilant might be:: - char x[23]; + char x[41]; sprintf(x, "%d %d", i, i); -Which is fine, except you can't even return ``x`` from this function, a more +Note that we had to stop for a second and consider how big numbers might get +and overestimate the size (41 = length of the biggest number on 64bit + 1 for +the sign). +This is fine, except you can't even return ``x`` from this function, a more fair comparison might be:: - char *x = calloc(23, sizeof(char)); + char *x = malloc(41 * sizeof(char)); sprintf(x, "%d %d", i, i); ``x`` is slightly overallocated in some situations, but that's fine. @@ -36,7 +39,7 @@ int main() { int i = 0; - char x[23]; + char x[41]; for (i = 0; i < 10000000; i++) { sprintf(x, "%d %d", i, i); } @@ -64,4 +67,4 @@ } } -Which as discussed above, is more comperable to the Python, takes 1.93 seconds. \ No newline at end of file +Which as discussed above, is more comperable to the Python, takes 1.93 seconds. _______________________________________________ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit