Speed of Python vs. Perl

Paul Jackson pj at sgi.com
Fri Jan 12 18:27:41 EST 2001


/F wrote:
|> how about changing read_compiled_module()

Ok - working on a copy of Python 1.5.2 over Irix 6.5.10, I tried
this, and also squeezing out an expensive strspn() call inside
the Python/compile.c code.

The original Python can print "Hello, world" in about 180
msecs on this machine.  Perl can do it in about 50 msecs.
Fredrik's marshal.c change saves perhaps 40 msecs, and my
strspn() change another 10 or 20 msecs, bringing the Python
cost down from 180 msecs to about 120 or 130 msecs.

So these two changes account for almost half the Perl win
in startup time, at least on my current system.

Here are the two diff's.  Probably there is something better I
should be doing with these diff's than just posting them here
-- if so, let me know.


--- marshal.c.orig      Wed Oct  7 18:45:47 1998
+++ marshal.c   Fri Jan 12 15:01:03 2001
@@ -38,6 +38,8 @@
 #include "longintrepr.h"
 #include "compile.h"
 #include "marshal.h"
+#include <sys/types.h>
+#include <sys/stat.h>
 
 #define TYPE_NULL      '0'
 #define TYPE_NONE      'N'
@@ -611,14 +613,40 @@
        return r_long(&rf);
 }
 
+static off_t
+getfilesize(fp)
+       FILE *fp;
+{
+       struct stat st;
+       if (fstat(fileno(fp), &st) != 0)
+               return -1;
+       else
+               return st.st_size;
+}
+
 PyObject *
 PyMarshal_ReadObjectFromFile(fp)
        FILE *fp;
 {
        RFILE rf;
+       off_t filesize;
        if (PyErr_Occurred()) {
                fprintf(stderr, "XXX rd_object called with exception set\n");
                return NULL;
+       }
+#      define FILEBUFSIZE 65536
+       filesize = getfilesize(fp);
+       if (filesize > 0 && filesize < FILEBUFSIZE) {
+               char *buf;
+               buf = malloc(filesize);
+               if (buf > 0) {
+                       PyObject *pop;
+                       size_t rsz;
+                       rsz = fread(buf, 1, filesize, fp);
+                       pop = PyMarshal_ReadObjectFromString(buf, rsz);
+                       free(buf);
+                       return pop;
+               }
        }
        rf.fp = fp;
        return r_object(&rf);


--- compile.c.orig      Thu Jan 28 07:08:09 1999
+++ compile.c   Fri Jan 12 14:59:41 2001
@@ -198,6 +198,27 @@
 #define NAME_CHARS \
        "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
 
+/* all_name_chars(s): true iff all chars in s are valid NAME_CHARS */
+
+static int
+all_name_chars(s)
+       char *s;
+{
+       static char ok_name_char[256];
+       static char *name_chars = NAME_CHARS;
+
+       if (ok_name_char[*name_chars] == 0) {
+               char *p;
+               for (p = name_chars; *p; p++)
+                       ok_name_char[*p] = 1;
+       }
+       while (*s) {
+               if (ok_name_char[*s++] == 0)
+                       return 0;
+       }
+       return 1;
+}
+
 PyCodeObject *
 PyCode_New(argcount, nlocals, stacksize, flags,
              code, consts, names, varnames, filename, name,
@@ -263,8 +284,7 @@
                if (!PyString_Check(v))
                        continue;
                p = PyString_AsString(v);
-               if ((int)strspn(p, NAME_CHARS)
-                   != PyString_Size(v))
+               if(!all_name_chars(p))
                        continue;
                PyString_InternInPlace(&PyTuple_GET_ITEM(consts, i));
        }
-- 
                          I won't rest till it's the best ...
                          Manager, Linux System Software
                          Paul Jackson <pj at sgi.com> 1.650.933.1373



More information about the Python-list mailing list