Pyhon 2.x or 3.x, which is faster?

BartC bc at freeuk.com
Mon Mar 7 19:22:06 EST 2016


On 07/03/2016 23:40, Chris Angelico wrote:
> On Tue, Mar 8, 2016 at 9:39 AM, BartC <bc at freeuk.com> wrote:

>> I'm using it because this kind of file reading in Python is a mess. If I do
>> a read, will I get a string, a byte sequence object, a byte-array, or
>> array-array, or what?
>
> Uhh.... you'll get either a text string or a byte string, depending on
> whether you opened the file as text or binary. Where's the mess?

(Is a byte string the same as a byte array? Is a byte array the same as 
an array.array? If I remove this line from my code, where 'data' has 
just been read from a file:

    data=array.array('B',data)

then it still works - Python 3. But not on Python 2. If I do .read on a 
binary file I get a byte string in Python 3, but a string in Python 2. 
That sort of mess.

And how do I write that deceptively simple header on the output file 
without array.array because I tried all sorts:

    f = open(file+".ppm", "wb")
    s="P6\n%d %d\n255\n" % (hdr.width, hdr.height)
    sbytes=array.array('B',list(map(ord,s)))
    f.write(sbytes)

Note this file mixes text and binary mode - blame the ppm format - but 
is mostly binary.)

>> But I also said I am interested in using the languages to directly implement
>> programs and algorithms not just for scripting. Then you need to be able to
>> measure what the core language is capable of.
>
> Yeah, because the value of human legs is proven by walking from one
> city to the next and seeing how long it takes. You're still using the
> language inefficiently, and then proving that one inefficient use is
> less inefficient than another. Congratulations.

So whoever created the speed comparison site linked to at the start of 
the thread is wasting their time?

Obviously some people are interested in this stuff, even if you're not.

The slow speed of Python (and a number of other scripting languages) is 
well known. But obviously that is not any sort of issue, and these 
people working flat out on tracing JIT compilers are all wasting their 
time too!

-- 
Bartc



More information about the Python-list mailing list