Print

Print


On Mon, May 18, 2015 at 10:34 PM, Eric Lease Morgan <[log in to unmask]> wrote:

> On May 18, 2015, at 9:23 PM, Galen Charlton <[log in to unmask]> wrote:
>
> >> I have two scripts, attached. They do EXACTLY the same thing
> >> in almost EXACTLY the same manner, but the Python script is
> >> almost 25 times slower than the Perl script:
> >
> > I'm no Python expert, but I think that the difference is much more
> > likely due to which JSON processor is being used.  I suspect your Perl
> > environment has the JSON::XS module, which is written in C, is fast,
> > and is automatically invoked (if present) by "use JSON;".
> >
> > In contrast, I believe that the Python "json" library is written in
> > Python itself.  I tried swapping in cjson and UltraJSON [1] in place
> > of "json" in your Python script, and in both cases it ran rather
> > faster.
> >
> > [1] https://github.com/esnme/ultrajson
>
>
> Thank you. After using the Python module ujson instead of json, the speed
> of my two scripts is now all but equal. Whew! —Eric
>

This is kind of weird, since the stdlib json module should be using a C
extension (from _json). ultrajson does claim to be faster than simplejson
(which is the library that got incorporated into the python 2.6 stdlib as
"json"), but I wouldn't have expected it to make that much of a difference.

Out of curiosity, does "import _json" work on your Python? I'm wondering if
the C extension didn't build for some reason.