can multi-core improve single funciton?

Chris Rebert clp2 at rebertia.com
Tue Feb 10 07:43:50 CET 2009


On Mon, Feb 9, 2009 at 10:28 PM, oyster <lepto.python at gmail.com> wrote:
> I mean this
> [code]
> def fib(n):
>    if n<=1:
>        return 1
>    return fib(n-1)+fib(n-2)
>
> useCore(1)
> timeit(fib(500)) #this show 20 seconds
>
> useCore(2)
> timeit(fib(500)) #this show 10 seconds
> [/code]
>
> Is it possible?
>
> and more, can threads/multi-processors/clusters be used to improve fib?

Considering the GIL, I highly doubt it so. Also, the algorithm is
fundamentally linear -- each result depends on previous results -- so
I don't think you can easily parallelize it (if at all). You'd
probably get greater speedup using memoization.

Cheers,
Chris

-- 
Follow the path of the Iguana...
http://rebertia.com



More information about the Python-list mailing list