Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Python is faster than C

Reply
Thread Tools

Python is faster than C

 
 
Duncan Booth
Guest
Posts: n/a
 
      04-04-2004
Joe Mason <> wrote in
news::

> In article <>, Armin Rigo wrote:
>> The reason that Psyco manages to outperform the C implementation is not
>> that gcc is a bad compiler (it is about 10 times better than Psyco's).
>> The reason is that the C implementation must use a generic '<' operator
>> to compare elements, while the Psyco version quickly figures out that it
>> can expect to find ints in the list; it still has to check this
>> assumption, but this is cheap and then the comparison is done with a
>> single machine instruction.

>
> Why can't the C implementation do the same thing?
>

It easily could, however sorting a list of ints sounds to me like a
comparatively unusual thing to do in any real application. Sorting strings,
or sorting more complex data structures are much more usual. In other words
it would be kind of pointless to introduce an optimisation that only helps
speedup benchmarks.

Also the builtin sort handles other cases which are important. Not all data
you want to sort is randomly ordered. The builtin sort method should
outperform quicksort in those cases where the input is already largely
sorted. The builtin sort is also stable, although for a list of random
integers you might be hard pushed to tell
 
Reply With Quote
 
 
 
 
Michel Claveau/Hamster
Guest
Posts: n/a
 
      04-04-2004
Hi !

Yes, but Psyco is writted in C & Python, and it use an C module.
Said "Psyco is faster than C", it's like said "Psyco is faster than Psyco".

@-salutations (and sorry for my *bad english*)
--
Michel Claveau



 
Reply With Quote
 
 
 
 
Michel Claveau/Hamster
Guest
Posts: n/a
 
      04-04-2004
Hi !

Sorry, but i had let down C language. I can't compare.
See also my answer to Josiah Carlson.

And Psyco is sufficiently good not to need ambiguous arguments.

@-salutations
--
Michel Claveau




 
Reply With Quote
 
Jeff Epler
Guest
Posts: n/a
 
      04-04-2004
from pprint import *
def install_hook():
def displayhook(v):
import __main__
if v is not None:
if isiterator(v):
print "<%s object at 0x%8x:" % (
type(v).__name__, id(v)),
v, copy = itertools.tee(v)
for elem in itertools.islice(copy, 3):
print saferepr(elem),
try:
copy.next()
except StopIteration:
pass
else:
print '...',
sys.stdout.softspace=0
print ">"
else:
pprint(v)
__main__._ = v

sys.displayhook = displayhook

Jeff

 
Reply With Quote
 
Andrew Dalke
Guest
Posts: n/a
 
      04-04-2004
Armin Rigo:
> Another example would be 'a'*999999999: the result is a string, but
> there is no reason that it takes 100MB of memory. Instead, store it
> into a C structure that contains a pointer to the original string object
> 'a' and the repetition counter, but still give this C structure the
> Python type str, so that the difference doesn't show up and the Python
> language remains simple.


I've written lazy code like this for my own projects, where the
concrete object wasn't fully created until needed. One of the
problems I had with it was error behaviour. In this case, suppose
there isn't enough memory available. Python as is will attempt to
allocate enough space when you request it and raise a MemoryError
if there isn't enough space.

Python as you propose it may allocate the string-like object and
only later (eg, when passing the object to some external C
function which expects a char *) realize the full string. There
isn't enough memory so the allocation fails, raising a MemoryError.
But how is the programmer to realize which operations may raise
a MemoryError? Instead, will everything need a try/except guard
around it?

Andrew



 
Reply With Quote
 
Josiah Carlson
Guest
Posts: n/a
 
      04-04-2004
> Yes, but Psyco is writted in C & Python, and it use an C module.
> Said "Psyco is faster than C", it's like said "Psyco is faster than Psyco".


You probably meant "C is faster than C". That is a reasonable
statement, but users of Psyco don't need to write anything special
beyond a handful of extra statements in Python.

To the user, Psyco is a module that makes things fast. They wrote
everything in Python, so to them, "Python with Psyco is faster than C"
is far more accurate.

- Josiah
 
Reply With Quote
 
Carl Banks
Guest
Posts: n/a
 
      04-05-2004
Raymond Hettinger wrote:
>
>
> [Armin Rigo]
>> >>> enumerate([6,7,8,9]) # uh ?

>> <enumerate object at 0x401a102c>

>
> This got me thinking about how much I would like to see the contents
> of an iterator at the interactive prompt.
>
> I wonder if the read-eval-print loop could be trained to make a better
> display:
>
> # rough pseudo-code sketch
> while 1:
> command = raw_input()
> result = eval(command)
> if result is None:
> continue
> if is_iterator(result):
> result, copy = itertools.tee(result)
> print "<%s object at 0x%8x:" %
> (type(result).__name__, id(result)),
> for elem in itertools.islice(copy, 3):
> print repr(elem),
> else:
> print '...',
> print '>'
> else:
> print repr(result)
> _ = result
>
>
> # intended result
>>>> enumerate('abcdefgh')

> <enumerate object at 0x401a102c: (0, 'a') (1, 'b') (2, 'c') ...>
>>>> list(_)

> [(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd'), (4, 'e'), (5, 'f'), (6, 'g'),
> (7, 'h'), (8, 'i'), (9, 'j'), (10, 'k'), (11, 'l'), (12, 'm'), (13,
> 'n')]



I thought this myself, but what if the iterator is computationally
intensive?


--
CARL BANKS http://www.aerojockey.com/software
"If you believe in yourself, drink your school, stay on drugs, and
don't do milk, you can get work."
-- Parody of Mr. T from a Robert Smigel Cartoon
 
Reply With Quote
 
Carl Banks
Guest
Posts: n/a
 
      04-05-2004
Armin Rigo wrote:
> Paul Rubin wrote:
>> I think you're saying that instead of having xrange return a special
>> object, range should return that special object instead. I'm not too
>> clear on the distinction.

>
> No, range should return an object that is a list, as far as you can tell
> from Python, but which is represented more efficiently than an array of
> objects internally. The distinction is between the language level (it
> would be a list, with all operations, etc.) and the implementation
> (there is no reason why all lists should be arrays of PyObjects
> internally).
>
> Another example would be 'a'*999999999: the result is a string, but
> there is no reason that it takes 100MB of memory. Instead, store it
> into a C structure that contains a pointer to the original string object
> 'a' and the repetition counter, but still give this C structure the
> Python type str, so that the difference doesn't show up and the Python
> language remains simple. (This is a bit difficult to implement
> currently in CPython, but not impossible.)


Hmm. What would be really cool is an abstract sequence type with all
kinds of knobs to specify the operations you want to support. In
other words, rather than saying "I want a vector" or "I want a linked
list," you say, "I want fast indexing, fast sorting, no insertion, and
no resizing," or "I want no indexing, no sorting, fast insertion, fast
appending on both ends". Then, let the computer choose the actual
implementation.

Better yet, if the compilation is highly optimized, the compiler could
trace the path of the object (best as it can) through the program, and
maybe use profiling data too, to see for itself the how the object is
used, thus freeing the programmer from even specifying the operations.
The programmer could say, "I want a sequence," use it, and the
compiler could choose the best implementation.

The only thing is, I think that would be prohibitively difficult in an
on-the-fly compiled language like Python. Even having a list with one
or two optimized forms would have drawbacks: it would be hard to
optimize the fast parts with the interpretter second-guessing you all
the time, and it would be hard to write extension modules, since
they'd have to be aware of all the different layouts, or have to
always use an API.

And, frankly, I think having a simple implementation is important,
too. Complex implementations are more likely to have lots of bugs
that could burden users more than some distinct optimized types would.
In my opinion, Python is doing the right thing by having one internal
form per type.

Given that, and keeping in mind that some of these optimizations
really do help, the question to ask is: do the extra power and
efficiency the optimized version gives you justify the extra burden of
complexity on the programmer? I believe that the answer is
occasionally yes.

Consider tuples. Tuples are for the most part an optimized version
of list, but with a few powers list doesn't have, and without a few
powers list has. It's the same with iterators.

IMO, the the benefit an iterator gives you is far more than the
benefit a tuple gives you. I would say 'yes' for an iterator, it's
worth a bit of complexity; and probably 'no' for a tuple (although
it's close).


my-two-cents-ly y'rs,

--
CARL BANKS http://www.aerojockey.com/software
"If you believe in yourself, drink your school, stay on drugs, and
don't do milk, you can get work."
-- Parody of Mr. T from a Robert Smigel Cartoon
 
Reply With Quote
 
Raymond Hettinger
Guest
Posts: n/a
 
      04-05-2004
[Armin Rigo]
> Armin> Worse, and more importantly, the optimization starts to
> Armin> become visible to the programmer. Iterators, for example,
> Armin> are great in limited cases but I consider their
> Armin> introduction a significant complication in the language;
> Armin> before, you could expect that some function from which you
> Armin> would expect a sequence returned a list. Python was all
> Armin> lists and


[Ville Vainio]
> Iterators are an optimization? I always considered them just a more
> clean and elegant way to do the same thing .


In the planning discussions, optimization was not mentioned as goal.
In fact, I think everyone was surprised that they happened to run
faster than the __getitem__ hack. It was also a surprise at how much
they unified the language and made the various pieces work together a
little better.

The original goals were much smaller:
* less cumbersome file iteration (eliminating the need for xreadlines
and reducing the need for the fileinput module).
* providing an extensible interface
* making the code for non-sequence collections more concise and
readable

There was a thought that the performance of dictionary iteration would
improve. Also, while it was not discussed explicitly, everyone was
aware that iterators were more memory/cache friendly than their list
based counterparts.


Raymond Hettinger
 
Reply With Quote
 
Raymond Hettinger
Guest
Posts: n/a
 
      04-05-2004
[Jeff Epler]
> from pprint import *
> def install_hook():
> def displayhook(v):
> import __main__
> if v is not None:
> if isiterator(v):
> print "<%s object at 0x%8x:" % (
> type(v).__name__, id(v)),
> v, copy = itertools.tee(v)
> for elem in itertools.islice(copy, 3):
> print saferepr(elem),
> try:
> copy.next()
> except StopIteration:
> pass
> else:
> print '...',
> sys.stdout.softspace=0
> print ">"
> else:
> pprint(v)
> __main__._ = v
>
> sys.displayhook = displayhook


This is very nice.

In my sketch, I used Py2.4's itertools.tee() to handle non-restartable
iterators. For Py2.3, a good alternative is:

copy = list(itertools.islice(v, 3))
v = itertools.chain(copy, v)

Now, copy can be used in the display and "v" produces the same values
as the original iterator.


Cheers,

Raymond Hettinger


P.S. If some experimentation shows your code to be useful at the
interactive prompt, please submit a patch on SF so it won't be lost.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
RE: Wow, Python much faster than MatLab Doran, Harold Python 10 01-01-2007 06:56 PM
Wow, Python much faster than MatLab Stef Mientki Python 11 01-01-2007 02:05 AM
Lisp development with macros faster than Python development?.. seberino@spawar.navy.mil Python 37 07-11-2005 02:10 PM
Re: Python is faster than C Armin Rigo Python 6 04-05-2004 04:33 PM
RE: Python is faster than C Robert Brewer Python 2 04-05-2004 04:16 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57