Python & Go

Terry Reedy tjreedy at udel.edu
Mon Nov 16 04:00:59 EST 2009


Simon Forman wrote:
> On Sat, Nov 14, 2009 at 5:10 PM, Terry Reedy <tjreedy at udel.edu> wrote:
>> Paul Rubin wrote:
>>
>>> Mark Chu-Carroll has a new post about Go:
>>>
>>>
>>>  http://scienceblogs.com/goodmath/2009/11/the_go_i_forgot_concurrency_an.php
>> In a couple of minutes, I wrote his toy prime filter example in Python,
>> mostly from the text rather than the code, which I can barely stand to read.
>> It ran the first time without error.
>>
>> def plurals():
>>  i = 2
>>  while True:
>>    yield i
>>    i += 1
>>
>> def primefilter(src, prime):
>>  for i in src:
>>    if i % prime:
>>      yield i
>>
>> src = plurals()
>> while True:
>>  i = next(src)
>>  print(i)
>>  src = primefilter(src, i)
>>
>> As I commented there
>> "It stopped at 7877 when it hit the default recursion limit of 1000, which
>> could easily be increased to get out-of-memory error instead.
>>
>> I think Google is making a blunder if it moves to another old-fashioned
>> language whose code is bloated with junky boilerplate that doubles the size.
>> It would be much better, for instance, to tweak Python, which it has had
>> great success with, to better run on multiple cores."
>>
>> Terry Jan Reedy
> 
> FWIW,
> 
> def plurals():
>  i = 3
>  while True:
>    yield i
>    i += 2

Of course, in fact, I thought of at the time

def plurals():
   i = 6
   while True:
     yield i-1
     yield i+1
     i += 6

5,7, 11,13, 17,19, 23,(25-first miss), 29,31, (35-2nd miss),37, 41,43, 
47,(49 erd)...Reduced the base cases by another 1/3. I *think* without 
measuring, that that compensates for the extra -1,+1. But I was 
implementing *Mark's* toy example, and first posted it on his blog, so I 
copied his naive version.

Even better, to avoid extra -1, +1

def plurals():
   yield 2
   yield 3
   i = 5
   while True:
     yield i
     i += 2
     yield i
     i += 4

Of course, testing with primes greater that square root(candidate) is 
wasteful, and recusion should be converted to iteration to avoid 
recursion limit.

Terry Jan Reedy




More information about the Python-list mailing list