[Stackless] Video on channels

OvermindDL1 overminddl1 at gmail.com
Tue Sep 11 21:49:38 CEST 2007


I have to ask, is there any way to speed up such a thing, because even
with psyco on full the above numbers do not have any real change
(including the generator version above, with psyco it ran in 103.518
seconds finding 10k primes, I upped the recursionlimit to max in the
stackless library) and run in about the same amount of time on my pc
as did the above test.  I wrote a version in C++ in a similar style to
Hallgrimur's and for 10k iterations it ran in 2.380 seconds.  A minor
change, perhaps on the order of just a few multiples would be
acceptable (it is still interpreted after all), but this is on the
order of unusably huge.  I also tested mine going to 100,000 primes
(did not run out of stack space yet), 295.211 seconds.
10k primes on mine are, as stated 2.380 seconds, and if I replace the
printf("%i\n", primes()); with a primes(); line (making sure it does
not optimize out, that was fun) then it drops to 1.844 seconds for 10k
primes.

This is the program I whipped up, I make no claims of accuracy or
what-not, took less then 5 minutes to write (if you notice, I used
boost shared pointer, although since this is a one shot app then it is
probably unnecessary and could be a little bit faster if memory was
just allowed to leak, also, this is not how my programs normally look,
so ignore the bad layout):

#include <stdio.h>
#include <boost/shared_ptr.hpp>

class baseCall
{
public:
	virtual int operator()(void)
	{
		return 0;
	}
};
typedef boost::shared_ptr<baseCall> baseCallPtr;

class counter : public baseCall
{
	int val;

public:
	counter(int start=1) : val(start) {}

	virtual int operator()(void)
	{
		return val++;
	}
};

class filter : public baseCall
{
	int p;
	baseCallPtr r;

public:
	filter(int _p, baseCallPtr _r): p(_p), r(_r) {}

	virtual int operator()(void)
	{
		while(true)
		{
			int x = (*r)();
			if(x%p) return x;
		}
	}
};

baseCallPtr applyfilter(baseCallPtr ch, int p)
{
	return baseCallPtr(new filter(p, ch));
}

class sieve
{
	baseCallPtr ch;
public:
	sieve(void) : ch(new counter(2))
	{
	}

	__inline int operator()(void)
	{
		int p = (*ch)();
		ch = applyfilter(ch, p);
		return p;
	}
};

int main(int argc, char *argv[], char *envp[])
{
	int goal=100;
	if(argc>=2)
	{
		goal=atoi(argv[1]);
	}

	sieve primes;

	for(int i=0;i<goal;++i)
	{
		printf("%i\n", primes());
	}

	return 0;
}




So, as asked, does anyone have ideas of getting the Python version
noticably faster using this style?



On 9/10/07, Arnar Birgisson <arnarbi at gmail.com> wrote:
> On 9/10/07, Johan Carlsson <johanc at easypublisher.com> wrote:
> > Ok FYI, I run the profiler on my imperative code and Hallgrimur
> > Gunnarsson stackless version of sieve prime (at 10000 iterations,
> > Arnars code wouldn't make it):
> >
> > 134731 function calls in 246.349 CPU seconds for my imperative version.
> > 10004 function calls in 141.017 CPU seconds for Hallgrimur version.
> > (124731 function calls in 250.432 CPU seconds for mine using tuple
> > instead of list)
> >
> > Clearly the overhead for bookkeeping is relevant of efficiency.
>
> Did you run your imperative version with standard python or stackless python?
>
> Arnar
>
> _______________________________________________
> Stackless mailing list
> Stackless at stackless.com
> http://www.stackless.com/mailman/listinfo/stackless
>




More information about the Stackless mailing list