Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

ANN: Pyrex - a language for writing Python extension modules

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


greg at cosc

Apr 3, 2002, 11:52 PM

Post #1 of 9 (2029 views)
Permalink
ANN: Pyrex - a language for writing Python extension modules

Folks here may be interested in my latest
project: A new language specially designed for
writing Python extension modules.

Pyrex is more or less Python with C data types
added. You can write functions which freely
intermix operations on Python and C data, with
all Python reference counting and error checking
completely automated.

An example Pyrex module is shown below. For more
information, see:

http://www.cosc.canterbury.ac.nz/~greg/python/Pyrex/

where you can also download version 0.1 and try it
out. I'm very keen to get feedback on this, both to
find out whether people think it's useful, and to
help me debug it!

Oh, yeah, here's the example. It finds prime
numbers at C speed.

def primes(int kmax):
cdef int n, k, i
cdef int p[1000]
result = []
if kmax > 1000:
kmax = 1000
k = 0
n = 2
while k < kmax:
i = 0
while i < k and n % p[i] <> 0:
i = i + 1
if i == k:
p[k] = n
k = k + 1
result.append(n)
n = n + 1
return result

--
Greg Ewing, Computer Science Dept, University of Canterbury,
Christchurch, New Zealand
To get my email address, please visit my web page:
http://www.cosc.canterbury.ac.nz/~greg


skip at pobox

Apr 4, 2002, 2:50 PM

Post #2 of 9 (1966 views)
Permalink
ANN: Pyrex - a language for writing Python extension modules [In reply to]

Greg> Pyrex is more or less Python with C data types added. You can
Greg> write functions which freely intermix operations on Python and C
Greg> data, with all Python reference counting and error checking
Greg> completely automated.

Interesting, but why not simply attach the type information to function
attributes so the Pyrex code really is just Python? The Pyrex compiler can
fish the type information out of function attributes as readily as from the
program source code.

The advantages? I can think of a couple right off the bat:

You can defer Pyrex compilation until you realize you need it. Naively
compiling all your code to C makes for a lot of (object) code bloat and
doesn't necessarily speed anything up (if, for example, the code is I/O
bound or not executed frequently enough). This was one of the problems
with Python2C.

You could train PyChecker about the data type information, which it
could use to produce more (and more useful) warnings about suspect
Python usage.

--
Skip Montanaro (skip [at] pobox - http://www.mojam.com/)


eppstein at ics

Apr 4, 2002, 6:19 PM

Post #3 of 9 (1962 views)
Permalink
ANN: Pyrex - a language for writing Python extension modules [In reply to]

In article <mailman.1017952983.647.clpa-moderators [at] python>,
Greg Ewing <greg [at] cosc> wrote:

> Oh, yeah, here's the example. It finds prime
> numbers at C speed.
>
> def primes(int kmax):
> cdef int n, k, i
> cdef int p[1000]
> result = []
> if kmax > 1000:
> kmax = 1000
> k = 0
> n = 2
> while k < kmax:
> i = 0
> while i < k and n % p[i] <> 0:
> i = i + 1
> if i == k:
> p[k] = n
> k = k + 1
> result.append(n)
> n = n + 1
> return result

Not to criticise your project, but this is not a good example to use --
you've started with a very unnecessarily slow algorithm. For finding
prime numbers, the Sieve of Eratosthenes is much better. See e.g.
<http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/117119> which I
implemented using simple generators so you don't need the kmax parameter.

--
David Eppstein UC Irvine Dept. of Information & Computer Science
eppstein [at] ics http://www.ics.uci.edu/~eppstein/


greg at cosc

Apr 7, 2002, 10:57 PM

Post #4 of 9 (1965 views)
Permalink
ANN: Pyrex - a language for writing Python extension modules [In reply to]

David Eppstein wrote:
>
> Not to criticise your project, but this is not a good example to use --
> you've started with a very unnecessarily slow algorithm.

The point of the example isn't to show how to find
prime numbers quickly. It's just a representative
CPU-intensive task that demonstrates what Pyrex
can achieve. Using a more efficient algorithm
wouldn't make that point any more effectively.

--
Greg Ewing, Computer Science Dept, University of Canterbury,
Christchurch, New Zealand
To get my email address, please visit my web page:
http://www.cosc.canterbury.ac.nz/~greg


eppstein at ics

Apr 7, 2002, 11:23 PM

Post #5 of 9 (1968 views)
Permalink
ANN: Pyrex - a language for writing Python extension modules [In reply to]

In article <3CB13147.89F02706 [at] cosc>,
Greg Ewing <greg [at] cosc> wrote:

> > Not to criticise your project, but this is not a good example to use --
> > you've started with a very unnecessarily slow algorithm.
>
> The point of the example isn't to show how to find prime numbers
> quickly. It's just a representative CPU-intensive task that
> demonstrates what Pyrex can achieve. Using a more efficient algorithm
> wouldn't make that point any more effectively.

But I think your example makes a different point even more effectively:
if you're having speed bottlenecks that would cause you to want to use
Pyrex or similar extension modules, you can often do a lot better by
instead applying some thought and using a better algorithm.

If you're using algorithms that are as efficient as you can make them
and still not getting enough speed, that may be the time to start using
extension modules.

--
David Eppstein UC Irvine Dept. of Information & Computer Science
eppstein [at] ics http://www.ics.uci.edu/~eppstein/


borcis at geneva-link

Apr 8, 2002, 5:17 AM

Post #6 of 9 (1967 views)
Permalink
ANN: Pyrex - a language for writing Python extension modules [In reply to]

David Eppstein wrote:

> In article <3CB13147.89F02706 [at] cosc>,
> Greg Ewing <greg [at] cosc> wrote:
>
>>The point of the example isn't to show how to find prime numbers
>>quickly. It's just a representative CPU-intensive task that
>>demonstrates what Pyrex can achieve. Using a more efficient algorithm
>>wouldn't make that point any more effectively.
>>
>
> But I think your example makes a different point even more effectively:


If such was true, the different point would not need explicitation.
Frankly I feel you are being overly pedantic. While the sieve is
efficient, it's neither secret magic nor particularly readable as an
expression of the definition of prime numbers. And I do feel Greg's
efforts deserve more relevant feedback.

Regards, Boris Borcic
--
python >>> filter(lambda W : W not in "ILLITERATE","BULLSHIT")


evan at 4-am

Apr 8, 2002, 6:01 PM

Post #7 of 9 (1948 views)
Permalink
ANN: Pyrex - a language for writing Python extension modules [In reply to]

Greg Ewing wrote:
> Pyrex is more or less Python with C data types
> added. You can write functions which freely
> intermix operations on Python and C data, with
> all Python reference counting and error checking
> completely automated.

It's SPython! Yaay! (If you don't know what I'm on about, see
http://groups.google.com/groups?selm=HeIo2.78%24u5.3610%40news-dal.corridex.com
and it's thread).

Thanks,

Evan @ 4-am


mrchameleon at hotmail

Apr 8, 2002, 10:06 PM

Post #8 of 9 (1970 views)
Permalink
ANN: Pyrex - a language for writing Python extension modules [In reply to]

>>
>> Not to criticise your project, but this is not a good example to use --
>> you've started with a very unnecessarily slow algorithm. For finding
>> prime numbers, the Sieve of Eratosthenes is much better. See e.g.
>> <http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/117119> which I
>> implemented using simple generators so you don't need the kmax parameter.
>>
>> --
>> David Eppstein UC Irvine Dept. of Information & Computer Science

Are you kidding, David? That is "very unnecessarily" (sic) banal.


greg at cosc

Apr 9, 2002, 7:29 PM

Post #9 of 9 (1950 views)
Permalink
ANN: Pyrex - a language for writing Python extension modules [In reply to]

Skip Montanaro <skip [at] pobox>:

> Interesting, but why not simply attach the type information to function
> attributes so the Pyrex code really is just Python?

I imagine that would look extremely ugly. Also, functions aren't the
only places that type information, and other kinds of information, is
needed in Pyrex. Have you read the Language Overview? There's much,
much more to Pyrex than just annotating things with types.

> You can defer Pyrex compilation until you realize you need it. Naively
> compiling all your code to C makes for a lot of (object) code bloat and
> doesn't necessarily speed anything up (if, for example, the code is I/O
> bound or not executed frequently enough). This was one of the problems
> with Python2C.

You're not meant to compile all your Python code with Pyrex.
You only put Python code in a Pyrex module if it's needed to
interface with non-Python code. Pyrex isn't designed to speed
up Python code, but to bridge the gap between Python and
non-Python. Speed, if any, comes from the non-Python code
(Pyrex code operating on C data, or external C code).

Greg Ewing, Computer Science Dept, +--------------------------------------+
University of Canterbury, | A citizen of NewZealandCorp, a |
Christchurch, New Zealand | wholly-owned subsidiary of USA Inc. |
greg [at] cosc +--------------------------------------+

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.